Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: extensions/browser/api/execute_code_function.h

Issue 2630753003: Separate validation failures from other failures in ExecuteCodeFunction. (Closed)
Patch Set: . Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.
Devlin 2017/01/17 18:52:09 We should describe the importance of these. In pa
lazyboy 2017/01/18 02:29:10 Done.
33 VALIDATION_FAILURE,
34 // Failure other than validation.
35 FAILURE,
36 SUCCESS
37 };
38
39 // Initializes |details_| and other variables if they haven't already been.
40 // Returns whether or not it succeeded. Failure can be tolerable (FAILURE), or
41 // fatal (VALIDATION_FAILURE).
42 virtual InitResult Init() = 0;
32 virtual bool ShouldInsertCSS() const = 0; 43 virtual bool ShouldInsertCSS() const = 0;
33 virtual bool CanExecuteScriptOnPage() = 0; 44 virtual bool CanExecuteScriptOnPage() = 0;
34 virtual ScriptExecutor* GetScriptExecutor() = 0; 45 virtual ScriptExecutor* GetScriptExecutor() = 0;
35 virtual bool IsWebView() const = 0; 46 virtual bool IsWebView() const = 0;
36 virtual const GURL& GetWebViewSrc() const = 0; 47 virtual const GURL& GetWebViewSrc() const = 0;
37 virtual void OnExecuteCodeFinished(const std::string& error, 48 virtual void OnExecuteCodeFinished(const std::string& error,
38 const GURL& on_url, 49 const GURL& on_url,
39 const base::ListValue& result); 50 const base::ListValue& result);
40 51
41 virtual bool LoadFile(const std::string& file); 52 virtual bool LoadFile(const std::string& file);
42 53
43 // Called when contents from the loaded file have been localized. 54 // Called when contents from the loaded file have been localized.
44 void DidLoadAndLocalizeFile(const std::string& file, 55 void DidLoadAndLocalizeFile(const std::string& file,
45 bool success, 56 bool success,
46 std::unique_ptr<std::string> data); 57 std::unique_ptr<std::string> data);
47 58
48 const HostID& host_id() const { return host_id_; } 59 const HostID& host_id() const { return host_id_; }
49 void set_host_id(const HostID& host_id) { host_id_ = host_id; } 60 void set_host_id(const HostID& host_id) { host_id_ = host_id; }
50 61
62 InitResult set_init_result(InitResult init_result) {
63 init_result_ = init_result;
64 return init_result_.value();
65 }
66 InitResult set_init_result(InitResult init_result, const std::string& error) {
Devlin 2017/01/17 18:52:09 This can only be called with FAILURE, right? Can
lazyboy 2017/01/18 02:29:10 Assigned directly via renamed method set_init_resu
67 init_error_ = error;
68 return set_init_result(init_result);
69 }
70
51 // The injection details. 71 // The injection details.
52 std::unique_ptr<api::extension_types::InjectDetails> details_; 72 std::unique_ptr<api::extension_types::InjectDetails> details_;
73 base::Optional<InitResult> init_result_;
74 // Set iff |init_result_| == FAILURE, holds the error string.
75 base::Optional<std::string> init_error_;
53 76
54 private: 77 private:
55 // Retrieves the file url for the given |extension_path| and optionally 78 // Retrieves the file url for the given |extension_path| and optionally
56 // localizes |data|. 79 // localizes |data|.
57 // Localization depends on whether |might_require_localization| was specified. 80 // Localization depends on whether |might_require_localization| was specified.
58 // Only CSS file content needs to be localized. 81 // Only CSS file content needs to be localized.
59 void GetFileURLAndMaybeLocalizeOnFileThread( 82 void GetFileURLAndMaybeLocalizeOnFileThread(
60 const std::string& extension_id, 83 const std::string& extension_id,
61 const base::FilePath& extension_path, 84 const base::FilePath& extension_path,
62 const std::string& extension_default_locale, 85 const std::string& extension_default_locale,
(...skipping 24 matching lines...) Expand all
87 110
88 // The ID of the injection host. 111 // The ID of the injection host.
89 HostID host_id_; 112 HostID host_id_;
90 113
91 DISALLOW_COPY_AND_ASSIGN(ExecuteCodeFunction); 114 DISALLOW_COPY_AND_ASSIGN(ExecuteCodeFunction);
92 }; 115 };
93 116
94 } // namespace extensions 117 } // namespace extensions
95 118
96 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_ 119 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698