1 // Copyright (c) 2011 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_EXECUTE_CODE_IN_TAB_FUNCTION_H__ 6 #define CHROME_BROWSER_EXTENSIONS_EXECUTE_CODE_IN_TAB_FUNCTION_H__ 7 #pragma once 8 9 #include <string> 10 11 #include "chrome/browser/extensions/extension_function.h" 12 #include "chrome/common/extensions/extension_resource.h" 13 #include "content/browser/tab_contents/tab_contents_observer.h" 14 15 // Implement API call tabs.executeScript and tabs.insertCSS. 16 class ExecuteCodeInTabFunction : public AsyncExtensionFunction, 17 public TabContentsObserver { 18 public: 19 ExecuteCodeInTabFunction(); 20 virtual ~ExecuteCodeInTabFunction(); 21 22 private: 23 virtual bool RunImpl(); 24 25 // TabContentsObserver overrides. 26 virtual bool OnMessageReceived(const IPC::Message& message); 27 28 // Message handler. 29 void OnExecuteCodeFinished(int request_id, bool success, 30 const std::string& error); 31 32 // Called when contents from the file whose path is specified in JSON 33 // arguments has been loaded. 34 void DidLoadFile(bool success, const std::string& data); 35 36 // Run in UI thread. Code string contains the code to be executed. Returns 37 // true on success. If true is returned, this does an AddRef. 38 bool Execute(const std::string& code_string); 39 40 TabContentsObserver::Registrar registrar_; 41 42 // Id of tab which executes code. 43 int execute_tab_id_; 44 45 // Contains extension resource built from path of file which is 46 // specified in JSON arguments. 47 ExtensionResource resource_; 48 49 // If all_frames_ is true, script or CSS text would be injected 50 // to all frames; Otherwise only injected to top main frame. 51 bool all_frames_; 52 }; 53 54 class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction { 55 DECLARE_EXTENSION_FUNCTION_NAME("tabs.executeScript") 56 }; 57 58 class TabsInsertCSSFunction : public ExecuteCodeInTabFunction { 59 DECLARE_EXTENSION_FUNCTION_NAME("tabs.insertCSS") 60 }; 61 62 #endif // CHROME_BROWSER_EXTENSIONS_EXECUTE_CODE_IN_TAB_FUNCTION_H__ 63