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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ 6 #define CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ 7 8 #include <string> 9 10 #include "base/callback_forward.h" 11 #include "base/observer_list.h" 12 #include "chrome/browser/extensions/tab_helper.h" 13 #include "extensions/common/user_script.h" 14 15 class GURL; 16 struct ExtensionMsg_ExecuteCode_Params; 17 18 namespace base { 19 class ListValue; 20 } // namespace base 21 22 namespace content { 23 class WebContents; 24 } 25 26 namespace extensions { 27 28 // Interface for executing extension content scripts (e.g. executeScript) as 29 // described by the ExtensionMsg_ExecuteCode_Params IPC, and notifying the 30 // caller when responded with ExtensionHostMsg_ExecuteCodeFinished. 31 class ScriptExecutor { 32 public: 33 ScriptExecutor( 34 content::WebContents* web_contents, 35 // |script_observers| is assumed to be owned by |this|'s owner, and in 36 // such a way that |this| is destroyed first. 37 ObserverList<TabHelper::ScriptExecutionObserver>* script_observers); 38 39 ~ScriptExecutor(); 40 41 // The type of script being injected. 42 enum ScriptType { 43 JAVASCRIPT, 44 CSS, 45 }; 46 47 // The scope of the script injection across the frames. 48 enum FrameScope { 49 TOP_FRAME, 50 ALL_FRAMES, 51 }; 52 53 // Whether to insert the script in about: frames when its origin matches 54 // the extension's host permissions. 55 enum MatchAboutBlank { 56 DONT_MATCH_ABOUT_BLANK, 57 MATCH_ABOUT_BLANK, 58 }; 59 60 // The type of world to inject into (main world, or its own isolated world). 61 enum WorldType { 62 MAIN_WORLD, 63 ISOLATED_WORLD, 64 }; 65 66 // The type of process the target is. 67 enum ProcessType { 68 DEFAULT_PROCESS, 69 WEB_VIEW_PROCESS, 70 }; 71 72 // The type of result the caller is interested in. 73 enum ResultType { 74 NO_RESULT, 75 JSON_SERIALIZED_RESULT, 76 }; 77 78 // Callback from ExecuteScript. The arguments are (error, on_page_id, on_url, 79 // result). Success is implied by an empty error. 80 typedef base::Callback<void(const std::string&, int32, const GURL&, 81 const base::ListValue&)> 82 ExecuteScriptCallback; 83 84 // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in 85 // extension_messages.h (request_id is populated automatically). 86 // 87 // |callback| will always be called even if the IPC'd renderer is destroyed 88 // before a response is received (in this case the callback will be with a 89 // failure and appropriate error message). 90 void ExecuteScript(const std::string& extension_id, 91 ScriptType script_type, 92 const std::string& code, 93 FrameScope frame_scope, 94 MatchAboutBlank match_about_blank, 95 UserScript::RunLocation run_at, 96 WorldType world_type, 97 ProcessType process_type, 98 const GURL& webview_src, 99 const GURL& file_url, 100 bool user_gesture, 101 ResultType result_type, 102 const ExecuteScriptCallback& callback); 103 104 private: 105 // Called upon a request being given to execute the script. 106 void ExecuteScriptHelper(scoped_ptr<ExtensionMsg_ExecuteCode_Params> params, 107 const ExecuteScriptCallback& callback); 108 109 // The next value to use for request_id in ExtensionMsg_ExecuteCode_Params. 110 int next_request_id_; 111 112 content::WebContents* web_contents_; 113 114 ObserverList<TabHelper::ScriptExecutionObserver>* script_observers_; 115 }; 116 117 } // namespace extensions 118 119 #endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_ 120