| 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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 return; | 288 return; |
| 315 const ExtensionSet* extensions = extension_service->extensions(); | 289 const ExtensionSet* extensions = extension_service->extensions(); |
| 316 for (ExtensionSet::const_iterator it = extensions->begin(); | 290 for (ExtensionSet::const_iterator it = extensions->begin(); |
| 317 it != extensions->end(); ++it) { | 291 it != extensions->end(); ++it) { |
| 318 std::string extension_id = (*it)->id(); | 292 std::string extension_id = (*it)->id(); |
| 319 // TODO(bauerb): Only iterate over registered event listeners. | 293 // TODO(bauerb): Only iterate over registered event listeners. |
| 320 if (router->ExtensionHasEventListener(extension_id, event_name) && | 294 if (router->ExtensionHasEventListener(extension_id, event_name) && |
| 321 (*it)->HasAPIPermission(permission) && | 295 (*it)->HasAPIPermission(permission) && |
| 322 (!incognito || extension_service->CanCrossIncognito(*it))) { | 296 (!incognito || extension_service->CanCrossIncognito(*it))) { |
| 323 std::string level_of_control = | 297 std::string level_of_control = |
| 324 GetLevelOfControl(profile_, extension_id, browser_pref, incognito); | 298 helpers::GetLevelOfControl(profile_, extension_id, browser_pref, |
| 299 incognito); |
| 325 dict->SetString(keys::kLevelOfControl, level_of_control); | 300 dict->SetString(keys::kLevelOfControl, level_of_control); |
| 326 | 301 |
| 327 std::string json_args; | 302 std::string json_args; |
| 328 base::JSONWriter::Write(&args, &json_args); | 303 base::JSONWriter::Write(&args, &json_args); |
| 329 | 304 |
| 330 DispatchEvent(extension_id, event_name, json_args); | 305 DispatchEvent(extension_id, event_name, json_args); |
| 331 } | 306 } |
| 332 } | 307 } |
| 333 } | 308 } |
| 334 | 309 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() | 358 PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() |
| 384 : profile_->GetPrefs(); | 359 : profile_->GetPrefs(); |
| 385 const PrefService::Preference* pref = | 360 const PrefService::Preference* pref = |
| 386 prefs->FindPreference(browser_pref.c_str()); | 361 prefs->FindPreference(browser_pref.c_str()); |
| 387 CHECK(pref); | 362 CHECK(pref); |
| 388 | 363 |
| 389 scoped_ptr<DictionaryValue> result(new DictionaryValue); | 364 scoped_ptr<DictionaryValue> result(new DictionaryValue); |
| 390 | 365 |
| 391 // Retrieve level of control. | 366 // Retrieve level of control. |
| 392 std::string level_of_control = | 367 std::string level_of_control = |
| 393 GetLevelOfControl(profile_, extension_id(), browser_pref, incognito); | 368 helpers::GetLevelOfControl(profile_, extension_id(), browser_pref, |
| 369 incognito); |
| 394 result->SetString(keys::kLevelOfControl, level_of_control); | 370 result->SetString(keys::kLevelOfControl, level_of_control); |
| 395 | 371 |
| 396 // Retrieve pref value. | 372 // Retrieve pref value. |
| 397 PrefTransformerInterface* transformer = | 373 PrefTransformerInterface* transformer = |
| 398 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); | 374 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
| 399 result->Set(keys::kValue, | 375 result->Set(keys::kValue, |
| 400 transformer->BrowserToExtensionPref(pref->GetValue())); | 376 transformer->BrowserToExtensionPref(pref->GetValue())); |
| 401 | 377 |
| 402 // Retrieve incognito status. | 378 // Retrieve incognito status. |
| 403 if (incognito) { | 379 if (incognito) { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 } | 493 } |
| 518 | 494 |
| 519 std::string browser_pref; | 495 std::string browser_pref; |
| 520 if (!ValidateBrowserPref(pref_key, &browser_pref)) | 496 if (!ValidateBrowserPref(pref_key, &browser_pref)) |
| 521 return false; | 497 return false; |
| 522 | 498 |
| 523 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 499 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 524 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); | 500 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); |
| 525 return true; | 501 return true; |
| 526 } | 502 } |
| OLD | NEW |