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"
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"
24 namespace extensions
{
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
{
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 extension_id_(params
.extension_id
),
42 request_id_(params
.request_id
),
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
)
56 int message_request_id
;
57 PickleIterator
iter(message
);
58 CHECK(iter
.ReadInt(&message_request_id
));
60 if (message_request_id
!= request_id_
)
63 IPC_BEGIN_MESSAGE_MAP(Handler
, message
)
64 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ExecuteCodeFinished
,
65 OnExecuteCodeFinished
)
70 void WebContentsDestroyed() override
{
72 callback_
.Run(kRendererDestroyed
, GURL(std::string()), val
);
77 void OnExecuteCodeFinished(int request_id
,
78 const std::string
& error
,
80 const base::ListValue
& script_result
) {
81 if (script_observers_
.get() && error
.empty()) {
82 ScriptExecutionObserver::ExecutingScriptsMap id_map
;
83 id_map
[extension_id_
] = std::set
<std::string
>();
84 FOR_EACH_OBSERVER(ScriptExecutionObserver
,
86 OnScriptsExecuted(web_contents(), id_map
, on_url
));
89 callback_
.Run(error
, on_url
, script_result
);
93 base::WeakPtr
<ObserverList
<ScriptExecutionObserver
> > script_observers_
;
94 std::string extension_id_
;
96 ScriptExecutor::ExecuteScriptCallback callback_
;
101 ScriptExecutionObserver::~ScriptExecutionObserver() {
104 ScriptExecutor::ScriptExecutor(
105 content::WebContents
* web_contents
,
106 ObserverList
<ScriptExecutionObserver
>* script_observers
)
107 : next_request_id_(0),
108 web_contents_(web_contents
),
109 script_observers_(script_observers
) {
110 CHECK(web_contents_
);
113 ScriptExecutor::~ScriptExecutor() {
116 void ScriptExecutor::ExecuteScript(const std::string
& extension_id
,
117 ScriptExecutor::ScriptType script_type
,
118 const std::string
& code
,
119 ScriptExecutor::FrameScope frame_scope
,
120 ScriptExecutor::MatchAboutBlank about_blank
,
121 UserScript::RunLocation run_at
,
122 ScriptExecutor::WorldType world_type
,
123 ScriptExecutor::ProcessType process_type
,
124 const GURL
& webview_src
,
125 const GURL
& file_url
,
127 ScriptExecutor::ResultType result_type
,
128 const ExecuteScriptCallback
& callback
) {
129 // Don't execute if the extension has been unloaded.
130 const Extension
* extension
=
131 ExtensionRegistry::Get(web_contents_
->GetBrowserContext())
132 ->enabled_extensions().GetByID(extension_id
);
136 ExtensionMsg_ExecuteCode_Params params
;
137 params
.request_id
= next_request_id_
++;
138 params
.extension_id
= extension_id
;
139 params
.is_javascript
= (script_type
== JAVASCRIPT
);
141 params
.all_frames
= (frame_scope
== ALL_FRAMES
);
142 params
.match_about_blank
= (about_blank
== MATCH_ABOUT_BLANK
);
143 params
.run_at
= static_cast<int>(run_at
);
144 params
.in_main_world
= (world_type
== MAIN_WORLD
);
145 params
.is_web_view
= (process_type
== WEB_VIEW_PROCESS
);
146 params
.webview_src
= webview_src
;
147 params
.file_url
= file_url
;
148 params
.wants_result
= (result_type
== JSON_SERIALIZED_RESULT
);
149 params
.user_gesture
= user_gesture
;
151 // Handler handles IPCs and deletes itself on completion.
152 new Handler(script_observers_
, web_contents_
, params
, callback
);
155 } // namespace extensions