Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: extensions/browser/script_executor.cc

Issue 1628423002: Add frameId to chrome.tabs.executeScript/insertCSS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@permissiondata-remove-process_id
Patch Set: Remove unused include from rebase Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/browser/script_executor.h ('k') | extensions/common/api/extension_types.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/script_executor.h" 5 #include "extensions/browser/script_executor.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/pickle.h" 11 #include "base/pickle.h"
12 #include "content/public/browser/render_frame_host.h" 12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/render_view_host.h" 13 #include "content/public/browser/render_view_host.h"
14 #include "content/public/browser/web_contents.h" 14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_observer.h" 15 #include "content/public/browser/web_contents_observer.h"
16 #include "extensions/browser/extension_api_frame_id_map.h"
16 #include "extensions/browser/extension_registry.h" 17 #include "extensions/browser/extension_registry.h"
17 #include "extensions/browser/script_execution_observer.h" 18 #include "extensions/browser/script_execution_observer.h"
18 #include "extensions/common/extension_messages.h" 19 #include "extensions/common/extension_messages.h"
19 #include "ipc/ipc_message.h" 20 #include "ipc/ipc_message.h"
20 #include "ipc/ipc_message_macros.h" 21 #include "ipc/ipc_message_macros.h"
21 22
22 namespace base { 23 namespace base {
23 class ListValue; 24 class ListValue;
24 } // namespace base 25 } // namespace base
25 26
26 namespace extensions { 27 namespace extensions {
27 28
28 namespace { 29 namespace {
29 30
30 const char* kRendererDestroyed = "The tab was closed."; 31 const char* kRendererDestroyed = "The tab was closed.";
32 const char* kFrameRemoved = "The frame was removed.";
31 33
32 // A handler for a single injection request. On creation this will send the 34 // A handler for a single injection request. On creation this will send the
33 // injection request to the renderer, and it will be destroyed after either the 35 // injection request to the renderer, and it will be destroyed after either the
34 // corresponding response comes from the renderer, or the renderer is destroyed. 36 // corresponding response comes from the renderer, or the renderer is destroyed.
35 class Handler : public content::WebContentsObserver { 37 class Handler : public content::WebContentsObserver {
36 public: 38 public:
37 Handler(base::ObserverList<ScriptExecutionObserver>* script_observers, 39 Handler(base::ObserverList<ScriptExecutionObserver>* script_observers,
38 content::WebContents* web_contents, 40 content::WebContents* web_contents,
39 const ExtensionMsg_ExecuteCode_Params& params, 41 const ExtensionMsg_ExecuteCode_Params& params,
40 ScriptExecutor::FrameScope scope, 42 ScriptExecutor::FrameScope scope,
43 int frame_id,
41 const ScriptExecutor::ExecuteScriptCallback& callback) 44 const ScriptExecutor::ExecuteScriptCallback& callback)
42 : content::WebContentsObserver(web_contents), 45 : content::WebContentsObserver(web_contents),
43 script_observers_(AsWeakPtr(script_observers)), 46 script_observers_(AsWeakPtr(script_observers)),
44 host_id_(params.host_id), 47 host_id_(params.host_id),
45 request_id_(params.request_id), 48 request_id_(params.request_id),
49 include_sub_frames_(scope == ScriptExecutor::INCLUDE_SUB_FRAMES),
50 root_rfh_(ExtensionApiFrameIdMap::GetRenderFrameHostById(web_contents,
51 frame_id)),
52 root_is_main_frame_(root_rfh_ ? !root_rfh_->GetParent() : false),
46 callback_(callback) { 53 callback_(callback) {
47 if (scope == ScriptExecutor::ALL_FRAMES) { 54 if (root_rfh_) {
48 web_contents->ForEachFrame(base::Bind(&Handler::SendExecuteCode, 55 if (include_sub_frames_) {
49 base::Unretained(this), params)); 56 web_contents->ForEachFrame(base::Bind(&Handler::SendExecuteCode,
50 } else { 57 base::Unretained(this), params));
51 SendExecuteCode(params, web_contents->GetMainFrame()); 58 } else {
59 SendExecuteCode(params, root_rfh_);
60 }
52 } 61 }
62
63 if (pending_render_frames_.empty())
64 Finish();
53 } 65 }
54 66
55 private: 67 private:
56 // This class manages its own lifetime. 68 // This class manages its own lifetime.
57 ~Handler() override {} 69 ~Handler() override {}
58 70
59 // content::WebContentsObserver: 71 // content::WebContentsObserver:
60 void WebContentsDestroyed() override { Finish(); } 72 void WebContentsDestroyed() override { Finish(); }
61 73
62 bool OnMessageReceived(const IPC::Message& message, 74 bool OnMessageReceived(const IPC::Message& message,
(...skipping 22 matching lines...) Expand all
85 if (pending_render_frames_.erase(render_frame_host) == 1 && 97 if (pending_render_frames_.erase(render_frame_host) == 1 &&
86 pending_render_frames_.empty()) { 98 pending_render_frames_.empty()) {
87 Finish(); 99 Finish();
88 } 100 }
89 } 101 }
90 102
91 // Sends an ExecuteCode message to the given frame host, and increments 103 // Sends an ExecuteCode message to the given frame host, and increments
92 // the number of pending messages. 104 // the number of pending messages.
93 void SendExecuteCode(const ExtensionMsg_ExecuteCode_Params& params, 105 void SendExecuteCode(const ExtensionMsg_ExecuteCode_Params& params,
94 content::RenderFrameHost* frame) { 106 content::RenderFrameHost* frame) {
107 if (!frame->IsRenderFrameLive())
108 return;
109 DCHECK(!root_is_main_frame_ || ShouldIncludeFrame(frame));
110 if (!root_is_main_frame_ && !ShouldIncludeFrame(frame))
111 return;
95 pending_render_frames_.insert(frame); 112 pending_render_frames_.insert(frame);
96 frame->Send(new ExtensionMsg_ExecuteCode(frame->GetRoutingID(), params)); 113 frame->Send(new ExtensionMsg_ExecuteCode(frame->GetRoutingID(), params));
97 } 114 }
98 115
116 // Returns whether a frame is the root frame or a descendant of it.
117 bool ShouldIncludeFrame(content::RenderFrameHost* frame) {
118 while (frame) {
119 if (frame == root_rfh_)
120 return true;
121 frame = frame->GetParent();
122 }
123 return false;
124 }
125
99 // Handles the ExecuteCodeFinished message. 126 // Handles the ExecuteCodeFinished message.
100 void OnExecuteCodeFinished(content::RenderFrameHost* render_frame_host, 127 void OnExecuteCodeFinished(content::RenderFrameHost* render_frame_host,
101 int request_id, 128 int request_id,
102 const std::string& error, 129 const std::string& error,
103 const GURL& on_url, 130 const GURL& on_url,
104 const base::ListValue& result_list) { 131 const base::ListValue& result_list) {
105 DCHECK_EQ(request_id_, request_id); 132 DCHECK_EQ(request_id_, request_id);
106 DCHECK(!pending_render_frames_.empty()); 133 DCHECK(!pending_render_frames_.empty());
107 bool erased = pending_render_frames_.erase(render_frame_host) == 1; 134 bool erased = pending_render_frames_.erase(render_frame_host) == 1;
108 DCHECK(erased); 135 DCHECK(erased);
109 bool is_main_frame = web_contents()->GetMainFrame() == render_frame_host; 136 bool is_root_frame = root_rfh_ == render_frame_host;
110 137
111 // Set the result, if there is one. 138 // Set the result, if there is one.
112 const base::Value* script_value = nullptr; 139 const base::Value* script_value = nullptr;
113 if (result_list.Get(0u, &script_value)) { 140 if (result_list.Get(0u, &script_value)) {
114 // If this is the main result, we put it at index 0. Otherwise, we just 141 // If this is the main result, we put it at index 0. Otherwise, we just
115 // append it at the end. 142 // append it at the end.
116 if (is_main_frame && !results_.empty()) 143 if (is_root_frame && !results_.empty())
117 CHECK(results_.Insert(0u, script_value->DeepCopy())); 144 CHECK(results_.Insert(0u, script_value->DeepCopy()));
118 else 145 else
119 results_.Append(script_value->DeepCopy()); 146 results_.Append(script_value->DeepCopy());
120 } 147 }
121 148
122 if (is_main_frame) { // Only use the main frame's error and url. 149 if (is_root_frame) { // Only use the root frame's error and url.
123 main_frame_error_ = error; 150 root_frame_error_ = error;
124 main_frame_url_ = on_url; 151 root_frame_url_ = on_url;
125 } 152 }
126 153
127 // Wait until the final request finishes before reporting back. 154 // Wait until the final request finishes before reporting back.
128 if (pending_render_frames_.empty()) 155 if (pending_render_frames_.empty())
129 Finish(); 156 Finish();
130 } 157 }
131 158
132 void Finish() { 159 void Finish() {
133 if (main_frame_url_.is_empty()) { 160 if (root_frame_url_.is_empty()) {
134 // We never finished the main frame injection. 161 // We never finished the root frame injection.
135 main_frame_error_ = kRendererDestroyed; 162 root_frame_error_ =
163 root_is_main_frame_ ? kRendererDestroyed : kFrameRemoved;
136 results_.Clear(); 164 results_.Clear();
137 } 165 }
138 166
139 if (script_observers_.get() && main_frame_error_.empty() && 167 if (script_observers_.get() && root_frame_error_.empty() &&
140 host_id_.type() == HostID::EXTENSIONS) { 168 host_id_.type() == HostID::EXTENSIONS) {
141 ScriptExecutionObserver::ExecutingScriptsMap id_map; 169 ScriptExecutionObserver::ExecutingScriptsMap id_map;
142 id_map[host_id_.id()] = std::set<std::string>(); 170 id_map[host_id_.id()] = std::set<std::string>();
143 FOR_EACH_OBSERVER( 171 FOR_EACH_OBSERVER(
144 ScriptExecutionObserver, *script_observers_, 172 ScriptExecutionObserver, *script_observers_,
145 OnScriptsExecuted(web_contents(), id_map, main_frame_url_)); 173 OnScriptsExecuted(web_contents(), id_map, root_frame_url_));
146 } 174 }
147 175
148 if (!callback_.is_null()) 176 if (!callback_.is_null())
149 callback_.Run(main_frame_error_, main_frame_url_, results_); 177 callback_.Run(root_frame_error_, root_frame_url_, results_);
150 delete this; 178 delete this;
151 } 179 }
152 180
153 base::WeakPtr<base::ObserverList<ScriptExecutionObserver>> script_observers_; 181 base::WeakPtr<base::ObserverList<ScriptExecutionObserver>> script_observers_;
154 182
155 // The id of the host (the extension or the webui) doing the injection. 183 // The id of the host (the extension or the webui) doing the injection.
156 HostID host_id_; 184 HostID host_id_;
157 185
158 // The request id of the injection. 186 // The request id of the injection.
159 int request_id_; 187 int request_id_;
160 188
189 // Whether to inject in |root_rfh_| and all of its descendant frames.
190 bool include_sub_frames_;
191
192 // The frame (and optionally its descendant frames) where the injection will
193 // occur.
194 content::RenderFrameHost* root_rfh_;
195
196 // Whether |root_rfh_| is the main frame of a tab.
197 bool root_is_main_frame_;
198
161 // The hosts of the still-running injections. 199 // The hosts of the still-running injections.
162 std::set<content::RenderFrameHost*> pending_render_frames_; 200 std::set<content::RenderFrameHost*> pending_render_frames_;
163 201
164 // The results of the injection. 202 // The results of the injection.
165 base::ListValue results_; 203 base::ListValue results_;
166 204
167 // The error from injecting into the main frame. 205 // The error from injecting into the root frame.
168 std::string main_frame_error_; 206 std::string root_frame_error_;
169 207
170 // The url of the main frame. 208 // The url of the root frame.
171 GURL main_frame_url_; 209 GURL root_frame_url_;
172 210
173 // The callback to run after all injections complete. 211 // The callback to run after all injections complete.
174 ScriptExecutor::ExecuteScriptCallback callback_; 212 ScriptExecutor::ExecuteScriptCallback callback_;
175 213
176 DISALLOW_COPY_AND_ASSIGN(Handler); 214 DISALLOW_COPY_AND_ASSIGN(Handler);
177 }; 215 };
178 216
179 } // namespace 217 } // namespace
180 218
181 ScriptExecutionObserver::~ScriptExecutionObserver() { 219 ScriptExecutionObserver::~ScriptExecutionObserver() {
182 } 220 }
183 221
184 ScriptExecutor::ScriptExecutor( 222 ScriptExecutor::ScriptExecutor(
185 content::WebContents* web_contents, 223 content::WebContents* web_contents,
186 base::ObserverList<ScriptExecutionObserver>* script_observers) 224 base::ObserverList<ScriptExecutionObserver>* script_observers)
187 : next_request_id_(0), 225 : next_request_id_(0),
188 web_contents_(web_contents), 226 web_contents_(web_contents),
189 script_observers_(script_observers) { 227 script_observers_(script_observers) {
190 CHECK(web_contents_); 228 CHECK(web_contents_);
191 } 229 }
192 230
193 ScriptExecutor::~ScriptExecutor() { 231 ScriptExecutor::~ScriptExecutor() {
194 } 232 }
195 233
196 void ScriptExecutor::ExecuteScript(const HostID& host_id, 234 void ScriptExecutor::ExecuteScript(const HostID& host_id,
197 ScriptExecutor::ScriptType script_type, 235 ScriptExecutor::ScriptType script_type,
198 const std::string& code, 236 const std::string& code,
199 ScriptExecutor::FrameScope frame_scope, 237 ScriptExecutor::FrameScope frame_scope,
238 int frame_id,
200 ScriptExecutor::MatchAboutBlank about_blank, 239 ScriptExecutor::MatchAboutBlank about_blank,
201 UserScript::RunLocation run_at, 240 UserScript::RunLocation run_at,
202 ScriptExecutor::WorldType world_type, 241 ScriptExecutor::WorldType world_type,
203 ScriptExecutor::ProcessType process_type, 242 ScriptExecutor::ProcessType process_type,
204 const GURL& webview_src, 243 const GURL& webview_src,
205 const GURL& file_url, 244 const GURL& file_url,
206 bool user_gesture, 245 bool user_gesture,
207 ScriptExecutor::ResultType result_type, 246 ScriptExecutor::ResultType result_type,
208 const ExecuteScriptCallback& callback) { 247 const ExecuteScriptCallback& callback) {
209 if (host_id.type() == HostID::EXTENSIONS) { 248 if (host_id.type() == HostID::EXTENSIONS) {
(...skipping 15 matching lines...) Expand all
225 params.match_about_blank = (about_blank == MATCH_ABOUT_BLANK); 264 params.match_about_blank = (about_blank == MATCH_ABOUT_BLANK);
226 params.run_at = static_cast<int>(run_at); 265 params.run_at = static_cast<int>(run_at);
227 params.in_main_world = (world_type == MAIN_WORLD); 266 params.in_main_world = (world_type == MAIN_WORLD);
228 params.is_web_view = (process_type == WEB_VIEW_PROCESS); 267 params.is_web_view = (process_type == WEB_VIEW_PROCESS);
229 params.webview_src = webview_src; 268 params.webview_src = webview_src;
230 params.file_url = file_url; 269 params.file_url = file_url;
231 params.wants_result = (result_type == JSON_SERIALIZED_RESULT); 270 params.wants_result = (result_type == JSON_SERIALIZED_RESULT);
232 params.user_gesture = user_gesture; 271 params.user_gesture = user_gesture;
233 272
234 // Handler handles IPCs and deletes itself on completion. 273 // Handler handles IPCs and deletes itself on completion.
235 new Handler(script_observers_, web_contents_, params, frame_scope, callback); 274 new Handler(script_observers_, web_contents_, params, frame_scope, frame_id,
275 callback);
236 } 276 }
237 277
238 } // namespace extensions 278 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/script_executor.h ('k') | extensions/common/api/extension_types.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698