| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/settings/settings_api.h" | 5 #include "chrome/browser/extensions/settings/settings_api.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extensions_quota_service.h" | 10 #include "chrome/browser/extensions/extensions_quota_service.h" |
| 11 #include "chrome/browser/extensions/settings/settings_api.h" | 11 #include "chrome/browser/extensions/settings/settings_api.h" |
| 12 #include "chrome/browser/extensions/settings/settings_frontend.h" | 12 #include "chrome/browser/extensions/settings/settings_frontend.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 | 15 |
| 16 namespace extensions { | 16 namespace extensions { |
| 17 | 17 |
| 18 using content::BrowserThread; | 18 using content::BrowserThread; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 const char* kUnsupportedArgumentType = "Unsupported argument type"; | 21 const char* kUnsupportedArgumentType = "Unsupported argument type"; |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 // SettingsFunction | 24 // SettingsFunction |
| 25 | 25 |
| 26 SettingsFunction::SettingsFunction() |
| 27 : settings_namespace_(settings_namespace::INVALID) {} |
| 28 |
| 29 SettingsFunction::~SettingsFunction() {} |
| 30 |
| 26 bool SettingsFunction::RunImpl() { | 31 bool SettingsFunction::RunImpl() { |
| 32 { |
| 33 std::string settings_namespace_string; |
| 34 EXTENSION_FUNCTION_VALIDATE( |
| 35 args_->GetString(0, &settings_namespace_string)); |
| 36 args_->Remove(0, NULL); |
| 37 settings_namespace_ = |
| 38 settings_namespace::StringToNamespace(settings_namespace_string); |
| 39 EXTENSION_FUNCTION_VALIDATE( |
| 40 settings_namespace_ != settings_namespace::INVALID); |
| 41 } |
| 42 |
| 27 SettingsFrontend* frontend = | 43 SettingsFrontend* frontend = |
| 28 profile()->GetExtensionService()->settings_frontend(); | 44 profile()->GetExtensionService()->settings_frontend(); |
| 45 observers_ = frontend->GetObservers(); |
| 29 frontend->RunWithStorage( | 46 frontend->RunWithStorage( |
| 30 extension_id(), | 47 extension_id(), |
| 31 base::Bind( | 48 settings_namespace_, |
| 32 &SettingsFunction::RunWithStorageOnFileThread, | 49 base::Bind(&SettingsFunction::RunWithStorageOnFileThread, this)); |
| 33 this, | |
| 34 frontend->GetObservers())); | |
| 35 return true; | 50 return true; |
| 36 } | 51 } |
| 37 | 52 |
| 38 void SettingsFunction::RunWithStorageOnFileThread( | 53 void SettingsFunction::RunWithStorageOnFileThread(SettingsStorage* storage) { |
| 39 scoped_refptr<SettingsObserverList> observers, | |
| 40 SettingsStorage* storage) { | |
| 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 42 bool success = RunWithStorage(observers.get(), storage); | 55 bool success = RunWithStorage(storage); |
| 43 BrowserThread::PostTask( | 56 BrowserThread::PostTask( |
| 44 BrowserThread::UI, | 57 BrowserThread::UI, |
| 45 FROM_HERE, | 58 FROM_HERE, |
| 46 base::Bind(&SettingsFunction::SendResponse, this, success)); | 59 base::Bind(&SettingsFunction::SendResponse, this, success)); |
| 47 } | 60 } |
| 48 | 61 |
| 49 bool SettingsFunction::UseReadResult( | 62 bool SettingsFunction::UseReadResult( |
| 50 const SettingsStorage::ReadResult& result) { | 63 const SettingsStorage::ReadResult& result) { |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 52 if (result.HasError()) { | 65 if (result.HasError()) { |
| 53 error_ = result.error(); | 66 error_ = result.error(); |
| 54 return false; | 67 return false; |
| 55 } | 68 } |
| 56 | 69 |
| 57 result_.reset(result.settings().DeepCopy()); | 70 result_.reset(result.settings().DeepCopy()); |
| 58 return true; | 71 return true; |
| 59 } | 72 } |
| 60 | 73 |
| 61 bool SettingsFunction::UseWriteResult( | 74 bool SettingsFunction::UseWriteResult( |
| 62 scoped_refptr<SettingsObserverList> observers, | |
| 63 const SettingsStorage::WriteResult& result) { | 75 const SettingsStorage::WriteResult& result) { |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 65 if (result.HasError()) { | 77 if (result.HasError()) { |
| 66 error_ = result.error(); | 78 error_ = result.error(); |
| 67 return false; | 79 return false; |
| 68 } | 80 } |
| 69 | 81 |
| 70 if (!result.changes().empty()) { | 82 if (!result.changes().empty()) { |
| 71 observers->Notify( | 83 observers_->Notify( |
| 72 &SettingsObserver::OnSettingsChanged, | 84 &SettingsObserver::OnSettingsChanged, |
| 73 extension_id(), | 85 extension_id(), |
| 86 settings_namespace_, |
| 74 SettingChange::GetEventJson(result.changes())); | 87 SettingChange::GetEventJson(result.changes())); |
| 75 } | 88 } |
| 76 | 89 |
| 77 return true; | 90 return true; |
| 78 } | 91 } |
| 79 | 92 |
| 80 // Concrete settings functions | 93 // Concrete settings functions |
| 81 | 94 |
| 82 namespace { | 95 namespace { |
| 83 | 96 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 }; | 134 }; |
| 122 heuristics->push_back( | 135 heuristics->push_back( |
| 123 new ExtensionsQuotaService::SustainedLimit( | 136 new ExtensionsQuotaService::SustainedLimit( |
| 124 base::TimeDelta::FromMinutes(10), | 137 base::TimeDelta::FromMinutes(10), |
| 125 shortLimitConfig, | 138 shortLimitConfig, |
| 126 new QuotaLimitHeuristic::SingletonBucketMapper())); | 139 new QuotaLimitHeuristic::SingletonBucketMapper())); |
| 127 }; | 140 }; |
| 128 | 141 |
| 129 } // namespace | 142 } // namespace |
| 130 | 143 |
| 131 bool GetSettingsFunction::RunWithStorage( | 144 bool GetSettingsFunction::RunWithStorage(SettingsStorage* storage) { |
| 132 scoped_refptr<SettingsObserverList> observers, | |
| 133 SettingsStorage* storage) { | |
| 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 135 Value *input; | 146 Value *input; |
| 136 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 147 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 137 | 148 |
| 138 switch (input->GetType()) { | 149 switch (input->GetType()) { |
| 139 case Value::TYPE_NULL: | 150 case Value::TYPE_NULL: |
| 140 return UseReadResult(storage->Get()); | 151 return UseReadResult(storage->Get()); |
| 141 | 152 |
| 142 case Value::TYPE_STRING: { | 153 case Value::TYPE_STRING: { |
| 143 std::string as_string; | 154 std::string as_string; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 164 return UseReadResult( | 175 return UseReadResult( |
| 165 SettingsStorage::ReadResult(with_default_values)); | 176 SettingsStorage::ReadResult(with_default_values)); |
| 166 } | 177 } |
| 167 | 178 |
| 168 default: | 179 default: |
| 169 return UseReadResult( | 180 return UseReadResult( |
| 170 SettingsStorage::ReadResult(kUnsupportedArgumentType)); | 181 SettingsStorage::ReadResult(kUnsupportedArgumentType)); |
| 171 } | 182 } |
| 172 } | 183 } |
| 173 | 184 |
| 174 bool SetSettingsFunction::RunWithStorage( | 185 bool SetSettingsFunction::RunWithStorage(SettingsStorage* storage) { |
| 175 scoped_refptr<SettingsObserverList> observers, | |
| 176 SettingsStorage* storage) { | |
| 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 178 DictionaryValue *input; | 187 DictionaryValue* input; |
| 179 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); | 188 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); |
| 180 return UseWriteResult( | 189 return UseWriteResult(storage->Set(SettingsStorage::DEFAULTS, *input)); |
| 181 observers, storage->Set(SettingsStorage::DEFAULTS, *input)); | |
| 182 } | 190 } |
| 183 | 191 |
| 184 void SetSettingsFunction::GetQuotaLimitHeuristics( | 192 void SetSettingsFunction::GetQuotaLimitHeuristics( |
| 185 QuotaLimitHeuristics* heuristics) const { | 193 QuotaLimitHeuristics* heuristics) const { |
| 186 GetModificationQuotaLimitHeuristics(heuristics); | 194 GetModificationQuotaLimitHeuristics(heuristics); |
| 187 } | 195 } |
| 188 | 196 |
| 189 bool RemoveSettingsFunction::RunWithStorage( | 197 bool RemoveSettingsFunction::RunWithStorage(SettingsStorage* storage) { |
| 190 scoped_refptr<SettingsObserverList> observers, | |
| 191 SettingsStorage* storage) { | |
| 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 193 Value *input; | 199 Value *input; |
| 194 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 200 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 195 | 201 |
| 196 switch (input->GetType()) { | 202 switch (input->GetType()) { |
| 197 case Value::TYPE_STRING: { | 203 case Value::TYPE_STRING: { |
| 198 std::string as_string; | 204 std::string as_string; |
| 199 input->GetAsString(&as_string); | 205 input->GetAsString(&as_string); |
| 200 return UseWriteResult(observers, storage->Remove(as_string)); | 206 return UseWriteResult(storage->Remove(as_string)); |
| 201 } | 207 } |
| 202 | 208 |
| 203 case Value::TYPE_LIST: { | 209 case Value::TYPE_LIST: { |
| 204 std::vector<std::string> as_string_list; | 210 std::vector<std::string> as_string_list; |
| 205 AddAllStringValues(*static_cast<ListValue*>(input), &as_string_list); | 211 AddAllStringValues(*static_cast<ListValue*>(input), &as_string_list); |
| 206 return UseWriteResult(observers, storage->Remove(as_string_list)); | 212 return UseWriteResult(storage->Remove(as_string_list)); |
| 207 } | 213 } |
| 208 | 214 |
| 209 default: | 215 default: |
| 210 return UseWriteResult( | 216 return UseWriteResult( |
| 211 observers, | |
| 212 SettingsStorage::WriteResult(kUnsupportedArgumentType)); | 217 SettingsStorage::WriteResult(kUnsupportedArgumentType)); |
| 213 }; | 218 }; |
| 214 } | 219 } |
| 215 | 220 |
| 216 void RemoveSettingsFunction::GetQuotaLimitHeuristics( | 221 void RemoveSettingsFunction::GetQuotaLimitHeuristics( |
| 217 QuotaLimitHeuristics* heuristics) const { | 222 QuotaLimitHeuristics* heuristics) const { |
| 218 GetModificationQuotaLimitHeuristics(heuristics); | 223 GetModificationQuotaLimitHeuristics(heuristics); |
| 219 } | 224 } |
| 220 | 225 |
| 221 bool ClearSettingsFunction::RunWithStorage( | 226 bool ClearSettingsFunction::RunWithStorage(SettingsStorage* storage) { |
| 222 scoped_refptr<SettingsObserverList> observers, | |
| 223 SettingsStorage* storage) { | |
| 224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 225 return UseWriteResult(observers, storage->Clear()); | 228 return UseWriteResult(storage->Clear()); |
| 226 } | 229 } |
| 227 | 230 |
| 228 void ClearSettingsFunction::GetQuotaLimitHeuristics( | 231 void ClearSettingsFunction::GetQuotaLimitHeuristics( |
| 229 QuotaLimitHeuristics* heuristics) const { | 232 QuotaLimitHeuristics* heuristics) const { |
| 230 GetModificationQuotaLimitHeuristics(heuristics); | 233 GetModificationQuotaLimitHeuristics(heuristics); |
| 231 } | 234 } |
| 232 | 235 |
| 233 } // namespace extensions | 236 } // namespace extensions |
| OLD | NEW |