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

Side by Side Diff: chrome/browser/extensions/api/declarative/declarative_api.cc

Issue 28273006: <webview>: Implement declarativeWebRequest API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added tests Created 7 years, 1 month 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/api/declarative/declarative_api.h" 5 #include "chrome/browser/extensions/api/declarative/declarative_api.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/task_runner_util.h" 9 #include "base/task_runner_util.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" 11 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
12 #include "chrome/browser/extensions/extension_system_factory.h" 12 #include "chrome/browser/extensions/extension_system_factory.h"
13 #include "chrome/browser/guestview/webview/webview_guest.h"
13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/extensions/api/events.h" 15 #include "chrome/common/extensions/api/events.h"
15 #include "chrome/common/extensions/api/extension_api.h" 16 #include "chrome/common/extensions/api/extension_api.h"
16 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/render_view_host.h"
17 20
18 using extensions::api::events::Rule; 21 using extensions::api::events::Rule;
19 22
20 namespace AddRules = extensions::api::events::Event::AddRules; 23 namespace AddRules = extensions::api::events::Event::AddRules;
21 namespace GetRules = extensions::api::events::Event::GetRules; 24 namespace GetRules = extensions::api::events::Event::GetRules;
22 namespace RemoveRules = extensions::api::events::Event::RemoveRules; 25 namespace RemoveRules = extensions::api::events::Event::RemoveRules;
23 26
27 namespace {
28
29 using extensions::RulesRegistryService;
30
31 const char kWebRequest[] = "declarativeWebRequest";
32 const char kWebView[] = "webview";
33
34 std::string GetWebRequestEventName(const std::string& event_name) {
35 std::string web_request_event_name(event_name);
36 if (web_request_event_name.find(kWebView) != std::string::npos)
vabr (Chromium) 2013/10/25 20:40:24 [This is optional, and probably questionable, feel
Fady Samuel 2013/10/25 22:48:08 It doesn't hurt to be more restrictive. Done.
37 web_request_event_name.replace(0, sizeof(kWebView) - 1, kWebRequest);
38 return web_request_event_name;
39 }
40
41 } // namespace
42
24 namespace extensions { 43 namespace extensions {
25 44
26 RulesFunction::RulesFunction() : rules_registry_(NULL) {} 45 RulesFunction::RulesFunction()
46 : rules_registry_(NULL),
47 webview_instance_id_(0) {
48 }
27 49
28 RulesFunction::~RulesFunction() {} 50 RulesFunction::~RulesFunction() {}
29 51
30 bool RulesFunction::HasPermission() { 52 bool RulesFunction::HasPermission() {
31 std::string event_name; 53 std::string event_name;
32 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); 54 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
55 if ((event_name.find(kWebView) != std::string::npos) &&
56 extension_->HasAPIPermission(extensions::APIPermission::kWebView))
57 return true;
33 Feature::Availability availability = 58 Feature::Availability availability =
34 ExtensionAPI::GetSharedInstance()->IsAvailable( 59 ExtensionAPI::GetSharedInstance()->IsAvailable(
35 event_name, extension_, Feature::BLESSED_EXTENSION_CONTEXT, 60 event_name, extension_, Feature::BLESSED_EXTENSION_CONTEXT,
36 source_url()); 61 source_url());
37 return availability.is_available(); 62 return availability.is_available();
38 } 63 }
39 64
40 bool RulesFunction::RunImpl() { 65 bool RulesFunction::RunImpl() {
41 std::string event_name; 66 std::string event_name;
42 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); 67 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
68 event_name = GetWebRequestEventName(event_name);
69
70 int webview_instance_id = 0;
71 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &webview_instance_id));
72 int embedder_process_id = render_view_host()->GetProcess()->GetID();
73
74 // Note: This is structured for readability.
75 // We can only install rules on a <webview> in a context with the webview
76 // permission.
77 if (extension_->HasAPIPermission(extensions::APIPermission::kWebView)) {
78 if (!webview_instance_id)
79 return false;
80 } else {
81 if (webview_instance_id)
82 return false;
83 }
84
85 // If are not operating on a particular <webview> then the key is (0, 0).
vabr (Chromium) 2013/10/25 20:40:24 grammar: remove "are"?
Fady Samuel 2013/10/25 22:48:08 Added "we"
86 RulesRegistryService::WebViewKey key(
87 webview_instance_id ? embedder_process_id : 0, webview_instance_id);
43 88
44 RulesRegistryService* rules_registry_service = 89 RulesRegistryService* rules_registry_service =
45 RulesRegistryService::Get(profile()); 90 RulesRegistryService::Get(profile());
46 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name); 91 rules_registry_ = rules_registry_service->GetRulesRegistry(key, event_name);
92
47 // Raw access to this function is not available to extensions, therefore 93 // Raw access to this function is not available to extensions, therefore
48 // there should never be a request for a nonexisting rules registry. 94 // there should never be a request for a nonexisting rules registry.
49 EXTENSION_FUNCTION_VALIDATE(rules_registry_.get()); 95 EXTENSION_FUNCTION_VALIDATE(rules_registry_.get());
50 96
51 if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) { 97 if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) {
52 bool success = RunImplOnCorrectThread(); 98 bool success = RunImplOnCorrectThread();
53 SendResponse(success); 99 SendResponse(success);
54 } else { 100 } else {
55 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = 101 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
56 content::BrowserThread::GetMessageLoopProxyForThread( 102 content::BrowserThread::GetMessageLoopProxyForThread(
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 error_ = rules_registry_->GetAllRules(extension_id(), &rules); 150 error_ = rules_registry_->GetAllRules(extension_id(), &rules);
105 } 151 }
106 152
107 if (error_.empty()) 153 if (error_.empty())
108 results_ = GetRules::Results::Create(rules); 154 results_ = GetRules::Results::Create(rules);
109 155
110 return error_.empty(); 156 return error_.empty();
111 } 157 }
112 158
113 } // namespace extensions 159 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698