Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_all_frames_(scope == ScriptExecutor::ALL_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 SendExecuteCode(params, root_rfh_); |
| 49 base::Unretained(this), params)); | 56 |
| 50 } else { | 57 if (pending_render_frames_.empty()) |
| 51 SendExecuteCode(params, web_contents->GetMainFrame()); | 58 Finish(); |
| 52 } | |
| 53 } | 59 } |
| 54 | 60 |
| 55 private: | 61 private: |
| 56 // This class manages its own lifetime. | 62 // This class manages its own lifetime. |
| 57 ~Handler() override {} | 63 ~Handler() override {} |
| 58 | 64 |
| 59 // content::WebContentsObserver: | 65 // content::WebContentsObserver: |
| 60 void WebContentsDestroyed() override { Finish(); } | 66 void WebContentsDestroyed() override { Finish(); } |
| 61 | 67 |
| 62 bool OnMessageReceived(const IPC::Message& message, | 68 bool OnMessageReceived(const IPC::Message& message, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 85 if (pending_render_frames_.erase(render_frame_host) == 1 && | 91 if (pending_render_frames_.erase(render_frame_host) == 1 && |
| 86 pending_render_frames_.empty()) { | 92 pending_render_frames_.empty()) { |
| 87 Finish(); | 93 Finish(); |
| 88 } | 94 } |
| 89 } | 95 } |
| 90 | 96 |
| 91 // Sends an ExecuteCode message to the given frame host, and increments | 97 // Sends an ExecuteCode message to the given frame host, and increments |
| 92 // the number of pending messages. | 98 // the number of pending messages. |
| 93 void SendExecuteCode(const ExtensionMsg_ExecuteCode_Params& params, | 99 void SendExecuteCode(const ExtensionMsg_ExecuteCode_Params& params, |
| 94 content::RenderFrameHost* frame) { | 100 content::RenderFrameHost* frame) { |
| 101 if (!frame->IsRenderFrameLive()) | |
| 102 return; | |
| 95 pending_render_frames_.insert(frame); | 103 pending_render_frames_.insert(frame); |
| 96 frame->Send(new ExtensionMsg_ExecuteCode(frame->GetRoutingID(), params)); | 104 frame->Send(new ExtensionMsg_ExecuteCode(frame->GetRoutingID(), params)); |
| 105 | |
| 106 if (include_all_frames_) { | |
| 107 frame->ForEachChildFrame(base::Bind(&Handler::SendExecuteCode, | |
| 108 base::Unretained(this), params)); | |
| 109 } | |
| 97 } | 110 } |
| 98 | 111 |
| 99 // Handles the ExecuteCodeFinished message. | 112 // Handles the ExecuteCodeFinished message. |
| 100 void OnExecuteCodeFinished(content::RenderFrameHost* render_frame_host, | 113 void OnExecuteCodeFinished(content::RenderFrameHost* render_frame_host, |
| 101 int request_id, | 114 int request_id, |
| 102 const std::string& error, | 115 const std::string& error, |
| 103 const GURL& on_url, | 116 const GURL& on_url, |
| 104 const base::ListValue& result_list) { | 117 const base::ListValue& result_list) { |
| 105 DCHECK_EQ(request_id_, request_id); | 118 DCHECK_EQ(request_id_, request_id); |
| 106 DCHECK(!pending_render_frames_.empty()); | 119 DCHECK(!pending_render_frames_.empty()); |
| 107 bool erased = pending_render_frames_.erase(render_frame_host) == 1; | 120 bool erased = pending_render_frames_.erase(render_frame_host) == 1; |
| 108 DCHECK(erased); | 121 DCHECK(erased); |
| 109 bool is_main_frame = web_contents()->GetMainFrame() == render_frame_host; | 122 bool is_root_frame = root_rfh_ == render_frame_host; |
| 110 | 123 |
| 111 // Set the result, if there is one. | 124 // Set the result, if there is one. |
| 112 const base::Value* script_value = nullptr; | 125 const base::Value* script_value = nullptr; |
| 113 if (result_list.Get(0u, &script_value)) { | 126 if (result_list.Get(0u, &script_value)) { |
| 114 // If this is the main result, we put it at index 0. Otherwise, we just | 127 // If this is the main result, we put it at index 0. Otherwise, we just |
| 115 // append it at the end. | 128 // append it at the end. |
| 116 if (is_main_frame && !results_.empty()) | 129 if (is_root_frame && !results_.empty()) |
| 117 CHECK(results_.Insert(0u, script_value->DeepCopy())); | 130 CHECK(results_.Insert(0u, script_value->DeepCopy())); |
| 118 else | 131 else |
| 119 results_.Append(script_value->DeepCopy()); | 132 results_.Append(script_value->DeepCopy()); |
| 120 } | 133 } |
| 121 | 134 |
| 122 if (is_main_frame) { // Only use the main frame's error and url. | 135 if (is_root_frame) { // Only use the root frame's error and url. |
| 123 main_frame_error_ = error; | 136 root_frame_error_ = error; |
| 124 main_frame_url_ = on_url; | 137 root_frame_url_ = on_url; |
| 125 } | 138 } |
| 126 | 139 |
| 127 // Wait until the final request finishes before reporting back. | 140 // Wait until the final request finishes before reporting back. |
| 128 if (pending_render_frames_.empty()) | 141 if (pending_render_frames_.empty()) |
| 129 Finish(); | 142 Finish(); |
| 130 } | 143 } |
| 131 | 144 |
| 132 void Finish() { | 145 void Finish() { |
| 133 if (main_frame_url_.is_empty()) { | 146 if (root_frame_url_.is_empty()) { |
| 134 // We never finished the main frame injection. | 147 // We never finished the root frame injection. |
| 135 main_frame_error_ = kRendererDestroyed; | 148 root_frame_error_ = |
| 149 root_is_main_frame_ ? kRendererDestroyed : kFrameRemoved; | |
| 136 results_.Clear(); | 150 results_.Clear(); |
| 137 } | 151 } |
| 138 | 152 |
| 139 if (script_observers_.get() && main_frame_error_.empty() && | 153 if (script_observers_.get() && root_frame_error_.empty() && |
| 140 host_id_.type() == HostID::EXTENSIONS) { | 154 host_id_.type() == HostID::EXTENSIONS) { |
| 141 ScriptExecutionObserver::ExecutingScriptsMap id_map; | 155 ScriptExecutionObserver::ExecutingScriptsMap id_map; |
| 142 id_map[host_id_.id()] = std::set<std::string>(); | 156 id_map[host_id_.id()] = std::set<std::string>(); |
| 143 FOR_EACH_OBSERVER( | 157 FOR_EACH_OBSERVER( |
| 144 ScriptExecutionObserver, *script_observers_, | 158 ScriptExecutionObserver, *script_observers_, |
| 145 OnScriptsExecuted(web_contents(), id_map, main_frame_url_)); | 159 OnScriptsExecuted(web_contents(), id_map, root_frame_url_)); |
| 146 } | 160 } |
| 147 | 161 |
| 148 if (!callback_.is_null()) | 162 if (!callback_.is_null()) |
| 149 callback_.Run(main_frame_error_, main_frame_url_, results_); | 163 callback_.Run(root_frame_error_, root_frame_url_, results_); |
| 150 delete this; | 164 delete this; |
| 151 } | 165 } |
| 152 | 166 |
| 153 base::WeakPtr<base::ObserverList<ScriptExecutionObserver>> script_observers_; | 167 base::WeakPtr<base::ObserverList<ScriptExecutionObserver>> script_observers_; |
| 154 | 168 |
| 155 // The id of the host (the extension or the webui) doing the injection. | 169 // The id of the host (the extension or the webui) doing the injection. |
| 156 HostID host_id_; | 170 HostID host_id_; |
| 157 | 171 |
| 158 // The request id of the injection. | 172 // The request id of the injection. |
| 159 int request_id_; | 173 int request_id_; |
| 160 | 174 |
| 175 // Whether to inject in all frames. | |
|
Devlin
2016/01/25 19:36:40
nit: "inject in all child frames"
robwu
2016/01/26 11:03:02
Changed to "in all descendant frames of |root_rfh|
| |
| 176 bool include_all_frames_; | |
| 177 | |
| 178 // The frame (and optionally its descendant frames) where the injection will | |
| 179 // occur. | |
| 180 content::RenderFrameHost* root_rfh_; | |
| 181 | |
| 182 // Whether |root_rfh_| is the main frame of a tab. | |
| 183 bool root_is_main_frame_; | |
| 184 | |
| 161 // The hosts of the still-running injections. | 185 // The hosts of the still-running injections. |
| 162 std::set<content::RenderFrameHost*> pending_render_frames_; | 186 std::set<content::RenderFrameHost*> pending_render_frames_; |
| 163 | 187 |
| 164 // The results of the injection. | 188 // The results of the injection. |
| 165 base::ListValue results_; | 189 base::ListValue results_; |
| 166 | 190 |
| 167 // The error from injecting into the main frame. | 191 // The error from injecting into the root frame. |
| 168 std::string main_frame_error_; | 192 std::string root_frame_error_; |
| 169 | 193 |
| 170 // The url of the main frame. | 194 // The url of the root frame. |
| 171 GURL main_frame_url_; | 195 GURL root_frame_url_; |
| 172 | 196 |
| 173 // The callback to run after all injections complete. | 197 // The callback to run after all injections complete. |
| 174 ScriptExecutor::ExecuteScriptCallback callback_; | 198 ScriptExecutor::ExecuteScriptCallback callback_; |
| 175 | 199 |
| 176 DISALLOW_COPY_AND_ASSIGN(Handler); | 200 DISALLOW_COPY_AND_ASSIGN(Handler); |
| 177 }; | 201 }; |
| 178 | 202 |
| 179 } // namespace | 203 } // namespace |
| 180 | 204 |
| 181 ScriptExecutionObserver::~ScriptExecutionObserver() { | 205 ScriptExecutionObserver::~ScriptExecutionObserver() { |
| 182 } | 206 } |
| 183 | 207 |
| 184 ScriptExecutor::ScriptExecutor( | 208 ScriptExecutor::ScriptExecutor( |
| 185 content::WebContents* web_contents, | 209 content::WebContents* web_contents, |
| 186 base::ObserverList<ScriptExecutionObserver>* script_observers) | 210 base::ObserverList<ScriptExecutionObserver>* script_observers) |
| 187 : next_request_id_(0), | 211 : next_request_id_(0), |
| 188 web_contents_(web_contents), | 212 web_contents_(web_contents), |
| 189 script_observers_(script_observers) { | 213 script_observers_(script_observers) { |
| 190 CHECK(web_contents_); | 214 CHECK(web_contents_); |
| 191 } | 215 } |
| 192 | 216 |
| 193 ScriptExecutor::~ScriptExecutor() { | 217 ScriptExecutor::~ScriptExecutor() { |
| 194 } | 218 } |
| 195 | 219 |
| 196 void ScriptExecutor::ExecuteScript(const HostID& host_id, | 220 void ScriptExecutor::ExecuteScript(const HostID& host_id, |
| 197 ScriptExecutor::ScriptType script_type, | 221 ScriptExecutor::ScriptType script_type, |
| 198 const std::string& code, | 222 const std::string& code, |
| 199 ScriptExecutor::FrameScope frame_scope, | 223 ScriptExecutor::FrameScope frame_scope, |
| 224 int frame_id, | |
| 200 ScriptExecutor::MatchAboutBlank about_blank, | 225 ScriptExecutor::MatchAboutBlank about_blank, |
| 201 UserScript::RunLocation run_at, | 226 UserScript::RunLocation run_at, |
| 202 ScriptExecutor::WorldType world_type, | 227 ScriptExecutor::WorldType world_type, |
| 203 ScriptExecutor::ProcessType process_type, | 228 ScriptExecutor::ProcessType process_type, |
| 204 const GURL& webview_src, | 229 const GURL& webview_src, |
| 205 const GURL& file_url, | 230 const GURL& file_url, |
| 206 bool user_gesture, | 231 bool user_gesture, |
| 207 ScriptExecutor::ResultType result_type, | 232 ScriptExecutor::ResultType result_type, |
| 208 const ExecuteScriptCallback& callback) { | 233 const ExecuteScriptCallback& callback) { |
| 209 if (host_id.type() == HostID::EXTENSIONS) { | 234 if (host_id.type() == HostID::EXTENSIONS) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 225 params.match_about_blank = (about_blank == MATCH_ABOUT_BLANK); | 250 params.match_about_blank = (about_blank == MATCH_ABOUT_BLANK); |
| 226 params.run_at = static_cast<int>(run_at); | 251 params.run_at = static_cast<int>(run_at); |
| 227 params.in_main_world = (world_type == MAIN_WORLD); | 252 params.in_main_world = (world_type == MAIN_WORLD); |
| 228 params.is_web_view = (process_type == WEB_VIEW_PROCESS); | 253 params.is_web_view = (process_type == WEB_VIEW_PROCESS); |
| 229 params.webview_src = webview_src; | 254 params.webview_src = webview_src; |
| 230 params.file_url = file_url; | 255 params.file_url = file_url; |
| 231 params.wants_result = (result_type == JSON_SERIALIZED_RESULT); | 256 params.wants_result = (result_type == JSON_SERIALIZED_RESULT); |
| 232 params.user_gesture = user_gesture; | 257 params.user_gesture = user_gesture; |
| 233 | 258 |
| 234 // Handler handles IPCs and deletes itself on completion. | 259 // Handler handles IPCs and deletes itself on completion. |
| 235 new Handler(script_observers_, web_contents_, params, frame_scope, callback); | 260 new Handler(script_observers_, web_contents_, params, frame_scope, frame_id, |
| 261 callback); | |
| 236 } | 262 } |
| 237 | 263 |
| 238 } // namespace extensions | 264 } // namespace extensions |
| OLD | NEW |