| 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/extension_preference_api.h" | 5 #include "chrome/browser/extensions/extension_preference_api.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 128 |
| 129 private: | 129 private: |
| 130 static Value* InvertBooleanValue(const Value* value) { | 130 static Value* InvertBooleanValue(const Value* value) { |
| 131 bool bool_value = false; | 131 bool bool_value = false; |
| 132 bool result = value->GetAsBoolean(&bool_value); | 132 bool result = value->GetAsBoolean(&bool_value); |
| 133 DCHECK(result); | 133 DCHECK(result); |
| 134 return Value::CreateBooleanValue(!bool_value); | 134 return Value::CreateBooleanValue(!bool_value); |
| 135 } | 135 } |
| 136 }; | 136 }; |
| 137 | 137 |
| 138 // Returns a string constant (defined in the API) indicating the level of | |
| 139 // control this extension has over the specified preference. | |
| 140 const char* GetLevelOfControl( | |
| 141 Profile* profile, | |
| 142 const std::string& extension_id, | |
| 143 const std::string& browser_pref, | |
| 144 bool incognito) { | |
| 145 PrefService* prefs = incognito ? profile->GetOffTheRecordPrefs() | |
| 146 : profile->GetPrefs(); | |
| 147 const PrefService::Preference* pref = | |
| 148 prefs->FindPreference(browser_pref.c_str()); | |
| 149 CHECK(pref); | |
| 150 ExtensionPrefs* ep = profile->GetExtensionService()->extension_prefs(); | |
| 151 | |
| 152 if (!pref->IsExtensionModifiable()) | |
| 153 return keys::kNotControllable; | |
| 154 | |
| 155 if (ep->DoesExtensionControlPref(extension_id, browser_pref, incognito)) | |
| 156 return keys::kControlledByThisExtension; | |
| 157 | |
| 158 if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito)) | |
| 159 return keys::kControllableByThisExtension; | |
| 160 | |
| 161 return keys::kControlledByOtherExtensions; | |
| 162 } | |
| 163 | |
| 164 class PrefMapping { | 138 class PrefMapping { |
| 165 public: | 139 public: |
| 166 static PrefMapping* GetInstance() { | 140 static PrefMapping* GetInstance() { |
| 167 return Singleton<PrefMapping>::get(); | 141 return Singleton<PrefMapping>::get(); |
| 168 } | 142 } |
| 169 | 143 |
| 170 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, | 144 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, |
| 171 std::string* browser_pref, | 145 std::string* browser_pref, |
| 172 ExtensionAPIPermission::ID* permission) { | 146 ExtensionAPIPermission::ID* permission) { |
| 173 PrefMap::iterator it = mapping_.find(extension_pref); | 147 PrefMap::iterator it = mapping_.find(extension_pref); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 PrefTransformerInterface* transformer = | 276 PrefTransformerInterface* transformer = |
| 303 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); | 277 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 304 dict->Set(keys::kValue, | 278 dict->Set(keys::kValue, |
| 305 transformer->BrowserToExtensionPref(pref->GetValue())); | 279 transformer->BrowserToExtensionPref(pref->GetValue())); |
| 306 if (incognito) { | 280 if (incognito) { |
| 307 ExtensionPrefs* ep = extension_service->extension_prefs(); | 281 ExtensionPrefs* ep = extension_service->extension_prefs(); |
| 308 dict->SetBoolean(keys::kIncognitoSpecific, | 282 dict->SetBoolean(keys::kIncognitoSpecific, |
| 309 ep->HasIncognitoPrefValue(browser_pref)); | 283 ep->HasIncognitoPrefValue(browser_pref)); |
| 310 } | 284 } |
| 311 | 285 |
| 312 ExtensionEventRouter* router = profile_->GetExtensionEventRouter(); | 286 helpers::DispatchEventToExtensions(profile_, |
| 313 if (!router || !router->HasEventListener(event_name)) | 287 event_name, |
| 314 return; | 288 &args, |
| 315 const ExtensionSet* extensions = extension_service->extensions(); | 289 permission, |
| 316 for (ExtensionSet::const_iterator it = extensions->begin(); | 290 incognito, |
| 317 it != extensions->end(); ++it) { | 291 browser_pref.c_str()); |
| 318 std::string extension_id = (*it)->id(); | |
| 319 // TODO(bauerb): Only iterate over registered event listeners. | |
| 320 if (router->ExtensionHasEventListener(extension_id, event_name) && | |
| 321 (*it)->HasAPIPermission(permission) && | |
| 322 (!incognito || extension_service->CanCrossIncognito(*it))) { | |
| 323 std::string level_of_control = | |
| 324 GetLevelOfControl(profile_, extension_id, browser_pref, incognito); | |
| 325 dict->SetString(keys::kLevelOfControl, level_of_control); | |
| 326 | |
| 327 std::string json_args; | |
| 328 base::JSONWriter::Write(&args, &json_args); | |
| 329 | |
| 330 DispatchEvent(extension_id, event_name, json_args); | |
| 331 } | |
| 332 } | |
| 333 } | |
| 334 | |
| 335 void ExtensionPreferenceEventRouter::DispatchEvent( | |
| 336 const std::string& extension_id, | |
| 337 const std::string& event_name, | |
| 338 const std::string& json_args) { | |
| 339 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | |
| 340 extension_id, event_name, json_args, NULL, GURL()); | |
| 341 } | 292 } |
| 342 | 293 |
| 343 PreferenceFunction::~PreferenceFunction() { } | 294 PreferenceFunction::~PreferenceFunction() { } |
| 344 | 295 |
| 345 bool PreferenceFunction::ValidateBrowserPref( | 296 bool PreferenceFunction::ValidateBrowserPref( |
| 346 const std::string& extension_pref_key, | 297 const std::string& extension_pref_key, |
| 347 std::string* browser_pref_key) { | 298 std::string* browser_pref_key) { |
| 348 ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; | 299 ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; |
| 349 EXTENSION_FUNCTION_VALIDATE( | 300 EXTENSION_FUNCTION_VALIDATE( |
| 350 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 301 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() | 334 PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() |
| 384 : profile_->GetPrefs(); | 335 : profile_->GetPrefs(); |
| 385 const PrefService::Preference* pref = | 336 const PrefService::Preference* pref = |
| 386 prefs->FindPreference(browser_pref.c_str()); | 337 prefs->FindPreference(browser_pref.c_str()); |
| 387 CHECK(pref); | 338 CHECK(pref); |
| 388 | 339 |
| 389 scoped_ptr<DictionaryValue> result(new DictionaryValue); | 340 scoped_ptr<DictionaryValue> result(new DictionaryValue); |
| 390 | 341 |
| 391 // Retrieve level of control. | 342 // Retrieve level of control. |
| 392 std::string level_of_control = | 343 std::string level_of_control = |
| 393 GetLevelOfControl(profile_, extension_id(), browser_pref, incognito); | 344 helpers::GetLevelOfControl(profile_, extension_id(), browser_pref, |
| 345 incognito); |
| 394 result->SetString(keys::kLevelOfControl, level_of_control); | 346 result->SetString(keys::kLevelOfControl, level_of_control); |
| 395 | 347 |
| 396 // Retrieve pref value. | 348 // Retrieve pref value. |
| 397 PrefTransformerInterface* transformer = | 349 PrefTransformerInterface* transformer = |
| 398 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); | 350 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 399 result->Set(keys::kValue, | 351 result->Set(keys::kValue, |
| 400 transformer->BrowserToExtensionPref(pref->GetValue())); | 352 transformer->BrowserToExtensionPref(pref->GetValue())); |
| 401 | 353 |
| 402 // Retrieve incognito status. | 354 // Retrieve incognito status. |
| 403 if (incognito) { | 355 if (incognito) { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 } | 469 } |
| 518 | 470 |
| 519 std::string browser_pref; | 471 std::string browser_pref; |
| 520 if (!ValidateBrowserPref(pref_key, &browser_pref)) | 472 if (!ValidateBrowserPref(pref_key, &browser_pref)) |
| 521 return false; | 473 return false; |
| 522 | 474 |
| 523 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 475 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 524 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); | 476 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); |
| 525 return true; | 477 return true; |
| 526 } | 478 } |
| OLD | NEW |