1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/bind.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_settings_api.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace {
const char* kUnsupportedArgumentType = "Unsupported argument type";
} // namespace
// SettingsFunction
bool SettingsFunction::RunImpl() {
ExtensionSettingsFrontend* frontend =
profile()->GetExtensionService()->extension_settings_frontend();
frontend->RunWithStorage(
extension_id(),
base::Bind(
&SettingsFunction::RunWithStorageOnFileThread,
this,
frontend->GetObservers()));
return true;
}
void SettingsFunction::RunWithStorageOnFileThread(
scoped_refptr<ExtensionSettingsObserverList> observers,
ExtensionSettingsStorage* storage) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
bool success = false;
if (storage) {
success = RunWithStorage(observers.get(), storage);
}
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&SettingsFunction::SendResponse, this, success));
}
bool SettingsFunction::UseResult(
scoped_refptr<ExtensionSettingsObserverList> observers,
const ExtensionSettingsStorage::Result& storage_result) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
if (storage_result.HasError()) {
error_ = storage_result.GetError();
return false;
}
const DictionaryValue* settings = storage_result.GetSettings();
if (settings) {
result_.reset(settings->DeepCopy());
}
const std::set<std::string>* changed_keys = storage_result.GetChangedKeys();
if (changed_keys && !changed_keys->empty()) {
ExtensionSettingChanges::Builder changes;
for (std::set<std::string>::const_iterator it = changed_keys->begin();
it != changed_keys->end(); ++it) {
const Value* old_value = storage_result.GetOldValue(*it);
const Value* new_value = storage_result.GetNewValue(*it);
changes.AppendChange(
*it,
old_value ? old_value->DeepCopy() : NULL,
new_value ? new_value->DeepCopy() : NULL);
}
observers->Notify(
&ExtensionSettingsObserver::OnSettingsChanged,
profile(),
extension_id(),
changes.Build());
}
return true;
}
// Concrete settings functions
// Adds all StringValues from a ListValue to a vector of strings.
static void AddAllStringValues(
const ListValue& from, std::vector<std::string>* to) {
DCHECK(to->empty());
std::string as_string;
for (ListValue::const_iterator it = from.begin(); it != from.end(); ++it) {
if ((*it)->GetAsString(&as_string)) {
to->push_back(as_string);
}
}
}
bool GetSettingsFunction::RunWithStorage(
scoped_refptr<ExtensionSettingsObserverList> observers,
ExtensionSettingsStorage* storage) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
Value *input;
EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input));
std::string as_string;
ListValue* as_list;
if (input->IsType(Value::TYPE_NULL)) {
return UseResult(observers, storage->Get());
} else if (input->GetAsString(&as_string)) {
return UseResult(observers, storage->Get(as_string));
} else if (input->GetAsList(&as_list)) {
std::vector<std::string> string_list;
AddAllStringValues(*as_list, &string_list);
return UseResult(observers, storage->Get(string_list));
}
return UseResult(
observers, ExtensionSettingsStorage::Result(kUnsupportedArgumentType));
}
bool SetSettingsFunction::RunWithStorage(
scoped_refptr<ExtensionSettingsObserverList> observers,
ExtensionSettingsStorage* storage) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
DictionaryValue *input;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input));
return UseResult(observers, storage->Set(*input));
}
bool RemoveSettingsFunction::RunWithStorage(
scoped_refptr<ExtensionSettingsObserverList> observers,
ExtensionSettingsStorage* storage) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
Value *input;
EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input));
std::string as_string;
ListValue* as_list;
if (input->GetAsString(&as_string)) {
return UseResult(observers, storage->Remove(as_string));
} else if (input->GetAsList(&as_list)) {
std::vector<std::string> string_list;
AddAllStringValues(*as_list, &string_list);
return UseResult(observers, storage->Remove(string_list));
}
return UseResult(
observers, ExtensionSettingsStorage::Result(kUnsupportedArgumentType));
}
bool ClearSettingsFunction::RunWithStorage(
scoped_refptr<ExtensionSettingsObserverList> observers,
ExtensionSettingsStorage* storage) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
return UseResult(observers, storage->Clear());
}
|