| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/preference/preference_api.h" | 5 #include "chrome/browser/extensions/api/preference/preference_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 APIPermission::kAccessibilityFeaturesRead, | 164 APIPermission::kAccessibilityFeaturesRead, |
| 165 APIPermission::kAccessibilityFeaturesModify}, | 165 APIPermission::kAccessibilityFeaturesModify}, |
| 166 {"virtualKeyboard", prefs::kAccessibilityVirtualKeyboardEnabled, | 166 {"virtualKeyboard", prefs::kAccessibilityVirtualKeyboardEnabled, |
| 167 APIPermission::kAccessibilityFeaturesRead, | 167 APIPermission::kAccessibilityFeaturesRead, |
| 168 APIPermission::kAccessibilityFeaturesModify}, | 168 APIPermission::kAccessibilityFeaturesModify}, |
| 169 #endif | 169 #endif |
| 170 }; | 170 }; |
| 171 | 171 |
| 172 class IdentityPrefTransformer : public PrefTransformerInterface { | 172 class IdentityPrefTransformer : public PrefTransformerInterface { |
| 173 public: | 173 public: |
| 174 std::unique_ptr<base::Value> ExtensionToBrowserPref( | 174 base::Value* ExtensionToBrowserPref(const base::Value* extension_pref, |
| 175 const base::Value* extension_pref, | 175 std::string* error, |
| 176 std::string* error, | 176 bool* bad_message) override { |
| 177 bool* bad_message) override { | 177 return extension_pref->DeepCopy(); |
| 178 return extension_pref->CreateDeepCopy(); | |
| 179 } | 178 } |
| 180 | 179 |
| 181 std::unique_ptr<base::Value> BrowserToExtensionPref( | 180 base::Value* BrowserToExtensionPref( |
| 182 const base::Value* browser_pref) override { | 181 const base::Value* browser_pref) override { |
| 183 return browser_pref->CreateDeepCopy(); | 182 return browser_pref->DeepCopy(); |
| 184 } | 183 } |
| 185 }; | 184 }; |
| 186 | 185 |
| 187 class InvertBooleanTransformer : public PrefTransformerInterface { | 186 class InvertBooleanTransformer : public PrefTransformerInterface { |
| 188 public: | 187 public: |
| 189 std::unique_ptr<base::Value> ExtensionToBrowserPref( | 188 base::Value* ExtensionToBrowserPref(const base::Value* extension_pref, |
| 190 const base::Value* extension_pref, | 189 std::string* error, |
| 191 std::string* error, | 190 bool* bad_message) override { |
| 192 bool* bad_message) override { | |
| 193 return InvertBooleanValue(extension_pref); | 191 return InvertBooleanValue(extension_pref); |
| 194 } | 192 } |
| 195 | 193 |
| 196 std::unique_ptr<base::Value> BrowserToExtensionPref( | 194 base::Value* BrowserToExtensionPref( |
| 197 const base::Value* browser_pref) override { | 195 const base::Value* browser_pref) override { |
| 198 return InvertBooleanValue(browser_pref); | 196 return InvertBooleanValue(browser_pref); |
| 199 } | 197 } |
| 200 | 198 |
| 201 private: | 199 private: |
| 202 static std::unique_ptr<base::Value> InvertBooleanValue( | 200 static base::Value* InvertBooleanValue(const base::Value* value) { |
| 203 const base::Value* value) { | |
| 204 bool bool_value = false; | 201 bool bool_value = false; |
| 205 bool result = value->GetAsBoolean(&bool_value); | 202 bool result = value->GetAsBoolean(&bool_value); |
| 206 DCHECK(result); | 203 DCHECK(result); |
| 207 return base::MakeUnique<base::Value>(!bool_value); | 204 return new base::Value(!bool_value); |
| 208 } | 205 } |
| 209 }; | 206 }; |
| 210 | 207 |
| 211 class NetworkPredictionTransformer : public PrefTransformerInterface { | 208 class NetworkPredictionTransformer : public PrefTransformerInterface { |
| 212 public: | 209 public: |
| 213 std::unique_ptr<base::Value> ExtensionToBrowserPref( | 210 base::Value* ExtensionToBrowserPref(const base::Value* extension_pref, |
| 214 const base::Value* extension_pref, | 211 std::string* error, |
| 215 std::string* error, | 212 bool* bad_message) override { |
| 216 bool* bad_message) override { | |
| 217 bool bool_value = false; | 213 bool bool_value = false; |
| 218 const bool pref_found = extension_pref->GetAsBoolean(&bool_value); | 214 const bool pref_found = extension_pref->GetAsBoolean(&bool_value); |
| 219 DCHECK(pref_found) << "Preference not found."; | 215 DCHECK(pref_found) << "Preference not found."; |
| 220 if (bool_value) { | 216 if (bool_value) { |
| 221 return base::MakeUnique<base::Value>( | 217 return new base::Value(chrome_browser_net::NETWORK_PREDICTION_DEFAULT); |
| 222 chrome_browser_net::NETWORK_PREDICTION_DEFAULT); | |
| 223 } else { | 218 } else { |
| 224 return base::MakeUnique<base::Value>( | 219 return new base::Value(chrome_browser_net::NETWORK_PREDICTION_NEVER); |
| 225 chrome_browser_net::NETWORK_PREDICTION_NEVER); | |
| 226 } | 220 } |
| 227 } | 221 } |
| 228 | 222 |
| 229 std::unique_ptr<base::Value> BrowserToExtensionPref( | 223 base::Value* BrowserToExtensionPref( |
| 230 const base::Value* browser_pref) override { | 224 const base::Value* browser_pref) override { |
| 231 int int_value = chrome_browser_net::NETWORK_PREDICTION_DEFAULT; | 225 int int_value = chrome_browser_net::NETWORK_PREDICTION_DEFAULT; |
| 232 const bool pref_found = browser_pref->GetAsInteger(&int_value); | 226 const bool pref_found = browser_pref->GetAsInteger(&int_value); |
| 233 DCHECK(pref_found) << "Preference not found."; | 227 DCHECK(pref_found) << "Preference not found."; |
| 234 return base::MakeUnique<base::Value>( | 228 return new base::Value(int_value != |
| 235 int_value != chrome_browser_net::NETWORK_PREDICTION_NEVER); | 229 chrome_browser_net::NETWORK_PREDICTION_NEVER); |
| 236 } | 230 } |
| 237 }; | 231 }; |
| 238 | 232 |
| 239 class PrefMapping { | 233 class PrefMapping { |
| 240 public: | 234 public: |
| 241 static PrefMapping* GetInstance() { | 235 static PrefMapping* GetInstance() { |
| 242 return base::Singleton<PrefMapping>::get(); | 236 return base::Singleton<PrefMapping>::get(); |
| 243 } | 237 } |
| 244 | 238 |
| 245 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, | 239 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 bool rv = PrefMapping::GetInstance()->FindEventForBrowserPref( | 378 bool rv = PrefMapping::GetInstance()->FindEventForBrowserPref( |
| 385 browser_pref, &event_name, &permission); | 379 browser_pref, &event_name, &permission); |
| 386 DCHECK(rv); | 380 DCHECK(rv); |
| 387 | 381 |
| 388 base::ListValue args; | 382 base::ListValue args; |
| 389 const PrefService::Preference* pref = | 383 const PrefService::Preference* pref = |
| 390 pref_service->FindPreference(browser_pref); | 384 pref_service->FindPreference(browser_pref); |
| 391 CHECK(pref); | 385 CHECK(pref); |
| 392 PrefTransformerInterface* transformer = | 386 PrefTransformerInterface* transformer = |
| 393 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); | 387 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 394 std::unique_ptr<base::Value> transformed_value = | 388 base::Value* transformed_value = |
| 395 transformer->BrowserToExtensionPref(pref->GetValue()); | 389 transformer->BrowserToExtensionPref(pref->GetValue()); |
| 396 if (!transformed_value) { | 390 if (!transformed_value) { |
| 397 LOG(ERROR) << ErrorUtils::FormatErrorMessage(kConversionErrorMessage, | 391 LOG(ERROR) << ErrorUtils::FormatErrorMessage(kConversionErrorMessage, |
| 398 pref->name()); | 392 pref->name()); |
| 399 return; | 393 return; |
| 400 } | 394 } |
| 401 | 395 |
| 402 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 396 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 403 dict->Set(keys::kValue, std::move(transformed_value)); | 397 dict->Set(keys::kValue, transformed_value); |
| 404 if (incognito) { | 398 if (incognito) { |
| 405 ExtensionPrefs* ep = ExtensionPrefs::Get(profile_); | 399 ExtensionPrefs* ep = ExtensionPrefs::Get(profile_); |
| 406 dict->SetBoolean(keys::kIncognitoSpecific, | 400 dict->SetBoolean(keys::kIncognitoSpecific, |
| 407 ep->HasIncognitoPrefValue(browser_pref)); | 401 ep->HasIncognitoPrefValue(browser_pref)); |
| 408 } | 402 } |
| 409 args.Append(std::move(dict)); | 403 args.Append(std::move(dict)); |
| 410 | 404 |
| 411 // TODO(kalman): Have a histogram value for each pref type. | 405 // TODO(kalman): Have a histogram value for each pref type. |
| 412 // This isn't so important for the current use case of these | 406 // This isn't so important for the current use case of these |
| 413 // histograms, which is to track which event types are waking up event | 407 // histograms, which is to track which event types are waking up event |
| (...skipping 28 matching lines...) Expand all Loading... |
| 442 // ScopeToPrefName() returns false if the scope is not persisted. | 436 // ScopeToPrefName() returns false if the scope is not persisted. |
| 443 if (pref_names::ScopeToPrefName(scope, &scope_string)) { | 437 if (pref_names::ScopeToPrefName(scope, &scope_string)) { |
| 444 // Also store in persisted Preferences file to recover after a | 438 // Also store in persisted Preferences file to recover after a |
| 445 // browser restart. | 439 // browser restart. |
| 446 ExtensionPrefs::ScopedDictionaryUpdate update(extension_prefs(), | 440 ExtensionPrefs::ScopedDictionaryUpdate update(extension_prefs(), |
| 447 extension_id, | 441 extension_id, |
| 448 scope_string); | 442 scope_string); |
| 449 base::DictionaryValue* preference = update.Get(); | 443 base::DictionaryValue* preference = update.Get(); |
| 450 if (!preference) | 444 if (!preference) |
| 451 preference = update.Create(); | 445 preference = update.Create(); |
| 452 preference->SetWithoutPathExpansion(pref_key, value->CreateDeepCopy()); | 446 preference->SetWithoutPathExpansion(pref_key, value->DeepCopy()); |
| 453 } | 447 } |
| 454 extension_pref_value_map()->SetExtensionPref( | 448 extension_pref_value_map()->SetExtensionPref( |
| 455 extension_id, pref_key, scope, value); | 449 extension_id, pref_key, scope, value); |
| 456 } | 450 } |
| 457 | 451 |
| 458 void PreferenceAPIBase::RemoveExtensionControlledPref( | 452 void PreferenceAPIBase::RemoveExtensionControlledPref( |
| 459 const std::string& extension_id, | 453 const std::string& extension_id, |
| 460 const std::string& pref_key, | 454 const std::string& pref_key, |
| 461 ExtensionPrefsScope scope) { | 455 ExtensionPrefsScope scope) { |
| 462 DCHECK(extension_prefs()->pref_service()->FindPreference(pref_key)) | 456 DCHECK(extension_prefs()->pref_service()->FindPreference(pref_key)) |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); | 623 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); |
| 630 | 624 |
| 631 // Retrieve level of control. | 625 // Retrieve level of control. |
| 632 std::string level_of_control = helpers::GetLevelOfControl( | 626 std::string level_of_control = helpers::GetLevelOfControl( |
| 633 profile, extension_id(), browser_pref, incognito); | 627 profile, extension_id(), browser_pref, incognito); |
| 634 result->SetString(keys::kLevelOfControl, level_of_control); | 628 result->SetString(keys::kLevelOfControl, level_of_control); |
| 635 | 629 |
| 636 // Retrieve pref value. | 630 // Retrieve pref value. |
| 637 PrefTransformerInterface* transformer = | 631 PrefTransformerInterface* transformer = |
| 638 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); | 632 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 639 std::unique_ptr<base::Value> transformed_value = | 633 base::Value* transformed_value = |
| 640 transformer->BrowserToExtensionPref(pref->GetValue()); | 634 transformer->BrowserToExtensionPref(pref->GetValue()); |
| 641 if (!transformed_value) { | 635 if (!transformed_value) { |
| 642 // TODO(devlin): Can this happen? When? Should it be an error, or a bad | 636 // TODO(devlin): Can this happen? When? Should it be an error, or a bad |
| 643 // message? | 637 // message? |
| 644 LOG(ERROR) << | 638 LOG(ERROR) << |
| 645 ErrorUtils::FormatErrorMessage(kConversionErrorMessage, | 639 ErrorUtils::FormatErrorMessage(kConversionErrorMessage, |
| 646 pref->name()); | 640 pref->name()); |
| 647 return RespondNow(Error(kUnknownErrorDoNotUse)); | 641 return RespondNow(Error(kUnknownErrorDoNotUse)); |
| 648 } | 642 } |
| 649 result->Set(keys::kValue, std::move(transformed_value)); | 643 result->Set(keys::kValue, transformed_value); |
| 650 | 644 |
| 651 // Retrieve incognito status. | 645 // Retrieve incognito status. |
| 652 if (incognito) { | 646 if (incognito) { |
| 653 ExtensionPrefs* ep = ExtensionPrefs::Get(browser_context()); | 647 ExtensionPrefs* ep = ExtensionPrefs::Get(browser_context()); |
| 654 result->SetBoolean(keys::kIncognitoSpecific, | 648 result->SetBoolean(keys::kIncognitoSpecific, |
| 655 ep->HasIncognitoPrefValue(browser_pref)); | 649 ep->HasIncognitoPrefValue(browser_pref)); |
| 656 } | 650 } |
| 657 | 651 |
| 658 return RespondNow(OneArgument(std::move(result))); | 652 return RespondNow(OneArgument(std::move(result))); |
| 659 } | 653 } |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 pref_key, &browser_pref, &read_permission, &write_permission)); | 772 pref_key, &browser_pref, &read_permission, &write_permission)); |
| 779 if (!extension()->permissions_data()->HasAPIPermission(write_permission)) | 773 if (!extension()->permissions_data()->HasAPIPermission(write_permission)) |
| 780 return RespondNow(Error(keys::kPermissionErrorMessage, pref_key)); | 774 return RespondNow(Error(keys::kPermissionErrorMessage, pref_key)); |
| 781 | 775 |
| 782 PreferenceAPI::Get(browser_context()) | 776 PreferenceAPI::Get(browser_context()) |
| 783 ->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); | 777 ->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); |
| 784 return RespondNow(NoArguments()); | 778 return RespondNow(NoArguments()); |
| 785 } | 779 } |
| 786 | 780 |
| 787 } // namespace extensions | 781 } // namespace extensions |
| OLD | NEW |