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