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

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

Issue 286003004: Block tabs.executeScript() from executing until user grants permission (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Epic master rebase Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/extensions/script_executor.h" 5 #include "chrome/browser/extensions/script_executor.h"
6 6
7 #include "base/bind.h"
7 #include "base/callback.h" 8 #include "base/callback.h"
8 #include "base/logging.h" 9 #include "base/logging.h"
9 #include "base/pickle.h" 10 #include "base/pickle.h"
10 #include "chrome/browser/extensions/active_script_controller.h" 11 #include "chrome/browser/extensions/active_script_controller.h"
11 #include "chrome/browser/extensions/tab_helper.h" 12 #include "chrome/browser/extensions/tab_helper.h"
12 #include "content/public/browser/navigation_controller.h" 13 #include "content/public/browser/navigation_controller.h"
13 #include "content/public/browser/navigation_entry.h" 14 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/browser/render_view_host.h" 15 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_observer.h" 17 #include "content/public/browser/web_contents_observer.h"
18 #include "extensions/browser/extension_registry.h"
17 #include "extensions/common/extension_messages.h" 19 #include "extensions/common/extension_messages.h"
18 #include "ipc/ipc_message.h" 20 #include "ipc/ipc_message.h"
19 #include "ipc/ipc_message_macros.h" 21 #include "ipc/ipc_message_macros.h"
20 22
21 namespace base { 23 namespace base {
22 class ListValue; 24 class ListValue;
23 } // namespace base 25 } // namespace base
24 26
25 namespace extensions { 27 namespace extensions {
26 28
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 ScriptExecutor::FrameScope frame_scope, 122 ScriptExecutor::FrameScope frame_scope,
121 ScriptExecutor::MatchAboutBlank about_blank, 123 ScriptExecutor::MatchAboutBlank about_blank,
122 UserScript::RunLocation run_at, 124 UserScript::RunLocation run_at,
123 ScriptExecutor::WorldType world_type, 125 ScriptExecutor::WorldType world_type,
124 ScriptExecutor::ProcessType process_type, 126 ScriptExecutor::ProcessType process_type,
125 const GURL& webview_src, 127 const GURL& webview_src,
126 const GURL& file_url, 128 const GURL& file_url,
127 bool user_gesture, 129 bool user_gesture,
128 ScriptExecutor::ResultType result_type, 130 ScriptExecutor::ResultType result_type,
129 const ExecuteScriptCallback& callback) { 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);
136 if (!extension)
137 return;
138
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();
145 if (!visible_entry)
146 return;
147
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);
153 params->code = code;
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;
163
130 ActiveScriptController* active_script_controller = 164 ActiveScriptController* active_script_controller =
131 ActiveScriptController::GetForWebContents(web_contents_); 165 ActiveScriptController::GetForWebContents(web_contents_);
132 content::NavigationEntry* visible_entry = 166 if (active_script_controller &&
133 web_contents_->GetController().GetVisibleEntry(); 167 active_script_controller->RequiresUserConsentForScriptInjection(
134 if (active_script_controller && visible_entry) { 168 extension)) {
135 // TODO(rdevlin.cronin): Now, this is just a notification. Soon, it should 169 // The base::Unretained(this) is safe, because this and the
136 // block until the user gives the OK to execute. 170 // ActiveScriptController are both attached to the TabHelper. Thus, if the
137 active_script_controller->NotifyScriptExecuting(extension_id, 171 // ActiveScriptController is still alive to invoke the callback, this is
138 visible_entry->GetPageID()); 172 // alive, too.
173 active_script_controller->RequestScriptInjection(
174 extension,
175 visible_entry->GetPageID(),
176 base::Closure(base::Bind(&ScriptExecutor::ExecuteScriptHelper,
177 base::Unretained(this),
178 base::Passed(params.Pass()),
179 callback)));
180 } else {
181 ExecuteScriptHelper(params.Pass(), callback);
139 } 182 }
140 ExtensionMsg_ExecuteCode_Params params; 183 }
141 params.request_id = next_request_id_++;
142 params.extension_id = extension_id;
143 params.is_javascript = (script_type == JAVASCRIPT);
144 params.code = code;
145 params.all_frames = (frame_scope == ALL_FRAMES);
146 params.match_about_blank = (about_blank == MATCH_ABOUT_BLANK);
147 params.run_at = static_cast<int>(run_at);
148 params.in_main_world = (world_type == MAIN_WORLD);
149 params.is_web_view = (process_type == WEB_VIEW_PROCESS);
150 params.webview_src = webview_src;
151 params.file_url = file_url;
152 params.wants_result = (result_type == JSON_SERIALIZED_RESULT);
153 params.user_gesture = user_gesture;
154 184
185 void ScriptExecutor::ExecuteScriptHelper(
186 scoped_ptr<ExtensionMsg_ExecuteCode_Params> params,
187 const ExecuteScriptCallback& callback) {
155 // Handler handles IPCs and deletes itself on completion. 188 // Handler handles IPCs and deletes itself on completion.
156 new Handler(script_observers_, web_contents_, params, callback); 189 new Handler(script_observers_, web_contents_, *params, callback);
157 } 190 }
158 191
159 } // namespace extensions 192 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698