Add some convenience commands for building and uploading the linux sysroot image
[chromium-blink-merge.git] / extensions / browser / script_executor.cc
blobbdd831abdccfcc65b4b1470649c38a027d4021c4
1 // Copyright 2014 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/script_executor.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/pickle.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "extensions/browser/extension_registry.h"
15 #include "extensions/browser/script_execution_observer.h"
16 #include "extensions/common/extension_messages.h"
17 #include "ipc/ipc_message.h"
18 #include "ipc/ipc_message_macros.h"
20 namespace base {
21 class ListValue;
22 } // namespace base
24 namespace extensions {
26 namespace {
28 const char* kRendererDestroyed = "The tab was closed.";
30 // A handler for a single injection request. On creation this will send the
31 // injection request to the renderer, and it will be destroyed after either the
32 // corresponding response comes from the renderer, or the renderer is destroyed.
33 class Handler : public content::WebContentsObserver {
34 public:
35 Handler(ObserverList<ScriptExecutionObserver>* script_observers,
36 content::WebContents* web_contents,
37 const ExtensionMsg_ExecuteCode_Params& params,
38 const ScriptExecutor::ExecuteScriptCallback& callback)
39 : content::WebContentsObserver(web_contents),
40 script_observers_(AsWeakPtr(script_observers)),
41 host_id_(params.host_id),
42 request_id_(params.request_id),
43 callback_(callback) {
44 content::RenderViewHost* rvh = web_contents->GetRenderViewHost();
45 rvh->Send(new ExtensionMsg_ExecuteCode(rvh->GetRoutingID(), params));
48 ~Handler() override {}
50 bool OnMessageReceived(const IPC::Message& message) override {
51 // Unpack by hand to check the request_id, since there may be multiple
52 // requests in flight but only one is for this.
53 if (message.type() != ExtensionHostMsg_ExecuteCodeFinished::ID)
54 return false;
56 int message_request_id;
57 PickleIterator iter(message);
58 CHECK(iter.ReadInt(&message_request_id));
60 if (message_request_id != request_id_)
61 return false;
63 IPC_BEGIN_MESSAGE_MAP(Handler, message)
64 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ExecuteCodeFinished,
65 OnExecuteCodeFinished)
66 IPC_END_MESSAGE_MAP()
67 return true;
70 void WebContentsDestroyed() override {
71 base::ListValue val;
72 callback_.Run(kRendererDestroyed, GURL(std::string()), val);
73 delete this;
76 private:
77 void OnExecuteCodeFinished(int request_id,
78 const std::string& error,
79 const GURL& on_url,
80 const base::ListValue& script_result) {
81 if (script_observers_.get() && error.empty() &&
82 host_id_.type() == HostID::EXTENSIONS) {
83 ScriptExecutionObserver::ExecutingScriptsMap id_map;
84 id_map[host_id_.id()] = std::set<std::string>();
85 FOR_EACH_OBSERVER(ScriptExecutionObserver,
86 *script_observers_,
87 OnScriptsExecuted(web_contents(), id_map, on_url));
90 callback_.Run(error, on_url, script_result);
91 delete this;
94 base::WeakPtr<ObserverList<ScriptExecutionObserver> > script_observers_;
95 HostID host_id_;
96 int request_id_;
97 ScriptExecutor::ExecuteScriptCallback callback_;
100 } // namespace
102 ScriptExecutionObserver::~ScriptExecutionObserver() {
105 ScriptExecutor::ScriptExecutor(
106 content::WebContents* web_contents,
107 ObserverList<ScriptExecutionObserver>* script_observers)
108 : next_request_id_(0),
109 web_contents_(web_contents),
110 script_observers_(script_observers) {
111 CHECK(web_contents_);
114 ScriptExecutor::~ScriptExecutor() {
117 void ScriptExecutor::ExecuteScript(const HostID& host_id,
118 ScriptExecutor::ScriptType script_type,
119 const std::string& code,
120 ScriptExecutor::FrameScope frame_scope,
121 ScriptExecutor::MatchAboutBlank about_blank,
122 UserScript::RunLocation run_at,
123 ScriptExecutor::WorldType world_type,
124 ScriptExecutor::ProcessType process_type,
125 const GURL& webview_src,
126 const GURL& file_url,
127 bool user_gesture,
128 ScriptExecutor::ResultType result_type,
129 const ExecuteScriptCallback& callback) {
130 if (host_id.type() == HostID::EXTENSIONS) {
131 // Don't execute if the extension has been unloaded.
132 const Extension* extension =
133 ExtensionRegistry::Get(web_contents_->GetBrowserContext())
134 ->enabled_extensions().GetByID(host_id.id());
135 if (!extension)
136 return;
137 } else {
138 CHECK(process_type == WEB_VIEW_PROCESS);
141 ExtensionMsg_ExecuteCode_Params params;
142 params.request_id = next_request_id_++;
143 params.host_id = host_id;
144 params.is_javascript = (script_type == JAVASCRIPT);
145 params.code = code;
146 params.all_frames = (frame_scope == ALL_FRAMES);
147 params.match_about_blank = (about_blank == MATCH_ABOUT_BLANK);
148 params.run_at = static_cast<int>(run_at);
149 params.in_main_world = (world_type == MAIN_WORLD);
150 params.is_web_view = (process_type == WEB_VIEW_PROCESS);
151 params.webview_src = webview_src;
152 params.file_url = file_url;
153 params.wants_result = (result_type == JSON_SERIALIZED_RESULT);
154 params.user_gesture = user_gesture;
156 // Handler handles IPCs and deletes itself on completion.
157 new Handler(script_observers_, web_contents_, params, callback);
160 } // namespace extensions