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.
5 #include "chrome/browser/extensions/script_executor.h"
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/pickle.h"
11 #include "chrome/browser/extensions/active_script_controller.h"
12 #include "chrome/browser/extensions/tab_helper.h"
13 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/common/extension_messages.h"
20 #include "ipc/ipc_message.h"
21 #include "ipc/ipc_message_macros.h"
27 namespace extensions
{
31 const char* kRendererDestroyed
= "The tab was closed.";
33 // A handler for a single injection request. On creation this will send the
34 // injection request to the renderer, and it will be destroyed after either the
35 // corresponding response comes from the renderer, or the renderer is destroyed.
36 class Handler
: public content::WebContentsObserver
{
38 Handler(ObserverList
<TabHelper::ScriptExecutionObserver
>* script_observers
,
39 content::WebContents
* web_contents
,
40 const ExtensionMsg_ExecuteCode_Params
& params
,
41 const ScriptExecutor::ExecuteScriptCallback
& callback
)
42 : content::WebContentsObserver(web_contents
),
43 script_observers_(AsWeakPtr(script_observers
)),
44 extension_id_(params
.extension_id
),
45 request_id_(params
.request_id
),
47 content::RenderViewHost
* rvh
= web_contents
->GetRenderViewHost();
48 rvh
->Send(new ExtensionMsg_ExecuteCode(rvh
->GetRoutingID(), params
));
53 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
{
54 // Unpack by hand to check the request_id, since there may be multiple
55 // requests in flight but only one is for this.
56 if (message
.type() != ExtensionHostMsg_ExecuteCodeFinished::ID
)
59 int message_request_id
;
60 PickleIterator
iter(message
);
61 CHECK(message
.ReadInt(&iter
, &message_request_id
));
63 if (message_request_id
!= request_id_
)
66 IPC_BEGIN_MESSAGE_MAP(Handler
, message
)
67 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ExecuteCodeFinished
,
68 OnExecuteCodeFinished
)
73 virtual void WebContentsDestroyed() OVERRIDE
{
75 callback_
.Run(kRendererDestroyed
, -1, GURL(std::string()), val
);
80 void OnExecuteCodeFinished(int request_id
,
81 const std::string
& error
,
84 const base::ListValue
& script_result
) {
85 if (script_observers_
.get() && error
.empty()) {
86 TabHelper::ScriptExecutionObserver::ExecutingScriptsMap id_map
;
87 id_map
[extension_id_
] = std::set
<std::string
>();
88 FOR_EACH_OBSERVER(TabHelper::ScriptExecutionObserver
, *script_observers_
,
89 OnScriptsExecuted(web_contents(),
95 callback_
.Run(error
, on_page_id
, on_url
, script_result
);
99 base::WeakPtr
<ObserverList
<TabHelper::ScriptExecutionObserver
> >
101 std::string extension_id_
;
103 ScriptExecutor::ExecuteScriptCallback callback_
;
108 ScriptExecutor::ScriptExecutor(
109 content::WebContents
* web_contents
,
110 ObserverList
<TabHelper::ScriptExecutionObserver
>* script_observers
)
111 : next_request_id_(0),
112 web_contents_(web_contents
),
113 script_observers_(script_observers
) {
114 CHECK(web_contents_
);
117 ScriptExecutor::~ScriptExecutor() {}
119 void ScriptExecutor::ExecuteScript(const std::string
& extension_id
,
120 ScriptExecutor::ScriptType script_type
,
121 const std::string
& code
,
122 ScriptExecutor::FrameScope frame_scope
,
123 ScriptExecutor::MatchAboutBlank about_blank
,
124 UserScript::RunLocation run_at
,
125 ScriptExecutor::WorldType world_type
,
126 ScriptExecutor::ProcessType process_type
,
127 const GURL
& webview_src
,
128 const GURL
& file_url
,
130 ScriptExecutor::ResultType result_type
,
131 const ExecuteScriptCallback
& callback
) {
132 // Don't execute if the extension has been unloaded.
133 const Extension
* extension
=
134 ExtensionRegistry::Get(web_contents_
->GetBrowserContext())
135 ->enabled_extensions().GetByID(extension_id
);
139 // Don't execute if there's no visible entry. If this is the case, then our
140 // permissions checking is useless (because we can't evaluate the URL).
141 // TODO(rdevlin.cronin): This might be better somewhere higher up the
142 // callstack, but we know it's caught here.
143 content::NavigationEntry
* visible_entry
=
144 web_contents_
->GetController().GetVisibleEntry();
148 scoped_ptr
<ExtensionMsg_ExecuteCode_Params
> params(
149 new ExtensionMsg_ExecuteCode_Params());
150 params
->request_id
= next_request_id_
++;
151 params
->extension_id
= extension_id
;
152 params
->is_javascript
= (script_type
== JAVASCRIPT
);
154 params
->all_frames
= (frame_scope
== ALL_FRAMES
);
155 params
->match_about_blank
= (about_blank
== MATCH_ABOUT_BLANK
);
156 params
->run_at
= static_cast<int>(run_at
);
157 params
->in_main_world
= (world_type
== MAIN_WORLD
);
158 params
->is_web_view
= (process_type
== WEB_VIEW_PROCESS
);
159 params
->webview_src
= webview_src
;
160 params
->file_url
= file_url
;
161 params
->wants_result
= (result_type
== JSON_SERIALIZED_RESULT
);
162 params
->user_gesture
= user_gesture
;
164 ActiveScriptController
* active_script_controller
=
165 ActiveScriptController::GetForWebContents(web_contents_
);
166 if (active_script_controller
&&
167 active_script_controller
->RequiresUserConsentForScriptInjection(
169 // The base::Unretained(this) is safe, because this and the
170 // ActiveScriptController are both attached to the TabHelper. Thus, if the
171 // ActiveScriptController is still alive to invoke the callback, this is
173 active_script_controller
->RequestScriptInjection(
175 visible_entry
->GetPageID(),
176 base::Closure(base::Bind(&ScriptExecutor::ExecuteScriptHelper
,
177 base::Unretained(this),
178 base::Passed(params
.Pass()),
181 ExecuteScriptHelper(params
.Pass(), callback
);
185 void ScriptExecutor::ExecuteScriptHelper(
186 scoped_ptr
<ExtensionMsg_ExecuteCode_Params
> params
,
187 const ExecuteScriptCallback
& callback
) {
188 // Handler handles IPCs and deletes itself on completion.
189 new Handler(script_observers_
, web_contents_
, *params
, callback
);
192 } // namespace extensions