Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(709)

Side by Side Diff: chrome/browser/extensions/extension_settings_api.cc

Issue 8177022: Add onChanged events to the extension settings API, both from sync and between (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/values.h" 6 #include "base/values.h"
7 #include "chrome/browser/extensions/extension_service.h" 7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/extension_settings_api.h" 8 #include "chrome/browser/extensions/extension_settings_api.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "content/browser/browser_thread.h" 10 #include "content/browser/browser_thread.h"
11 11
12 namespace { 12 namespace {
13 const char* kUnsupportedArgumentType = "Unsupported argument type"; 13 const char* kUnsupportedArgumentType = "Unsupported argument type";
14 } // namespace 14 } // namespace
15 15
16 // SettingsFunction 16 // SettingsFunction
17 17
18 bool SettingsFunction::RunImpl() { 18 bool SettingsFunction::RunImpl() {
19 profile()->GetExtensionService()->extension_settings_frontend()-> 19 profile()->GetExtensionService()->extension_settings_frontend()->
20 RunWithBackend( 20 RunWithBackend(
21 base::Bind(&SettingsFunction::RunWithBackendOnFileThread, this)); 21 base::Bind(&SettingsFunction::RunWithBackendOnFileThread, this));
22 return true; 22 return true;
23 } 23 }
24 24
25 void SettingsFunction::RunWithBackendOnFileThread( 25 void SettingsFunction::RunWithBackendOnFileThread(
26 ExtensionSettingsBackend* backend) { 26 ExtensionSettingsBackend* backend) {
27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
28 bool success = RunWithStorage(backend->GetStorage(extension_id())); 28 bool success = RunWithStorage(backend, backend->GetStorage(extension_id()));
29 BrowserThread::PostTask( 29 BrowserThread::PostTask(
30 BrowserThread::UI, 30 BrowserThread::UI,
31 FROM_HERE, 31 FROM_HERE,
32 base::Bind(&SettingsFunction::SendResponse, this, success)); 32 base::Bind(&SettingsFunction::SendResponse, this, success));
33 } 33 }
34 34
35 bool SettingsFunction::UseResult( 35 bool SettingsFunction::UseResult(
36 ExtensionSettingsBackend* backend,
36 const ExtensionSettingsStorage::Result& storage_result) { 37 const ExtensionSettingsStorage::Result& storage_result) {
38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
37 if (storage_result.HasError()) { 39 if (storage_result.HasError()) {
38 error_ = storage_result.GetError(); 40 error_ = storage_result.GetError();
39 return false; 41 return false;
40 } 42 }
43
41 DictionaryValue* settings = storage_result.GetSettings(); 44 DictionaryValue* settings = storage_result.GetSettings();
42 result_.reset(settings == NULL ? NULL : settings->DeepCopy()); 45 result_.reset(settings == NULL ? NULL : settings->DeepCopy());
46
47 std::set<std::string>* changed_keys = storage_result.GetChangedKeys();
48 if (changed_keys && !changed_keys->empty()) {
49 ExtensionSettingsObserver::ChangeList change_event;
50 for (std::set<std::string>::iterator it = changed_keys->begin();
51 it != changed_keys->end(); ++it) {
52 Value* old_value = NULL;
53 if (storage_result.GetOldValue(*it, &old_value) && old_value) {
54 old_value = old_value->DeepCopy();
55 }
56 Value* new_value = NULL;
57 if (storage_result.GetNewValue(*it, &new_value) && new_value) {
58 new_value = new_value->DeepCopy();
59 }
60 change_event.AppendChange(*it, old_value, new_value);
61 }
62 backend->TriggerOnSettingsChanged(
Matt Perry 2011/10/07 22:39:52 It seems weird to leave it up to the consumer of E
not at google - send to devlin 2011/10/10 01:00:16 I figured it wasn't so bad since there's only 2 so
Matt Perry 2011/10/10 20:37:15 After thinking about it more, having the notificat
63 profile(), extension_id(), change_event.GetEventJson());
64 }
65
43 return true; 66 return true;
44 } 67 }
45 68
46 // Concrete settings functions 69 // Concrete settings functions
47 70
48 // Adds all StringValues from a ListValue to a vector of strings. 71 // Adds all StringValues from a ListValue to a vector of strings.
49 static void AddAllStringValues( 72 static void AddAllStringValues(
50 const ListValue& from, std::vector<std::string>* to) { 73 const ListValue& from, std::vector<std::string>* to) {
51 DCHECK(to->empty()); 74 DCHECK(to->empty());
52 std::string as_string; 75 std::string as_string;
53 for (ListValue::const_iterator it = from.begin(); it != from.end(); ++it) { 76 for (ListValue::const_iterator it = from.begin(); it != from.end(); ++it) {
54 if ((*it)->GetAsString(&as_string)) { 77 if ((*it)->GetAsString(&as_string)) {
55 to->push_back(as_string); 78 to->push_back(as_string);
56 } 79 }
57 } 80 }
58 } 81 }
59 82
60 bool GetSettingsFunction::RunWithStorage( 83 bool GetSettingsFunction::RunWithStorage(
84 ExtensionSettingsBackend* backend,
61 ExtensionSettingsStorage* storage) { 85 ExtensionSettingsStorage* storage) {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
62 Value *input; 87 Value *input;
63 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); 88 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input));
64 std::string as_string; 89 std::string as_string;
65 ListValue* as_list; 90 ListValue* as_list;
66 if (input->IsType(Value::TYPE_NULL)) { 91 if (input->IsType(Value::TYPE_NULL)) {
67 return UseResult(storage->Get()); 92 return UseResult(backend, storage->Get());
68 } else if (input->GetAsString(&as_string)) { 93 } else if (input->GetAsString(&as_string)) {
69 return UseResult(storage->Get(as_string)); 94 return UseResult(backend, storage->Get(as_string));
70 } else if (input->GetAsList(&as_list)) { 95 } else if (input->GetAsList(&as_list)) {
71 std::vector<std::string> string_list; 96 std::vector<std::string> string_list;
72 AddAllStringValues(*as_list, &string_list); 97 AddAllStringValues(*as_list, &string_list);
73 return UseResult(storage->Get(string_list)); 98 return UseResult(backend, storage->Get(string_list));
74 } 99 }
75 return UseResult(ExtensionSettingsStorage::Result(kUnsupportedArgumentType)); 100 return UseResult(
101 backend, ExtensionSettingsStorage::Result(kUnsupportedArgumentType));
76 } 102 }
77 103
78 bool SetSettingsFunction::RunWithStorage( 104 bool SetSettingsFunction::RunWithStorage(
105 ExtensionSettingsBackend* backend,
79 ExtensionSettingsStorage* storage) { 106 ExtensionSettingsStorage* storage) {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
80 DictionaryValue *input; 108 DictionaryValue *input;
81 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); 109 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input));
82 return UseResult(storage->Set(*input)); 110 return UseResult(backend, storage->Set(*input));
83 } 111 }
84 112
85 bool RemoveSettingsFunction::RunWithStorage( 113 bool RemoveSettingsFunction::RunWithStorage(
114 ExtensionSettingsBackend* backend,
86 ExtensionSettingsStorage* storage) { 115 ExtensionSettingsStorage* storage) {
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
87 Value *input; 117 Value *input;
88 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); 118 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input));
89 std::string as_string; 119 std::string as_string;
90 ListValue* as_list; 120 ListValue* as_list;
91 if (input->GetAsString(&as_string)) { 121 if (input->GetAsString(&as_string)) {
92 return UseResult(storage->Remove(as_string)); 122 return UseResult(backend, storage->Remove(as_string));
93 } else if (input->GetAsList(&as_list)) { 123 } else if (input->GetAsList(&as_list)) {
94 std::vector<std::string> string_list; 124 std::vector<std::string> string_list;
95 AddAllStringValues(*as_list, &string_list); 125 AddAllStringValues(*as_list, &string_list);
96 return UseResult(storage->Remove(string_list)); 126 return UseResult(backend, storage->Remove(string_list));
97 } 127 }
98 return UseResult(ExtensionSettingsStorage::Result(kUnsupportedArgumentType)); 128 return UseResult(
129 backend, ExtensionSettingsStorage::Result(kUnsupportedArgumentType));
99 } 130 }
100 131
101 bool ClearSettingsFunction::RunWithStorage( 132 bool ClearSettingsFunction::RunWithStorage(
133 ExtensionSettingsBackend* backend,
102 ExtensionSettingsStorage* storage) { 134 ExtensionSettingsStorage* storage) {
103 return UseResult(storage->Clear()); 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
136 return UseResult(backend, storage->Clear());
104 } 137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698