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

Side by Side Diff: chrome/browser/extensions/extension_install_checker.h

Issue 2751013002: Simplify ExtensionInstallChecker into a single-use class (Closed)
Patch Set: todo Created 3 years, 9 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_EXTENSION_INSTALL_CHECKER_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
16 #include "extensions/browser/blacklist_state.h" 16 #include "extensions/browser/blacklist_state.h"
17 #include "extensions/common/extension.h" 17 #include "extensions/common/extension.h"
18 18
19 class Profile; 19 class Profile;
20 20
21 namespace extensions { 21 namespace extensions {
22 22
23 class RequirementsChecker; 23 class RequirementsChecker;
24 24
25 // Performs common checks for an extension. Extensions that violate these checks 25 // Performs common checks for validating whether an extension may be installed.
26 // would be disabled or not even installed. 26 // This class should be Start()-ed at most once.
27 class ExtensionInstallChecker { 27 class ExtensionInstallChecker {
28 public: 28 public:
29 // Called when checks are complete. The returned value is a bitmask of 29 // Called when checks are complete. The returned value is a bitmask of
30 // failed checks. 30 // failed checks.
31 typedef base::Callback<void(int)> Callback; 31 typedef base::Callback<void(int)> Callback;
32 32
33 enum CheckType { 33 enum CheckType {
34 // Check the blacklist state of the extension. 34 // Check the blacklist state of the extension.
35 CHECK_BLACKLIST = 1 << 0, 35 CHECK_BLACKLIST = 1 << 0,
36 // Check whether the extension has requirement errors. 36 // Check whether the extension has requirement errors.
37 CHECK_REQUIREMENTS = 1 << 1, 37 CHECK_REQUIREMENTS = 1 << 1,
38 // Check whether the extension can be installed and loaded, according to 38 // Check whether the extension can be installed and loaded, according to
39 // management policies. 39 // management policies.
40 CHECK_MANAGEMENT_POLICY = 1 << 2, 40 CHECK_MANAGEMENT_POLICY = 1 << 2,
41 // Perform all checks. 41 // Perform all checks.
42 CHECK_ALL = (1 << 3) - 1 42 CHECK_ALL = (1 << 3) - 1
43 }; 43 };
44 44
45 explicit ExtensionInstallChecker(Profile* profile); 45 // |enabled_checks| is a bitmask of CheckTypes to run.
46 // If |fail_fast| is true, the callback to Start() will be invoked once any
47 // check fails. Otherwise it will be invoked when all checks have completed.
48 ExtensionInstallChecker(Profile* profile,
49 scoped_refptr<const Extension> extension,
50 int enabled_checks,
51 bool fail_fast);
46 virtual ~ExtensionInstallChecker(); 52 virtual ~ExtensionInstallChecker();
47 53
48 // Start a set of checks. |enabled_checks| is a bitmask of CheckTypes to run. 54 // Starts the set of checks. |callback| will only be called once.
49 // If |fail_fast| is true, the callback will be invoked once any check fails.
50 // Otherwise it will be invoked when all checks have completed. |callback|
51 // will only be called once.
52 // This function must be called on the UI thread. The callback also occurs on 55 // This function must be called on the UI thread. The callback also occurs on
53 // the UI thread. Checks may run asynchronously in parallel. 56 // the UI thread. Checks may run asynchronously in parallel.
54 // If checks are currently running, the caller must wait for the callback to 57 // This function should be invoked at most once.
55 // be invoked before starting another set of checks. 58 void Start(const Callback& callback);
56 void Start(int enabled_checks, bool fail_fast, const Callback& callback);
57
58 Profile* profile() const { return profile_; }
59
60 const scoped_refptr<const Extension>& extension() { return extension_; }
61 void set_extension(const scoped_refptr<const Extension>& extension) {
62 extension_ = extension;
63 }
64 59
65 // Returns true if any checks are currently running. 60 // Returns true if any checks are currently running.
66 bool is_running() const { return running_checks_ != 0; } 61 bool is_running() const { return running_checks_ != 0; }
67 62
68 // Returns the requirement violations. A non-empty list is considered to be 63 // Returns the requirement violations. A non-empty list is considered to be
69 // a check failure. 64 // a check failure.
70 const std::vector<std::string>& requirement_errors() const { 65 const std::vector<std::string>& requirement_errors() const {
71 return requirement_errors_; 66 return requirement_errors_;
72 } 67 }
73 68
74 // Returns the blacklist state of the extension. A blacklist state of 69 // Returns the blacklist state of the extension. A blacklist state of
75 // BLACKLISTED_MALWARE is considered to be a check failure. 70 // BLACKLISTED_MALWARE is considered to be a check failure.
76 BlacklistState blacklist_state() const { return blacklist_state_; } 71 BlacklistState blacklist_state() const { return blacklist_state_; }
77 72
78 // Returns whether management policy permits installation of the extension. 73 // Returns whether management policy permits installation of the extension.
79 bool policy_allows_load() const { return policy_allows_load_; } 74 bool policy_allows_load() const { return policy_allows_load_; }
80 const std::string& policy_error() const { return policy_error_; } 75 const std::string& policy_error() const { return policy_error_; }
81 76
82 protected: 77 protected:
83 virtual void CheckManagementPolicy(); 78 virtual void CheckManagementPolicy();
84 void OnManagementPolicyCheckDone(bool allows_load, const std::string& error); 79 void OnManagementPolicyCheckDone(bool allows_load, const std::string& error);
85 80
86 virtual void CheckRequirements(); 81 virtual void CheckRequirements();
87 void OnRequirementsCheckDone(int sequence_number, 82 void OnRequirementsCheckDone(const std::vector<std::string>& errors);
88 const std::vector<std::string>& errors);
89 83
90 virtual void CheckBlacklistState(); 84 virtual void CheckBlacklistState();
91 void OnBlacklistStateCheckDone(int sequence_number, BlacklistState state); 85 void OnBlacklistStateCheckDone(BlacklistState state);
92
93 virtual void ResetResults();
94 int current_sequence_number() const { return current_sequence_number_; }
95 86
96 private: 87 private:
97 void MaybeInvokeCallback(); 88 void MaybeInvokeCallback();
98 89
99 std::unique_ptr<RequirementsChecker> requirements_checker_; 90 std::unique_ptr<RequirementsChecker> requirements_checker_;
100 91
101 // The Profile where the extension is being installed in. 92 // The Profile where the extension is being installed in.
102 Profile* profile_; 93 Profile* profile_;
103 94
104 // The extension to run checks for. 95 // The extension to run checks for.
105 scoped_refptr<const Extension> extension_; 96 scoped_refptr<const Extension> extension_;
106 97
107 // Requirement violations. 98 // Requirement violations.
108 std::vector<std::string> requirement_errors_; 99 std::vector<std::string> requirement_errors_;
109 100
110 // Result of the blacklist state check. 101 // Result of the blacklist state check.
111 BlacklistState blacklist_state_; 102 BlacklistState blacklist_state_;
112 103
113 // Whether the extension can be installed, according to management policies. 104 // Whether the extension can be installed, according to management policies.
114 bool policy_allows_load_; 105 bool policy_allows_load_;
115 std::string policy_error_; 106 std::string policy_error_;
116 107
117 // The sequence number of the currently running checks. 108 // Bitmask of enabled checks.
118 int current_sequence_number_; 109 int enabled_checks_;
119 110
120 // Bitmask of currently running checks. 111 // Bitmask of currently running checks.
112 // TODO(michaelpg): Consolidate this with enabled_checks_.
121 int running_checks_; 113 int running_checks_;
122 114
123 // If true, the callback is invoked when the first check fails. 115 // If true, the callback is invoked when the first check fails.
124 bool fail_fast_; 116 bool fail_fast_;
125 117
126 // The callback to invoke when checks are complete. 118 // The callback to invoke when checks are complete.
127 Callback callback_; 119 Callback callback_;
128 120
129 base::WeakPtrFactory<ExtensionInstallChecker> weak_ptr_factory_; 121 base::WeakPtrFactory<ExtensionInstallChecker> weak_ptr_factory_;
130 122
131 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallChecker); 123 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallChecker);
132 }; 124 };
133 125
134 } // namespace extensions 126 } // namespace extensions
135 127
136 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_ 128 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_browsertest.h ('k') | chrome/browser/extensions/extension_install_checker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698