| 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 && |
| 134 // TODO(rdevlin.cronin): Now, this is just a notification. Soon, it should | 150 visible_entry && |
| 135 // block until the user gives the OK to execute. | 151 active_script_controller->RequiresUserConsentForScriptInjection( |
| 136 active_script_controller->NotifyScriptExecuting(extension_id, | 152 extension_id, visible_entry->GetPageID())) { |
| 137 visible_entry->GetPageID()); | 153 // The base::Unretained(this) is safe, because this and the |
| 154 // ActiveScriptController are both attached to the TabHelper. Thus, if the |
| 155 // ActiveScriptController is still alive to invoke the callback, this is |
| 156 // alive, too. |
| 157 active_script_controller->RequestScriptInjection( |
| 158 extension_id, |
| 159 base::Closure(base::Bind(&ScriptExecutor::ExecuteScriptHelper, |
| 160 base::Unretained(this), |
| 161 base::Passed(params.Pass()), |
| 162 callback))); |
| 163 } else { |
| 164 ExecuteScriptHelper(params.Pass(), callback); |
| 138 } | 165 } |
| 166 } |
| 139 | 167 |
| 140 ExtensionMsg_ExecuteCode_Params params; | 168 void ScriptExecutor::ExecuteScriptHelper( |
| 141 params.request_id = next_request_id_++; | 169 scoped_ptr<ExtensionMsg_ExecuteCode_Params> params, |
| 142 params.extension_id = extension_id; | 170 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. | 171 // Handler handles IPCs and deletes itself on completion. |
| 155 new Handler(script_observers_, web_contents_, params, callback); | 172 new Handler(script_observers_, web_contents_, *params, callback); |
| 156 } | 173 } |
| 157 | 174 |
| 158 } // namespace extensions | 175 } // namespace extensions |
| OLD | NEW |