| OLD | NEW | 
| (Empty) |  | 
 |    1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 
 |    2 // Use of this source code is governed by a BSD-style license that can be | 
 |    3 // found in the LICENSE file. | 
 |    4  | 
 |    5 #include "chrome/browser/prefs/incognito_user_pref_store.h" | 
 |    6  | 
 |    7 #include "base/memory/scoped_ptr.h" | 
 |    8 #include "base/values.h" | 
 |    9 #include "chrome/common/pref_names.h" | 
 |   10  | 
 |   11 IncognitoUserPrefStore::IncognitoUserPrefStore( | 
 |   12     PersistentPrefStore* underlay) | 
 |   13     : underlay_(underlay) { | 
 |   14   underlay_->AddObserver(this); | 
 |   15 } | 
 |   16  | 
 |   17 IncognitoUserPrefStore::~IncognitoUserPrefStore() { | 
 |   18   underlay_->RemoveObserver(this); | 
 |   19 } | 
 |   20  | 
 |   21 bool IncognitoUserPrefStore::IsSetInOverlay(const std::string& key) const { | 
 |   22   return overlay_.GetValue(key, NULL); | 
 |   23 } | 
 |   24  | 
 |   25 void IncognitoUserPrefStore::AddObserver(PrefStore::Observer* observer) { | 
 |   26   observers_.AddObserver(observer); | 
 |   27 } | 
 |   28  | 
 |   29 void IncognitoUserPrefStore::RemoveObserver(PrefStore::Observer* observer) { | 
 |   30   observers_.RemoveObserver(observer); | 
 |   31 } | 
 |   32  | 
 |   33 bool IncognitoUserPrefStore::IsInitializationComplete() const { | 
 |   34   return underlay_->IsInitializationComplete(); | 
 |   35 } | 
 |   36  | 
 |   37 PrefStore::ReadResult IncognitoUserPrefStore::GetValue( | 
 |   38     const std::string& key, | 
 |   39     const Value** result) const { | 
 |   40   // If the |key| shall NOT be stored in the overlay store, there must not | 
 |   41   // be an entry. | 
 |   42   DCHECK(ShallBeStoredInOverlay(key) || !overlay_.GetValue(key, NULL)); | 
 |   43  | 
 |   44   if (overlay_.GetValue(key, result)) | 
 |   45     return READ_OK; | 
 |   46   return underlay_->GetValue(key, result); | 
 |   47 } | 
 |   48  | 
 |   49 PrefStore::ReadResult IncognitoUserPrefStore::GetMutableValue( | 
 |   50     const std::string& key, | 
 |   51     Value** result) { | 
 |   52   if (!ShallBeStoredInOverlay(key)) | 
 |   53     return underlay_->GetMutableValue(key, result); | 
 |   54  | 
 |   55   if (overlay_.GetValue(key, result)) | 
 |   56     return READ_OK; | 
 |   57  | 
 |   58   // Try to create copy of underlay if the overlay does not contain a value. | 
 |   59   Value* underlay_value = NULL; | 
 |   60   PrefStore::ReadResult read_result = | 
 |   61       underlay_->GetMutableValue(key, &underlay_value); | 
 |   62   if (read_result != READ_OK) | 
 |   63     return read_result; | 
 |   64  | 
 |   65   *result = underlay_value->DeepCopy(); | 
 |   66   overlay_.SetValue(key, *result); | 
 |   67   return READ_OK; | 
 |   68 } | 
 |   69  | 
 |   70 void IncognitoUserPrefStore::SetValue(const std::string& key, | 
 |   71                                       Value* value) { | 
 |   72   if (!ShallBeStoredInOverlay(key)) { | 
 |   73     underlay_->SetValue(key, value); | 
 |   74     return; | 
 |   75   } | 
 |   76  | 
 |   77   if (overlay_.SetValue(key, value)) | 
 |   78     ReportValueChanged(key); | 
 |   79 } | 
 |   80  | 
 |   81 void IncognitoUserPrefStore::SetValueSilently(const std::string& key, | 
 |   82                                               Value* value) { | 
 |   83   if (!ShallBeStoredInOverlay(key)) { | 
 |   84     underlay_->SetValueSilently(key, value); | 
 |   85     return; | 
 |   86   } | 
 |   87  | 
 |   88   overlay_.SetValue(key, value); | 
 |   89 } | 
 |   90  | 
 |   91 void IncognitoUserPrefStore::RemoveValue(const std::string& key) { | 
 |   92   if (!ShallBeStoredInOverlay(key)) { | 
 |   93     underlay_->RemoveValue(key); | 
 |   94     return; | 
 |   95   } | 
 |   96  | 
 |   97   if (overlay_.RemoveValue(key)) | 
 |   98     ReportValueChanged(key); | 
 |   99 } | 
 |  100  | 
 |  101 bool IncognitoUserPrefStore::ReadOnly() const { | 
 |  102   return false; | 
 |  103 } | 
 |  104  | 
 |  105 PersistentPrefStore::PrefReadError IncognitoUserPrefStore::ReadPrefs() { | 
 |  106   // We do not read intentionally. | 
 |  107   OnInitializationCompleted(true); | 
 |  108   return PersistentPrefStore::PREF_READ_ERROR_NONE; | 
 |  109 } | 
 |  110  | 
 |  111 void IncognitoUserPrefStore::ReadPrefsAsync( | 
 |  112     ReadErrorDelegate* error_delegate_raw) { | 
 |  113   scoped_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw); | 
 |  114   // We do not read intentionally. | 
 |  115   OnInitializationCompleted(true); | 
 |  116 } | 
 |  117  | 
 |  118 bool IncognitoUserPrefStore::WritePrefs() { | 
 |  119   // We do not write our content intentionally. | 
 |  120   return true; | 
 |  121 } | 
 |  122  | 
 |  123 void IncognitoUserPrefStore::ScheduleWritePrefs() { | 
 |  124   underlay_->ScheduleWritePrefs(); | 
 |  125   // We do not write our content intentionally. | 
 |  126 } | 
 |  127  | 
 |  128 void IncognitoUserPrefStore::CommitPendingWrite() { | 
 |  129   underlay_->CommitPendingWrite(); | 
 |  130   // We do not write our content intentionally. | 
 |  131 } | 
 |  132  | 
 |  133 void IncognitoUserPrefStore::ReportValueChanged(const std::string& key) { | 
 |  134   FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 
 |  135 } | 
 |  136  | 
 |  137 void IncognitoUserPrefStore::OnPrefValueChanged(const std::string& key) { | 
 |  138   if (!overlay_.GetValue(key, NULL)) | 
 |  139     ReportValueChanged(key); | 
 |  140 } | 
 |  141  | 
 |  142 void IncognitoUserPrefStore::OnInitializationCompleted(bool succeeded) { | 
 |  143   FOR_EACH_OBSERVER(PrefStore::Observer, observers_, | 
 |  144                     OnInitializationCompleted(succeeded)); | 
 |  145 } | 
 |  146  | 
 |  147 bool IncognitoUserPrefStore::ShallBeStoredInOverlay( | 
 |  148     const std::string& key) const { | 
 |  149   // List of keys that cannot be changed in the user prefs file by the incognito | 
 |  150   // profile: | 
 |  151   return key == prefs::kBrowserWindowPlacement; | 
 |  152 } | 
| OLD | NEW |