| OLD | NEW |
| 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 "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/render_view_host.h" |
| 16 #include "extensions/common/extension_api.h" | 19 #include "extensions/common/extension_api.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 |
| 24 namespace extensions { | 28 namespace extensions { |
| 25 | 29 |
| 26 RulesFunction::RulesFunction() : rules_registry_(NULL) {} | 30 namespace { |
| 31 |
| 32 const char kWebRequest[] = "declarativeWebRequest."; |
| 33 const char kWebView[] = "webview."; |
| 34 const char kWebViewExpectedError[] = "Webview event with Webview ID expected."; |
| 35 |
| 36 bool IsWebViewEvent(const std::string& event_name) { |
| 37 // Sample event names: |
| 38 // webview.onRequest. |
| 39 // webview.OnMessage. |
| 40 return event_name.compare(0, strlen(kWebView), kWebView) == 0; |
| 41 } |
| 42 |
| 43 std::string GetWebRequestEventName(const std::string& event_name) { |
| 44 std::string web_request_event_name(event_name); |
| 45 if (IsWebViewEvent(web_request_event_name)) |
| 46 web_request_event_name.replace(0, strlen(kWebView), kWebRequest); |
| 47 return web_request_event_name; |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 RulesFunction::RulesFunction() |
| 53 : rules_registry_(NULL) { |
| 54 } |
| 27 | 55 |
| 28 RulesFunction::~RulesFunction() {} | 56 RulesFunction::~RulesFunction() {} |
| 29 | 57 |
| 30 bool RulesFunction::HasPermission() { | 58 bool RulesFunction::HasPermission() { |
| 31 std::string event_name; | 59 std::string event_name; |
| 32 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); | 60 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 61 if (IsWebViewEvent(event_name) && |
| 62 extension_->HasAPIPermission(extensions::APIPermission::kWebView)) |
| 63 return true; |
| 33 Feature::Availability availability = | 64 Feature::Availability availability = |
| 34 ExtensionAPI::GetSharedInstance()->IsAvailable( | 65 ExtensionAPI::GetSharedInstance()->IsAvailable( |
| 35 event_name, extension_, Feature::BLESSED_EXTENSION_CONTEXT, | 66 event_name, extension_, Feature::BLESSED_EXTENSION_CONTEXT, |
| 36 source_url()); | 67 source_url()); |
| 37 return availability.is_available(); | 68 return availability.is_available(); |
| 38 } | 69 } |
| 39 | 70 |
| 40 bool RulesFunction::RunImpl() { | 71 bool RulesFunction::RunImpl() { |
| 41 std::string event_name; | 72 std::string event_name; |
| 42 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); | 73 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 43 | 74 |
| 75 int webview_instance_id = 0; |
| 76 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &webview_instance_id)); |
| 77 int embedder_process_id = render_view_host()->GetProcess()->GetID(); |
| 78 |
| 79 bool has_webview = webview_instance_id != 0; |
| 80 if (has_webview != IsWebViewEvent(event_name)) |
| 81 EXTENSION_FUNCTION_ERROR(kWebViewExpectedError); |
| 82 event_name = GetWebRequestEventName(event_name); |
| 83 |
| 84 // If we are not operating on a particular <webview>, then the key is (0, 0). |
| 85 RulesRegistryService::WebViewKey key( |
| 86 webview_instance_id ? embedder_process_id : 0, webview_instance_id); |
| 87 |
| 44 RulesRegistryService* rules_registry_service = | 88 RulesRegistryService* rules_registry_service = |
| 45 RulesRegistryService::Get(GetProfile()); | 89 RulesRegistryService::Get(GetProfile()); |
| 46 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name); | 90 rules_registry_ = rules_registry_service->GetRulesRegistry(key, event_name); |
| 47 // Raw access to this function is not available to extensions, therefore | 91 // Raw access to this function is not available to extensions, therefore |
| 48 // there should never be a request for a nonexisting rules registry. | 92 // there should never be a request for a nonexisting rules registry. |
| 49 EXTENSION_FUNCTION_VALIDATE(rules_registry_.get()); | 93 EXTENSION_FUNCTION_VALIDATE(rules_registry_.get()); |
| 50 | 94 |
| 51 if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) { | 95 if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) { |
| 52 bool success = RunImplOnCorrectThread(); | 96 bool success = RunImplOnCorrectThread(); |
| 53 SendResponse(success); | 97 SendResponse(success); |
| 54 } else { | 98 } else { |
| 55 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | 99 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
| 56 content::BrowserThread::GetMessageLoopProxyForThread( | 100 content::BrowserThread::GetMessageLoopProxyForThread( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 error_ = rules_registry_->GetAllRules(extension_id(), &rules); | 148 error_ = rules_registry_->GetAllRules(extension_id(), &rules); |
| 105 } | 149 } |
| 106 | 150 |
| 107 if (error_.empty()) | 151 if (error_.empty()) |
| 108 results_ = GetRules::Results::Create(rules); | 152 results_ = GetRules::Results::Create(rules); |
| 109 | 153 |
| 110 return error_.empty(); | 154 return error_.empty(); |
| 111 } | 155 } |
| 112 | 156 |
| 113 } // namespace extensions | 157 } // namespace extensions |
| OLD | NEW |