Home | History | Annotate | Download | only in declarative
      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.
      4 
      5 #include "chrome/browser/extensions/api/declarative/declarative_api.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/bind_helpers.h"
      9 #include "base/task_runner_util.h"
     10 #include "base/values.h"
     11 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
     12 #include "chrome/browser/extensions/extension_system_factory.h"
     13 #include "chrome/browser/guestview/webview/webview_guest.h"
     14 #include "chrome/browser/profiles/profile.h"
     15 #include "chrome/common/extensions/api/events.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"
     19 #include "extensions/common/extension_api.h"
     20 
     21 using extensions::api::events::Rule;
     22 
     23 namespace AddRules = extensions::api::events::Event::AddRules;
     24 namespace GetRules = extensions::api::events::Event::GetRules;
     25 namespace RemoveRules = extensions::api::events::Event::RemoveRules;
     26 
     27 
     28 namespace extensions {
     29 
     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 }
     55 
     56 RulesFunction::~RulesFunction() {}
     57 
     58 bool RulesFunction::HasPermission() {
     59   std::string 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;
     64   Feature::Availability availability =
     65       ExtensionAPI::GetSharedInstance()->IsAvailable(
     66           event_name, extension_, Feature::BLESSED_EXTENSION_CONTEXT,
     67           source_url());
     68   return availability.is_available();
     69 }
     70 
     71 bool RulesFunction::RunImpl() {
     72   std::string event_name;
     73   EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
     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 
     88   RulesRegistryService* rules_registry_service =
     89       RulesRegistryService::Get(GetProfile());
     90   rules_registry_ = rules_registry_service->GetRulesRegistry(key, event_name);
     91   // Raw access to this function is not available to extensions, therefore
     92   // there should never be a request for a nonexisting rules registry.
     93   EXTENSION_FUNCTION_VALIDATE(rules_registry_.get());
     94 
     95   if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) {
     96     bool success = RunImplOnCorrectThread();
     97     SendResponse(success);
     98   } else {
     99     scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
    100         content::BrowserThread::GetMessageLoopProxyForThread(
    101             rules_registry_->owner_thread());
    102     base::PostTaskAndReplyWithResult(
    103         message_loop_proxy.get(),
    104         FROM_HERE,
    105         base::Bind(&RulesFunction::RunImplOnCorrectThread, this),
    106         base::Bind(&RulesFunction::SendResponse, this));
    107   }
    108 
    109   return true;
    110 }
    111 
    112 bool EventsEventAddRulesFunction::RunImplOnCorrectThread() {
    113   scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
    114   EXTENSION_FUNCTION_VALIDATE(params.get());
    115 
    116   error_ = rules_registry_->AddRules(extension_id(), params->rules);
    117 
    118   if (error_.empty())
    119     results_ = AddRules::Results::Create(params->rules);
    120 
    121   return error_.empty();
    122 }
    123 
    124 bool EventsEventRemoveRulesFunction::RunImplOnCorrectThread() {
    125   scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
    126   EXTENSION_FUNCTION_VALIDATE(params.get());
    127 
    128   if (params->rule_identifiers.get()) {
    129     error_ = rules_registry_->RemoveRules(extension_id(),
    130                                           *params->rule_identifiers);
    131   } else {
    132     error_ = rules_registry_->RemoveAllRules(extension_id());
    133   }
    134 
    135   return error_.empty();
    136 }
    137 
    138 bool EventsEventGetRulesFunction::RunImplOnCorrectThread() {
    139   scoped_ptr<GetRules::Params> params(GetRules::Params::Create(*args_));
    140   EXTENSION_FUNCTION_VALIDATE(params.get());
    141 
    142   std::vector<linked_ptr<Rule> > rules;
    143   if (params->rule_identifiers.get()) {
    144     rules_registry_->GetRules(
    145         extension_id(), *params->rule_identifiers, &rules);
    146   } else {
    147     rules_registry_->GetAllRules(extension_id(), &rules);
    148   }
    149 
    150   results_ = GetRules::Results::Create(rules);
    151 
    152   return true;
    153 }
    154 
    155 }  // namespace extensions
    156