Clear incognito preferences on destroying incognito profile.

Tests are not added, since it's not final version yet.

BUG=444283

Review URL: https://codereview.chromium.org/1643203002

Cr-Commit-Position: refs/heads/master@{#373804}
diff --git a/android_webview/browser/aw_pref_store.h b/android_webview/browser/aw_pref_store.h
index 6607e25..ba720ad 100644
--- a/android_webview/browser/aw_pref_store.h
+++ b/android_webview/browser/aw_pref_store.h
@@ -47,6 +47,7 @@
   void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override;
   void CommitPendingWrite() override {}
   void SchedulePendingLossyWrites() override {}
+  void ClearMutableValues() override {}
 
  protected:
   ~AwPrefStore() override;
diff --git a/chrome/browser/profiles/profile_impl.cc b/chrome/browser/profiles/profile_impl.cc
index cea73a75..01912fb 100644
--- a/chrome/browser/profiles/profile_impl.cc
+++ b/chrome/browser/profiles/profile_impl.cc
@@ -745,6 +745,7 @@
 
 void ProfileImpl::DestroyOffTheRecordProfile() {
   off_the_record_profile_.reset();
+  otr_prefs_->ClearMutableValues();
 #if defined(ENABLE_EXTENSIONS)
   ExtensionPrefValueMapFactory::GetForBrowserContext(this)->
       ClearAllIncognitoSessionOnlyPreferences();
diff --git a/chrome/browser/profiles/profile_manager_browsertest.cc b/chrome/browser/profiles/profile_manager_browsertest.cc
index 536164f..543e112 100644
--- a/chrome/browser/profiles/profile_manager_browsertest.cc
+++ b/chrome/browser/profiles/profile_manager_browsertest.cc
@@ -487,10 +487,27 @@
   ASSERT_TRUE(profile_manager->IsValidProfile(incognito_profile));
   EXPECT_EQ(initial_profile_count, profile_manager->GetNumberOfProfiles());
 
+  // Check that a default save path is not empty, since it's taken from the
+  // main profile preferences, set it to empty and verify that it becomes
+  // empty.
+  EXPECT_FALSE(profile->GetOffTheRecordPrefs()
+                   ->GetFilePath(prefs::kSaveFileDefaultDirectory)
+                   .empty());
+  profile->GetOffTheRecordPrefs()->SetFilePath(prefs::kSaveFileDefaultDirectory,
+                                               base::FilePath());
+  EXPECT_TRUE(profile->GetOffTheRecordPrefs()
+                  ->GetFilePath(prefs::kSaveFileDefaultDirectory)
+                  .empty());
+
   // Delete the incognito profile.
   incognito_profile->GetOriginalProfile()->DestroyOffTheRecordProfile();
 
   EXPECT_FALSE(profile->HasOffTheRecordProfile());
   EXPECT_FALSE(profile_manager->IsValidProfile(incognito_profile));
   EXPECT_EQ(initial_profile_count, profile_manager->GetNumberOfProfiles());
+  // After destroying the incognito profile incognito preferences should be
+  // cleared so the default save path should be taken from the main profile.
+  EXPECT_FALSE(profile->GetOffTheRecordPrefs()
+                   ->GetFilePath(prefs::kSaveFileDefaultDirectory)
+                   .empty());
 }
diff --git a/components/filesystem/public/cpp/prefs/filesystem_json_pref_store.cc b/components/filesystem/public/cpp/prefs/filesystem_json_pref_store.cc
index a31b6f2..6ab90dd 100644
--- a/components/filesystem/public/cpp/prefs/filesystem_json_pref_store.cc
+++ b/components/filesystem/public/cpp/prefs/filesystem_json_pref_store.cc
@@ -160,6 +160,10 @@
   ScheduleWrite(flags);
 }
 
+void FilesystemJsonPrefStore::ClearMutableValues() {
+  NOTIMPLEMENTED();
+}
+
 bool FilesystemJsonPrefStore::ReadOnly() const {
   DCHECK(CalledOnValidThread());
 
diff --git a/components/filesystem/public/cpp/prefs/filesystem_json_pref_store.h b/components/filesystem/public/cpp/prefs/filesystem_json_pref_store.h
index d75fcd6..0c3e2d8 100644
--- a/components/filesystem/public/cpp/prefs/filesystem_json_pref_store.h
+++ b/components/filesystem/public/cpp/prefs/filesystem_json_pref_store.h
@@ -100,6 +100,8 @@
   // cleanup that shouldn't otherwise alert observers.
   void RemoveValueSilently(const std::string& key, uint32_t flags);
 
+  void ClearMutableValues() override;
+
  private:
   friend class base::JsonPrefStoreLossyWriteTest;
 
diff --git a/components/prefs/json_pref_store.cc b/components/prefs/json_pref_store.cc
index 65b6f91..ba7cb91 100644
--- a/components/prefs/json_pref_store.cc
+++ b/components/prefs/json_pref_store.cc
@@ -330,6 +330,10 @@
   writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write);
 }
 
+void JsonPrefStore::ClearMutableValues() {
+  NOTIMPLEMENTED();
+}
+
 void JsonPrefStore::OnFileRead(scoped_ptr<ReadResult> read_result) {
   DCHECK(CalledOnValidThread());
 
diff --git a/components/prefs/json_pref_store.h b/components/prefs/json_pref_store.h
index 4b19c95..ff6e1f2 100644
--- a/components/prefs/json_pref_store.h
+++ b/components/prefs/json_pref_store.h
@@ -111,6 +111,8 @@
   void RegisterOnNextSuccessfulWriteCallback(
       const base::Closure& on_next_successful_write);
 
+  void ClearMutableValues() override;
+
  private:
   // Represents a histogram for recording the number of writes to the pref file
   // that occur every kHistogramWriteReportIntervalInMins minutes.
diff --git a/components/prefs/overlay_user_pref_store.cc b/components/prefs/overlay_user_pref_store.cc
index ff93c64..4d09053 100644
--- a/components/prefs/overlay_user_pref_store.cc
+++ b/components/prefs/overlay_user_pref_store.cc
@@ -161,6 +161,10 @@
   underlay_to_overlay_names_map_[underlay_key] = overlay_key;
 }
 
+void OverlayUserPrefStore::ClearMutableValues() {
+  overlay_.Clear();
+}
+
 OverlayUserPrefStore::~OverlayUserPrefStore() {
   underlay_->RemoveObserver(this);
 }
diff --git a/components/prefs/overlay_user_pref_store.h b/components/prefs/overlay_user_pref_store.h
index 82d13a1..c3382c25d 100644
--- a/components/prefs/overlay_user_pref_store.h
+++ b/components/prefs/overlay_user_pref_store.h
@@ -64,6 +64,8 @@
   void RegisterOverlayPref(const std::string& overlay_key,
                            const std::string& underlay_key);
 
+  void ClearMutableValues() override;
+
  protected:
   ~OverlayUserPrefStore() override;
 
diff --git a/components/prefs/overlay_user_pref_store_unittest.cc b/components/prefs/overlay_user_pref_store_unittest.cc
index fd30ccb..1ac185e 100644
--- a/components/prefs/overlay_user_pref_store_unittest.cc
+++ b/components/prefs/overlay_user_pref_store_unittest.cc
@@ -289,4 +289,23 @@
   EXPECT_TRUE(obs.changed_keys.empty());
 }
 
+// Check that mutable values are removed correctly.
+TEST_F(OverlayUserPrefStoreTest, ClearMutableValues) {
+  // Set in overlay and underlay the same preference.
+  underlay_->SetValue(overlay_key, make_scoped_ptr(new FundamentalValue(42)),
+                      WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
+  overlay_->SetValue(overlay_key, make_scoped_ptr(new FundamentalValue(43)),
+                     WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
+
+  const Value* value = nullptr;
+  // Check that an overlay preference is returned.
+  EXPECT_TRUE(overlay_->GetValue(overlay_key, &value));
+  EXPECT_TRUE(base::FundamentalValue(43).Equals(value));
+  overlay_->ClearMutableValues();
+
+  // Check that an underlay preference is returned.
+  EXPECT_TRUE(overlay_->GetValue(overlay_key, &value));
+  EXPECT_TRUE(base::FundamentalValue(42).Equals(value));
+}
+
 }  // namespace base
diff --git a/components/prefs/persistent_pref_store.h b/components/prefs/persistent_pref_store.h
index 1356f8e5..7bc2827 100644
--- a/components/prefs/persistent_pref_store.h
+++ b/components/prefs/persistent_pref_store.h
@@ -70,6 +70,9 @@
   // isn't one scheduled already.
   virtual void SchedulePendingLossyWrites() = 0;
 
+  // It should be called only for Incognito pref store.
+  virtual void ClearMutableValues() = 0;
+
  protected:
   ~PersistentPrefStore() override {}
 };
diff --git a/components/prefs/pref_service.cc b/components/prefs/pref_service.cc
index aaf9fa7..1ef2313 100644
--- a/components/prefs/pref_service.cc
+++ b/components/prefs/pref_service.cc
@@ -378,6 +378,10 @@
   user_pref_store_->RemoveValue(path, GetWriteFlags(pref));
 }
 
+void PrefService::ClearMutableValues() {
+  user_pref_store_->ClearMutableValues();
+}
+
 void PrefService::Set(const std::string& path, const base::Value& value) {
   SetUserPrefValue(path, value.DeepCopy());
 }
diff --git a/components/prefs/pref_service.h b/components/prefs/pref_service.h
index 50a827c..6c95b64 100644
--- a/components/prefs/pref_service.h
+++ b/components/prefs/pref_service.h
@@ -295,6 +295,9 @@
   // implemented in chrome/browser/prefs/browser_prefs.cc.
   PrefRegistry* DeprecatedGetPrefRegistry();
 
+  // Clears mutable values.
+  void ClearMutableValues();
+
  protected:
   // The PrefNotifier handles registering and notifying preference observers.
   // It is created and owned by this PrefService. Subclasses may access it for
diff --git a/components/prefs/testing_pref_store.cc b/components/prefs/testing_pref_store.cc
index 45c608c..3a8be42c 100644
--- a/components/prefs/testing_pref_store.cc
+++ b/components/prefs/testing_pref_store.cc
@@ -163,6 +163,10 @@
     NotifyInitializationCompleted();
 }
 
+void TestingPrefStore::ClearMutableValues() {
+  NOTIMPLEMENTED();
+}
+
 void TestingPrefStore::set_read_only(bool read_only) {
   read_only_ = read_only;
 }
diff --git a/components/prefs/testing_pref_store.h b/components/prefs/testing_pref_store.h
index ea21769..8fb4b8d 100644
--- a/components/prefs/testing_pref_store.h
+++ b/components/prefs/testing_pref_store.h
@@ -69,6 +69,8 @@
   // the call to ReadPrefsAsync.
   void SetBlockAsyncRead(bool block_async_read);
 
+  void ClearMutableValues() override;
+
   // Getter and Setter methods for setting and getting the state of the
   // |TestingPrefStore|.
   virtual void set_read_only(bool read_only);
diff --git a/components/user_prefs/tracked/segregated_pref_store.cc b/components/user_prefs/tracked/segregated_pref_store.cc
index 177736c..7f5453f 100644
--- a/components/user_prefs/tracked/segregated_pref_store.cc
+++ b/components/user_prefs/tracked/segregated_pref_store.cc
@@ -156,6 +156,10 @@
   selected_pref_store_->SchedulePendingLossyWrites();
 }
 
+void SegregatedPrefStore::ClearMutableValues() {
+  NOTIMPLEMENTED();
+}
+
 SegregatedPrefStore::~SegregatedPrefStore() {
   default_pref_store_->RemoveObserver(&aggregating_observer_);
   selected_pref_store_->RemoveObserver(&aggregating_observer_);
diff --git a/components/user_prefs/tracked/segregated_pref_store.h b/components/user_prefs/tracked/segregated_pref_store.h
index 249f5f2d..2f52f01 100644
--- a/components/user_prefs/tracked/segregated_pref_store.h
+++ b/components/user_prefs/tracked/segregated_pref_store.h
@@ -72,6 +72,8 @@
   void CommitPendingWrite() override;
   void SchedulePendingLossyWrites() override;
 
+  void ClearMutableValues() override;
+
  private:
   // Aggregates events from the underlying stores and synthesizes external
   // events via |on_initialization|, |read_error_delegate_|, and |observers_|.