| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_ |
| 6 #define EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_ | 6 #define EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/optional.h" |
| 9 #include "extensions/browser/extension_function.h" | 10 #include "extensions/browser/extension_function.h" |
| 10 #include "extensions/browser/script_executor.h" | 11 #include "extensions/browser/script_executor.h" |
| 11 #include "extensions/common/api/extension_types.h" | 12 #include "extensions/common/api/extension_types.h" |
| 12 #include "extensions/common/host_id.h" | 13 #include "extensions/common/host_id.h" |
| 13 | 14 |
| 14 namespace extensions { | 15 namespace extensions { |
| 15 | 16 |
| 16 // Base class for javascript code injection. | 17 // Base class for javascript code injection. |
| 17 // This is used by both chrome.webview.executeScript and | 18 // This is used by both chrome.webview.executeScript and |
| 18 // chrome.tabs.executeScript. | 19 // chrome.tabs.executeScript. |
| 19 class ExecuteCodeFunction : public AsyncExtensionFunction { | 20 class ExecuteCodeFunction : public AsyncExtensionFunction { |
| 20 public: | 21 public: |
| 21 ExecuteCodeFunction(); | 22 ExecuteCodeFunction(); |
| 22 | 23 |
| 23 protected: | 24 protected: |
| 24 ~ExecuteCodeFunction() override; | 25 ~ExecuteCodeFunction() override; |
| 25 | 26 |
| 26 // ExtensionFunction implementation. | 27 // ExtensionFunction implementation. |
| 27 bool HasPermission() override; | 28 bool HasPermission() override; |
| 28 bool RunAsync() override; | 29 bool RunAsync() override; |
| 29 | 30 |
| 30 // Initialize |details_| if it hasn't already been. | 31 enum InitResult { |
| 31 virtual bool Init() = 0; | 32 // ExtensionFunction validation failure. |
| 33 // Warning: Validation failures kill the renderer and should only be used |
| 34 // for circumstances that should never happen. |
| 35 VALIDATION_FAILURE, |
| 36 // Failure other than validation. |
| 37 // Failures return an error to the extension function and should be used |
| 38 // when an error situation occurs: e.g. trying to execute script during |
| 39 // browser shutdown. |
| 40 FAILURE, |
| 41 SUCCESS |
| 42 }; |
| 43 |
| 44 // Initializes |details_| and other variables if they haven't already been. |
| 45 // Returns whether or not it succeeded. Failure can be tolerable (FAILURE), or |
| 46 // fatal (VALIDATION_FAILURE). |
| 47 virtual InitResult Init() = 0; |
| 32 virtual bool ShouldInsertCSS() const = 0; | 48 virtual bool ShouldInsertCSS() const = 0; |
| 33 virtual bool CanExecuteScriptOnPage() = 0; | 49 virtual bool CanExecuteScriptOnPage() = 0; |
| 34 virtual ScriptExecutor* GetScriptExecutor() = 0; | 50 virtual ScriptExecutor* GetScriptExecutor() = 0; |
| 35 virtual bool IsWebView() const = 0; | 51 virtual bool IsWebView() const = 0; |
| 36 virtual const GURL& GetWebViewSrc() const = 0; | 52 virtual const GURL& GetWebViewSrc() const = 0; |
| 37 virtual void OnExecuteCodeFinished(const std::string& error, | 53 virtual void OnExecuteCodeFinished(const std::string& error, |
| 38 const GURL& on_url, | 54 const GURL& on_url, |
| 39 const base::ListValue& result); | 55 const base::ListValue& result); |
| 40 | 56 |
| 41 virtual bool LoadFile(const std::string& file); | 57 virtual bool LoadFile(const std::string& file); |
| 42 | 58 |
| 43 // Called when contents from the loaded file have been localized. | 59 // Called when contents from the loaded file have been localized. |
| 44 void DidLoadAndLocalizeFile(const std::string& file, | 60 void DidLoadAndLocalizeFile(const std::string& file, |
| 45 bool success, | 61 bool success, |
| 46 std::unique_ptr<std::string> data); | 62 std::unique_ptr<std::string> data); |
| 47 | 63 |
| 48 const HostID& host_id() const { return host_id_; } | 64 const HostID& host_id() const { return host_id_; } |
| 49 void set_host_id(const HostID& host_id) { host_id_ = host_id; } | 65 void set_host_id(const HostID& host_id) { host_id_ = host_id; } |
| 50 | 66 |
| 67 InitResult set_init_result(InitResult init_result) { |
| 68 init_result_ = init_result; |
| 69 return init_result_.value(); |
| 70 } |
| 71 InitResult set_init_result_error(const std::string& error) { |
| 72 init_error_ = error; |
| 73 return set_init_result(FAILURE); |
| 74 } |
| 75 |
| 51 // The injection details. | 76 // The injection details. |
| 52 std::unique_ptr<api::extension_types::InjectDetails> details_; | 77 std::unique_ptr<api::extension_types::InjectDetails> details_; |
| 78 base::Optional<InitResult> init_result_; |
| 79 // Set iff |init_result_| == FAILURE, holds the error string. |
| 80 base::Optional<std::string> init_error_; |
| 53 | 81 |
| 54 private: | 82 private: |
| 55 // Retrieves the file url for the given |extension_path| and optionally | 83 // Retrieves the file url for the given |extension_path| and optionally |
| 56 // localizes |data|. | 84 // localizes |data|. |
| 57 // Localization depends on whether |might_require_localization| was specified. | 85 // Localization depends on whether |might_require_localization| was specified. |
| 58 // Only CSS file content needs to be localized. | 86 // Only CSS file content needs to be localized. |
| 59 void GetFileURLAndMaybeLocalizeOnFileThread( | 87 void GetFileURLAndMaybeLocalizeOnFileThread( |
| 60 const std::string& extension_id, | 88 const std::string& extension_id, |
| 61 const base::FilePath& extension_path, | 89 const base::FilePath& extension_path, |
| 62 const std::string& extension_default_locale, | 90 const std::string& extension_default_locale, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 87 | 115 |
| 88 // The ID of the injection host. | 116 // The ID of the injection host. |
| 89 HostID host_id_; | 117 HostID host_id_; |
| 90 | 118 |
| 91 DISALLOW_COPY_AND_ASSIGN(ExecuteCodeFunction); | 119 DISALLOW_COPY_AND_ASSIGN(ExecuteCodeFunction); |
| 92 }; | 120 }; |
| 93 | 121 |
| 94 } // namespace extensions | 122 } // namespace extensions |
| 95 | 123 |
| 96 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_ | 124 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_ |
| OLD | NEW |