Home | History | Annotate | Download | only in webview
      1 // Copyright (c) 2013 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/webview/webview_api.h"
      6 
      7 #include "chrome/browser/extensions/tab_helper.h"
      8 #include "chrome/browser/guestview/webview/webview_guest.h"
      9 #include "chrome/common/extensions/api/webview.h"
     10 #include "content/public/browser/render_process_host.h"
     11 #include "content/public/browser/render_view_host.h"
     12 #include "content/public/browser/user_metrics.h"
     13 #include "content/public/browser/web_contents.h"
     14 #include "extensions/common/error_utils.h"
     15 
     16 using extensions::api::tabs::InjectDetails;
     17 
     18 WebviewExecuteCodeFunction::WebviewExecuteCodeFunction()
     19     : guest_instance_id_(0) {
     20 }
     21 
     22 WebviewExecuteCodeFunction::~WebviewExecuteCodeFunction() {
     23 }
     24 
     25 bool WebviewExecuteCodeFunction::Init() {
     26   if (details_.get())
     27     return true;
     28 
     29   if (!args_->GetInteger(0, &guest_instance_id_))
     30     return false;
     31 
     32   if (!guest_instance_id_)
     33     return false;
     34 
     35   base::DictionaryValue* details_value = NULL;
     36   if (!args_->GetDictionary(1, &details_value))
     37     return false;
     38   scoped_ptr<InjectDetails> details(new InjectDetails());
     39   if (!InjectDetails::Populate(*details_value, details.get()))
     40     return false;
     41 
     42   details_ = details.Pass();
     43   return true;
     44 }
     45 
     46 bool WebviewExecuteCodeFunction::ShouldInsertCSS() const {
     47   return false;
     48 }
     49 
     50 bool WebviewExecuteCodeFunction::CanExecuteScriptOnPage() {
     51   return true;
     52 }
     53 
     54 extensions::ScriptExecutor* WebviewExecuteCodeFunction::GetScriptExecutor() {
     55   WebViewGuest* guest = WebViewGuest::From(
     56       render_view_host()->GetProcess()->GetID(), guest_instance_id_);
     57   if (!guest)
     58     return NULL;
     59 
     60   return guest->script_executor();
     61 }
     62 
     63 bool WebviewExecuteCodeFunction::IsWebView() const {
     64   return true;
     65 }
     66 
     67 WebviewExecuteScriptFunction::WebviewExecuteScriptFunction() {
     68 }
     69 
     70 void WebviewExecuteScriptFunction::OnExecuteCodeFinished(
     71     const std::string& error,
     72     int32 on_page_id,
     73     const GURL& on_url,
     74     const base::ListValue& result) {
     75   content::RecordAction(content::UserMetricsAction("WebView.ExecuteScript"));
     76   if (error.empty())
     77     SetResult(result.DeepCopy());
     78   WebviewExecuteCodeFunction::OnExecuteCodeFinished(error, on_page_id, on_url,
     79                                                     result);
     80 }
     81 
     82 WebviewInsertCSSFunction::WebviewInsertCSSFunction() {
     83 }
     84 
     85 bool WebviewInsertCSSFunction::ShouldInsertCSS() const {
     86   return true;
     87 }
     88 
     89 WebviewGoFunction::WebviewGoFunction() {
     90 }
     91 
     92 WebviewGoFunction::~WebviewGoFunction() {
     93 }
     94 
     95 bool WebviewGoFunction::RunImpl() {
     96   int instance_id = 0;
     97   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
     98 
     99   int relative_index = 0;
    100   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &relative_index));
    101 
    102   WebViewGuest* guest = WebViewGuest::From(
    103       render_view_host()->GetProcess()->GetID(), instance_id);
    104   if (!guest)
    105     return false;
    106 
    107   guest->Go(relative_index);
    108   return true;
    109 }
    110 
    111 WebviewReloadFunction::WebviewReloadFunction() {
    112 }
    113 
    114 WebviewReloadFunction::~WebviewReloadFunction() {
    115 }
    116 
    117 bool WebviewReloadFunction::RunImpl() {
    118   int instance_id = 0;
    119   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
    120 
    121   WebViewGuest* guest = WebViewGuest::From(
    122       render_view_host()->GetProcess()->GetID(), instance_id);
    123   if (!guest)
    124     return false;
    125 
    126   guest->Reload();
    127   return true;
    128 }
    129 
    130 WebviewSetPermissionFunction::WebviewSetPermissionFunction() {
    131 }
    132 
    133 WebviewSetPermissionFunction::~WebviewSetPermissionFunction() {
    134 }
    135 
    136 bool WebviewSetPermissionFunction::RunImpl() {
    137   int instance_id = 0;
    138   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
    139 
    140   int request_id = 0;
    141   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &request_id));
    142 
    143   bool should_allow = false;
    144   EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(2, &should_allow));
    145 
    146   std::string user_input;
    147   EXTENSION_FUNCTION_VALIDATE(args_->GetString(3, &user_input));
    148 
    149   WebViewGuest* guest = WebViewGuest::From(
    150       render_view_host()->GetProcess()->GetID(), instance_id);
    151   if (!guest)
    152     return false;
    153 
    154   EXTENSION_FUNCTION_VALIDATE(
    155       guest->SetPermission(request_id, should_allow, user_input));
    156   return true;
    157 }
    158 
    159 WebviewStopFunction::WebviewStopFunction() {
    160 }
    161 
    162 WebviewStopFunction::~WebviewStopFunction() {
    163 }
    164 
    165 bool WebviewStopFunction::RunImpl() {
    166   int instance_id = 0;
    167   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
    168 
    169   WebViewGuest* guest = WebViewGuest::From(
    170       render_view_host()->GetProcess()->GetID(), instance_id);
    171   if (!guest)
    172     return false;
    173 
    174   guest->Stop();
    175   return true;
    176 }
    177 
    178 WebviewTerminateFunction::WebviewTerminateFunction() {
    179 }
    180 
    181 WebviewTerminateFunction::~WebviewTerminateFunction() {
    182 }
    183 
    184 bool WebviewTerminateFunction::RunImpl() {
    185   int instance_id = 0;
    186   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
    187 
    188   WebViewGuest* guest = WebViewGuest::From(
    189       render_view_host()->GetProcess()->GetID(), instance_id);
    190   if (!guest)
    191     return false;
    192 
    193   guest->Terminate();
    194   return true;
    195 }
    196