| 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 #ifndef CHROME_BROWSER_EXTENSIONS_API_TABS_EXECUTE_CODE_IN_TAB_FUNCTION_H__ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_TABS_EXECUTE_CODE_IN_TAB_FUNCTION_H__ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_TABS_EXECUTE_CODE_IN_TAB_FUNCTION_H__ | 6 #define CHROME_BROWSER_EXTENSIONS_API_TABS_EXECUTE_CODE_IN_TAB_FUNCTION_H__ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "chrome/browser/extensions/extension_function.h" | 10 #include "chrome/browser/extensions/extension_function.h" |
| 11 #include "chrome/common/extensions/extension_resource.h" | 11 #include "chrome/common/extensions/extension_resource.h" |
| 12 #include "chrome/common/extensions/user_script.h" | 12 #include "chrome/common/extensions/user_script.h" |
| 13 | 13 |
| 14 namespace extensions { |
| 15 namespace api { |
| 16 namespace tabs { |
| 17 struct InjectDetails; |
| 18 } // namespace tabs |
| 19 } // namespace api |
| 20 } // namespace extensions |
| 21 |
| 14 // Implement API call tabs.executeScript and tabs.insertCSS. | 22 // Implement API call tabs.executeScript and tabs.insertCSS. |
| 15 class ExecuteCodeInTabFunction : public AsyncExtensionFunction { | 23 class ExecuteCodeInTabFunction : public AsyncExtensionFunction { |
| 16 public: | 24 public: |
| 17 ExecuteCodeInTabFunction(); | 25 ExecuteCodeInTabFunction(); |
| 18 | 26 |
| 19 protected: | 27 protected: |
| 20 virtual ~ExecuteCodeInTabFunction(); | 28 virtual ~ExecuteCodeInTabFunction(); |
| 21 | 29 |
| 22 // ExtensionFunction: | 30 // ExtensionFunction: |
| 31 virtual bool HasPermission() OVERRIDE; |
| 23 virtual bool RunImpl() OVERRIDE; | 32 virtual bool RunImpl() OVERRIDE; |
| 24 | 33 |
| 25 // Message handler. | 34 // Message handler. |
| 26 virtual void OnExecuteCodeFinished(bool success, | 35 virtual void OnExecuteCodeFinished(bool success, |
| 27 int32 page_id, | 36 int32 page_id, |
| 28 const std::string& error, | 37 const std::string& error, |
| 29 const ListValue& script_result); | 38 const ListValue& script_result); |
| 30 | 39 |
| 31 private: | 40 private: |
| 41 // Initialize the |execute_tab_id_| and |details_| if they haven't already |
| 42 // been. Returns whether initialization was successful. |
| 43 bool Init(); |
| 44 |
| 32 // Called when contents from the file whose path is specified in JSON | 45 // Called when contents from the file whose path is specified in JSON |
| 33 // arguments has been loaded. | 46 // arguments has been loaded. |
| 34 void DidLoadFile(bool success, const std::string& data); | 47 void DidLoadFile(bool success, const std::string& data); |
| 35 | 48 |
| 36 // Runs on FILE thread. Loads message bundles for the extension and | 49 // Runs on FILE thread. Loads message bundles for the extension and |
| 37 // localizes the CSS data. Calls back DidLoadAndLocalizeFile on the UI thread. | 50 // localizes the CSS data. Calls back DidLoadAndLocalizeFile on the UI thread. |
| 38 void LocalizeCSS( | 51 void LocalizeCSS( |
| 39 const std::string& data, | 52 const std::string& data, |
| 40 const std::string& extension_id, | 53 const std::string& extension_id, |
| 41 const FilePath& extension_path, | 54 const FilePath& extension_path, |
| 42 const std::string& extension_default_locale); | 55 const std::string& extension_default_locale); |
| 43 | 56 |
| 44 // Called when contents from the loaded file have been localized. | 57 // Called when contents from the loaded file have been localized. |
| 45 void DidLoadAndLocalizeFile(bool success, const std::string& data); | 58 void DidLoadAndLocalizeFile(bool success, const std::string& data); |
| 46 | 59 |
| 47 // Run in UI thread. Code string contains the code to be executed. Returns | 60 // Run in UI thread. Code string contains the code to be executed. Returns |
| 48 // true on success. If true is returned, this does an AddRef. | 61 // true on success. If true is returned, this does an AddRef. |
| 49 bool Execute(const std::string& code_string); | 62 bool Execute(const std::string& code_string); |
| 50 | 63 |
| 51 // Id of tab which executes code. | 64 // Id of tab which executes code. |
| 52 int execute_tab_id_; | 65 int execute_tab_id_; |
| 53 | 66 |
| 67 // The injection details. |
| 68 scoped_ptr<extensions::api::tabs::InjectDetails> details_; |
| 69 |
| 54 // Contains extension resource built from path of file which is | 70 // Contains extension resource built from path of file which is |
| 55 // specified in JSON arguments. | 71 // specified in JSON arguments. |
| 56 ExtensionResource resource_; | 72 ExtensionResource resource_; |
| 57 | |
| 58 // If all_frames_ is true, script or CSS text would be injected | |
| 59 // to all frames; Otherwise only injected to top main frame. | |
| 60 bool all_frames_; | |
| 61 | |
| 62 // The intended time to run the script. | |
| 63 extensions::UserScript::RunLocation run_at_; | |
| 64 }; | 73 }; |
| 65 | 74 |
| 66 class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction { | 75 class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction { |
| 67 private: | 76 private: |
| 68 virtual ~TabsExecuteScriptFunction() {} | 77 virtual ~TabsExecuteScriptFunction() {} |
| 69 | 78 |
| 70 virtual void OnExecuteCodeFinished(bool success, int32 page_id, | 79 virtual void OnExecuteCodeFinished(bool success, int32 page_id, |
| 71 const std::string& error, | 80 const std::string& error, |
| 72 const ListValue& script_result) OVERRIDE; | 81 const ListValue& script_result) OVERRIDE; |
| 73 | 82 |
| 74 DECLARE_EXTENSION_FUNCTION_NAME("tabs.executeScript") | 83 DECLARE_EXTENSION_FUNCTION_NAME("tabs.executeScript") |
| 75 }; | 84 }; |
| 76 | 85 |
| 77 class TabsInsertCSSFunction : public ExecuteCodeInTabFunction { | 86 class TabsInsertCSSFunction : public ExecuteCodeInTabFunction { |
| 78 private: | 87 private: |
| 79 virtual ~TabsInsertCSSFunction() {} | 88 virtual ~TabsInsertCSSFunction() {} |
| 80 | 89 |
| 81 DECLARE_EXTENSION_FUNCTION_NAME("tabs.insertCSS") | 90 DECLARE_EXTENSION_FUNCTION_NAME("tabs.insertCSS") |
| 82 }; | 91 }; |
| 83 | 92 |
| 84 #endif // CHROME_BROWSER_EXTENSIONS_API_TABS_EXECUTE_CODE_IN_TAB_FUNCTION_H__ | 93 #endif // CHROME_BROWSER_EXTENSIONS_API_TABS_EXECUTE_CODE_IN_TAB_FUNCTION_H__ |
| OLD | NEW |