OLD | NEW |
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" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 const std::string& code, | 120 const std::string& code, |
120 ScriptExecutor::FrameScope frame_scope, | 121 ScriptExecutor::FrameScope frame_scope, |
121 UserScript::RunLocation run_at, | 122 UserScript::RunLocation run_at, |
122 ScriptExecutor::WorldType world_type, | 123 ScriptExecutor::WorldType world_type, |
123 ScriptExecutor::ProcessType process_type, | 124 ScriptExecutor::ProcessType process_type, |
124 const GURL& webview_src, | 125 const GURL& webview_src, |
125 const GURL& file_url, | 126 const GURL& file_url, |
126 bool user_gesture, | 127 bool user_gesture, |
127 ScriptExecutor::ResultType result_type, | 128 ScriptExecutor::ResultType result_type, |
128 const ExecuteScriptCallback& callback) { | 129 const ExecuteScriptCallback& callback) { |
| 130 scoped_ptr<ExtensionMsg_ExecuteCode_Params> params( |
| 131 new ExtensionMsg_ExecuteCode_Params()); |
| 132 params->request_id = next_request_id_++; |
| 133 params->extension_id = extension_id; |
| 134 params->is_javascript = (script_type == JAVASCRIPT); |
| 135 params->code = code; |
| 136 params->all_frames = (frame_scope == ALL_FRAMES); |
| 137 params->run_at = static_cast<int>(run_at); |
| 138 params->in_main_world = (world_type == MAIN_WORLD); |
| 139 params->is_web_view = (process_type == WEB_VIEW_PROCESS); |
| 140 params->webview_src = webview_src; |
| 141 params->file_url = file_url; |
| 142 params->wants_result = (result_type == JSON_SERIALIZED_RESULT); |
| 143 params->user_gesture = user_gesture; |
| 144 |
129 ActiveScriptController* active_script_controller = | 145 ActiveScriptController* active_script_controller = |
130 ActiveScriptController::GetForWebContents(web_contents_); | 146 ActiveScriptController::GetForWebContents(web_contents_); |
131 content::NavigationEntry* visible_entry = | 147 content::NavigationEntry* visible_entry = |
132 web_contents_->GetController().GetVisibleEntry(); | 148 web_contents_->GetController().GetVisibleEntry(); |
133 if (active_script_controller && visible_entry) { | 149 if (active_script_controller && visible_entry) { |
134 // TODO(rdevlin.cronin): Now, this is just a notification. Soon, it should | 150 // The base::Unretained(this) is safe, because this and the |
135 // block until the user gives the OK to execute. | 151 // ActiveScriptController are both attached to the TabHelper. Thus, if the |
136 active_script_controller->NotifyScriptExecuting(extension_id, | 152 // ActiveScriptController is still alive to invoke the callback, this is |
137 visible_entry->GetPageID()); | 153 // alive, too. |
| 154 active_script_controller->InjectScriptIfHasPermission( |
| 155 extension_id, |
| 156 visible_entry->GetPageID(), |
| 157 base::Closure(base::Bind(&ScriptExecutor::ExecuteScriptHelper, |
| 158 base::Unretained(this), |
| 159 base::Passed(params.Pass()), |
| 160 callback))); |
| 161 } else { |
| 162 ExecuteScriptHelper(params.Pass(), callback); |
138 } | 163 } |
| 164 } |
139 | 165 |
140 ExtensionMsg_ExecuteCode_Params params; | 166 void ScriptExecutor::ExecuteScriptHelper( |
141 params.request_id = next_request_id_++; | 167 scoped_ptr<ExtensionMsg_ExecuteCode_Params> params, |
142 params.extension_id = extension_id; | 168 const ExecuteScriptCallback& callback) { |
143 params.is_javascript = (script_type == JAVASCRIPT); | |
144 params.code = code; | |
145 params.all_frames = (frame_scope == ALL_FRAMES); | |
146 params.run_at = static_cast<int>(run_at); | |
147 params.in_main_world = (world_type == MAIN_WORLD); | |
148 params.is_web_view = (process_type == WEB_VIEW_PROCESS); | |
149 params.webview_src = webview_src; | |
150 params.file_url = file_url; | |
151 params.wants_result = (result_type == JSON_SERIALIZED_RESULT); | |
152 params.user_gesture = user_gesture; | |
153 | |
154 // Handler handles IPCs and deletes itself on completion. | 169 // Handler handles IPCs and deletes itself on completion. |
155 new Handler(script_observers_, web_contents_, params, callback); | 170 new Handler(script_observers_, web_contents_, *params, callback); |
156 } | 171 } |
157 | 172 |
158 } // namespace extensions | 173 } // namespace extensions |
OLD | NEW |