| 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/extensions/extension_preference_api.h" | 
|  | 6 | 
|  | 7 #include "base/singleton.h" | 
|  | 8 #include "base/stringprintf.h" | 
|  | 9 #include "base/values.h" | 
|  | 10 #include "chrome/browser/extensions/extension_prefs.h" | 
|  | 11 #include "chrome/browser/extensions/extension_service.h" | 
|  | 12 #include "chrome/browser/profiles/profile.h" | 
|  | 13 #include "chrome/common/pref_names.h" | 
|  | 14 | 
|  | 15 namespace { | 
|  | 16 | 
|  | 17 struct PrefMappingEntry { | 
|  | 18   const char* extension_pref; | 
|  | 19   const char* browser_pref; | 
|  | 20   const char* permission; | 
|  | 21 }; | 
|  | 22 | 
|  | 23 PrefMappingEntry pref_mapping[] = { | 
|  | 24   { "blockThirdPartyCookies", | 
|  | 25     prefs::kBlockThirdPartyCookies, | 
|  | 26     Extension::kContentSettingsPermission | 
|  | 27   }, | 
|  | 28 }; | 
|  | 29 | 
|  | 30 class PrefMapping { | 
|  | 31  public: | 
|  | 32   static PrefMapping* GetInstance() { | 
|  | 33     return Singleton<PrefMapping>::get(); | 
|  | 34   } | 
|  | 35 | 
|  | 36   bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, | 
|  | 37                                        std::string* browser_pref, | 
|  | 38                                        std::string* permission) { | 
|  | 39     std::map<std::string, std::pair<std::string, std::string> >::iterator it = | 
|  | 40         mapping_.find(extension_pref); | 
|  | 41     if (it != mapping_.end()) { | 
|  | 42       *browser_pref = it->second.first; | 
|  | 43       *permission = it->second.second; | 
|  | 44       return true; | 
|  | 45     } | 
|  | 46     return false; | 
|  | 47   } | 
|  | 48 | 
|  | 49  private: | 
|  | 50   friend struct DefaultSingletonTraits<PrefMapping>; | 
|  | 51 | 
|  | 52   std::map<std::string, std::pair<std::string, std::string> > mapping_; | 
|  | 53 | 
|  | 54   PrefMapping() { | 
|  | 55     for (size_t i = 0; i < arraysize(pref_mapping); ++i) { | 
|  | 56       mapping_[pref_mapping[i].extension_pref] = | 
|  | 57           std::make_pair(pref_mapping[i].browser_pref, | 
|  | 58                          pref_mapping[i].permission); | 
|  | 59     } | 
|  | 60   } | 
|  | 61 }; | 
|  | 62 | 
|  | 63 const char kPermissionErrorMessage[] = | 
|  | 64     "You do not have permission to access the preference '%s'. " | 
|  | 65     "Be sure to declare in your manifest what permissions you need."; | 
|  | 66 | 
|  | 67 }  // namespace | 
|  | 68 | 
|  | 69 GetPreferenceFunction::~GetPreferenceFunction() { } | 
|  | 70 | 
|  | 71 bool GetPreferenceFunction::RunImpl() { | 
|  | 72   std::string pref_key; | 
|  | 73   EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 
|  | 74   DictionaryValue* details = NULL; | 
|  | 75   EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 
|  | 76 | 
|  | 77   bool incognito = false; | 
|  | 78   if (details->HasKey("incognito")) | 
|  | 79     EXTENSION_FUNCTION_VALIDATE(details->GetBoolean("incognito", &incognito)); | 
|  | 80 | 
|  | 81   PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() | 
|  | 82                                  : profile_->GetPrefs(); | 
|  | 83   std::string browser_pref; | 
|  | 84   std::string permission; | 
|  | 85   EXTENSION_FUNCTION_VALIDATE( | 
|  | 86       PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 
|  | 87           pref_key, &browser_pref, &permission)); | 
|  | 88   if (!GetExtension()->HasApiPermission(permission)) { | 
|  | 89     error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 
|  | 90     return false; | 
|  | 91   } | 
|  | 92   const PrefService::Preference* pref = | 
|  | 93       prefs->FindPreference(browser_pref.c_str()); | 
|  | 94   CHECK(pref); | 
|  | 95   result_.reset(pref->GetValue()->DeepCopy()); | 
|  | 96   return true; | 
|  | 97 } | 
|  | 98 | 
|  | 99 SetPreferenceFunction::~SetPreferenceFunction() { } | 
|  | 100 | 
|  | 101 bool SetPreferenceFunction::RunImpl() { | 
|  | 102   std::string pref_key; | 
|  | 103   EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 
|  | 104   DictionaryValue* details = NULL; | 
|  | 105   EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 
|  | 106 | 
|  | 107   Value* value = NULL; | 
|  | 108   EXTENSION_FUNCTION_VALIDATE(details->Get("value", &value)); | 
|  | 109 | 
|  | 110   bool incognito = false; | 
|  | 111   if (details->HasKey("incognito")) | 
|  | 112     EXTENSION_FUNCTION_VALIDATE(details->GetBoolean("incognito", &incognito)); | 
|  | 113 | 
|  | 114   std::string browser_pref; | 
|  | 115   std::string permission; | 
|  | 116   EXTENSION_FUNCTION_VALIDATE( | 
|  | 117       PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 
|  | 118           pref_key, &browser_pref, &permission)); | 
|  | 119   if (!GetExtension()->HasApiPermission(permission)) { | 
|  | 120     error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 
|  | 121     return false; | 
|  | 122   } | 
|  | 123   ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 
|  | 124   const PrefService::Preference* pref = | 
|  | 125       prefs->pref_service()->FindPreference(browser_pref.c_str()); | 
|  | 126   CHECK(pref); | 
|  | 127   EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); | 
|  | 128   prefs->SetExtensionControlledPref(extension_id(), | 
|  | 129                                     browser_pref, | 
|  | 130                                     incognito, | 
|  | 131                                     value->DeepCopy()); | 
|  | 132   return true; | 
|  | 133 } | 
| OLD | NEW | 
|---|