Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / extensions / browser / api / declarative / declarative_api.cc
blobf88bbe8ce59dbb3669b6f1dadf95b0d67bace121
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
3 // found in the LICENSE file.
5 #include "extensions/browser/api/declarative/declarative_api.h"
7 #include "base/base64.h"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/task_runner_util.h"
11 #include "base/values.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "extensions/browser/api/declarative/rules_registry_service.h"
17 #include "extensions/browser/api/extensions_api_client.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/guest_view/web_view/web_view_constants.h"
20 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
21 #include "extensions/common/api/events.h"
22 #include "extensions/common/extension_api.h"
23 #include "extensions/common/permissions/permissions_data.h"
25 using extensions::core_api::events::Rule;
27 namespace AddRules = extensions::core_api::events::Event::AddRules;
28 namespace GetRules = extensions::core_api::events::Event::GetRules;
29 namespace RemoveRules = extensions::core_api::events::Event::RemoveRules;
32 namespace extensions {
34 namespace {
36 const char kDeclarativeEventPrefix[] = "declarative";
38 void ConvertBinaryDictionaryValuesToBase64(base::DictionaryValue* dict);
40 // Encodes |binary| as base64 and returns a new StringValue populated with the
41 // encoded string.
42 scoped_ptr<base::StringValue> ConvertBinaryToBase64(base::BinaryValue* binary) {
43 std::string binary_data = std::string(binary->GetBuffer(), binary->GetSize());
44 std::string data64;
45 base::Base64Encode(binary_data, &data64);
46 return scoped_ptr<base::StringValue>(new base::StringValue(data64));
49 // Parses through |args| replacing any BinaryValues with base64 encoded
50 // StringValues. Recurses over any nested ListValues, and calls
51 // ConvertBinaryDictionaryValuesToBase64 for any nested DictionaryValues.
52 void ConvertBinaryListElementsToBase64(base::ListValue* args) {
53 size_t index = 0;
54 for (base::ListValue::iterator iter = args->begin(); iter != args->end();
55 ++iter, ++index) {
56 if ((*iter)->IsType(base::Value::TYPE_BINARY)) {
57 base::BinaryValue* binary = NULL;
58 if (args->GetBinary(index, &binary))
59 args->Set(index, ConvertBinaryToBase64(binary).release());
60 } else if ((*iter)->IsType(base::Value::TYPE_LIST)) {
61 base::ListValue* list;
62 (*iter)->GetAsList(&list);
63 ConvertBinaryListElementsToBase64(list);
64 } else if ((*iter)->IsType(base::Value::TYPE_DICTIONARY)) {
65 base::DictionaryValue* dict;
66 (*iter)->GetAsDictionary(&dict);
67 ConvertBinaryDictionaryValuesToBase64(dict);
72 // Parses through |dict| replacing any BinaryValues with base64 encoded
73 // StringValues. Recurses over any nested DictionaryValues, and calls
74 // ConvertBinaryListElementsToBase64 for any nested ListValues.
75 void ConvertBinaryDictionaryValuesToBase64(base::DictionaryValue* dict) {
76 for (base::DictionaryValue::Iterator iter(*dict); !iter.IsAtEnd();
77 iter.Advance()) {
78 if (iter.value().IsType(base::Value::TYPE_BINARY)) {
79 base::BinaryValue* binary = NULL;
80 if (dict->GetBinary(iter.key(), &binary))
81 dict->Set(iter.key(), ConvertBinaryToBase64(binary).release());
82 } else if (iter.value().IsType(base::Value::TYPE_LIST)) {
83 const base::ListValue* list;
84 iter.value().GetAsList(&list);
85 ConvertBinaryListElementsToBase64(const_cast<base::ListValue*>(list));
86 } else if (iter.value().IsType(base::Value::TYPE_DICTIONARY)) {
87 const base::DictionaryValue* dict;
88 iter.value().GetAsDictionary(&dict);
89 ConvertBinaryDictionaryValuesToBase64(
90 const_cast<base::DictionaryValue*>(dict));
95 } // namespace
97 RulesFunction::RulesFunction()
98 : rules_registry_(NULL) {
101 RulesFunction::~RulesFunction() {}
103 bool RulesFunction::HasPermission() {
104 std::string event_name;
105 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
106 int web_view_instance_id = 0;
107 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &web_view_instance_id));
109 // <webview> embedders use the declarativeWebRequest API via
110 // <webview>.onRequest.
111 if (web_view_instance_id != 0 &&
112 extension_->permissions_data()->HasAPIPermission(
113 extensions::APIPermission::kWebView))
114 return true;
115 return ExtensionFunction::HasPermission();
118 bool RulesFunction::RunAsync() {
119 std::string event_name;
120 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
122 int web_view_instance_id = 0;
123 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &web_view_instance_id));
124 int embedder_process_id = render_view_host()->GetProcess()->GetID();
126 bool from_web_view = web_view_instance_id != 0;
127 // If we are not operating on a particular <webview>, then the key is 0.
128 int rules_registry_id = RulesRegistryService::kDefaultRulesRegistryID;
129 if (from_web_view) {
130 // Sample event names:
131 // webViewInternal.declarativeWebRequest.onRequest.
132 // webViewInternal.declarativeWebRequest.onMessage.
133 // The "webViewInternal." prefix is removed from the event name.
134 std::size_t found = event_name.find(kDeclarativeEventPrefix);
135 EXTENSION_FUNCTION_VALIDATE(found != std::string::npos);
136 event_name = event_name.substr(found);
138 rules_registry_id = WebViewGuest::GetOrGenerateRulesRegistryID(
139 embedder_process_id, web_view_instance_id);
142 // The following call will return a NULL pointer for apps_shell, but should
143 // never be called there anyways.
144 rules_registry_ = RulesRegistryService::Get(browser_context())->
145 GetRulesRegistry(rules_registry_id, event_name);
146 DCHECK(rules_registry_.get());
147 // Raw access to this function is not available to extensions, therefore
148 // there should never be a request for a nonexisting rules registry.
149 EXTENSION_FUNCTION_VALIDATE(rules_registry_.get());
151 if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) {
152 bool success = RunAsyncOnCorrectThread();
153 SendResponse(success);
154 } else {
155 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
156 content::BrowserThread::GetMessageLoopProxyForThread(
157 rules_registry_->owner_thread());
158 base::PostTaskAndReplyWithResult(
159 message_loop_proxy.get(),
160 FROM_HERE,
161 base::Bind(&RulesFunction::RunAsyncOnCorrectThread, this),
162 base::Bind(&RulesFunction::SendResponse, this));
165 return true;
168 bool EventsEventAddRulesFunction::RunAsyncOnCorrectThread() {
169 ConvertBinaryListElementsToBase64(args_.get());
170 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
171 EXTENSION_FUNCTION_VALIDATE(params.get());
173 error_ = rules_registry_->AddRules(extension_id(), params->rules);
175 if (error_.empty())
176 results_ = AddRules::Results::Create(params->rules);
178 return error_.empty();
181 bool EventsEventRemoveRulesFunction::RunAsyncOnCorrectThread() {
182 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
183 EXTENSION_FUNCTION_VALIDATE(params.get());
185 if (params->rule_identifiers.get()) {
186 error_ = rules_registry_->RemoveRules(extension_id(),
187 *params->rule_identifiers);
188 } else {
189 error_ = rules_registry_->RemoveAllRules(extension_id());
192 return error_.empty();
195 bool EventsEventGetRulesFunction::RunAsyncOnCorrectThread() {
196 scoped_ptr<GetRules::Params> params(GetRules::Params::Create(*args_));
197 EXTENSION_FUNCTION_VALIDATE(params.get());
199 std::vector<linked_ptr<Rule> > rules;
200 if (params->rule_identifiers.get()) {
201 rules_registry_->GetRules(
202 extension_id(), *params->rule_identifiers, &rules);
203 } else {
204 rules_registry_->GetAllRules(extension_id(), &rules);
207 results_ = GetRules::Results::Create(rules);
209 return true;
212 } // namespace extensions