OLD | NEW |
---|---|
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 | 59 |
58 Profile* profile() const { return profile_; } | 60 Profile* profile() const { return profile_; } |
Devlin
2017/03/20 15:36:46
I think we can remove this?
michaelpg
2017/03/22 00:17:12
Done.
| |
59 | 61 |
60 const scoped_refptr<const Extension>& extension() { return extension_; } | 62 const scoped_refptr<const Extension>& extension() { return extension_; } |
Devlin
2017/03/20 15:36:46
and maybe this?
michaelpg
2017/03/22 00:17:12
Done.
| |
61 void set_extension(const scoped_refptr<const Extension>& extension) { | |
62 extension_ = extension; | |
63 } | |
64 | 63 |
65 // Returns true if any checks are currently running. | 64 // Returns true if any checks are currently running. |
66 bool is_running() const { return running_checks_ != 0; } | 65 bool is_running() const { return running_checks_ != 0; } |
67 | 66 |
68 // Returns the requirement violations. A non-empty list is considered to be | 67 // Returns the requirement violations. A non-empty list is considered to be |
69 // a check failure. | 68 // a check failure. |
70 const std::vector<std::string>& requirement_errors() const { | 69 const std::vector<std::string>& requirement_errors() const { |
71 return requirement_errors_; | 70 return requirement_errors_; |
72 } | 71 } |
73 | 72 |
74 // Returns the blacklist state of the extension. A blacklist state of | 73 // Returns the blacklist state of the extension. A blacklist state of |
75 // BLACKLISTED_MALWARE is considered to be a check failure. | 74 // BLACKLISTED_MALWARE is considered to be a check failure. |
76 BlacklistState blacklist_state() const { return blacklist_state_; } | 75 BlacklistState blacklist_state() const { return blacklist_state_; } |
77 | 76 |
78 // Returns whether management policy permits installation of the extension. | 77 // Returns whether management policy permits installation of the extension. |
79 bool policy_allows_load() const { return policy_allows_load_; } | 78 bool policy_allows_load() const { return policy_allows_load_; } |
80 const std::string& policy_error() const { return policy_error_; } | 79 const std::string& policy_error() const { return policy_error_; } |
81 | 80 |
82 protected: | 81 protected: |
83 virtual void CheckManagementPolicy(); | 82 virtual void CheckManagementPolicy(); |
84 void OnManagementPolicyCheckDone(bool allows_load, const std::string& error); | 83 void OnManagementPolicyCheckDone(bool allows_load, const std::string& error); |
85 | 84 |
86 virtual void CheckRequirements(); | 85 virtual void CheckRequirements(); |
87 void OnRequirementsCheckDone(int sequence_number, | 86 void OnRequirementsCheckDone(const std::vector<std::string>& errors); |
88 const std::vector<std::string>& errors); | |
89 | 87 |
90 virtual void CheckBlacklistState(); | 88 virtual void CheckBlacklistState(); |
91 void OnBlacklistStateCheckDone(int sequence_number, BlacklistState state); | 89 void OnBlacklistStateCheckDone(BlacklistState state); |
92 | |
93 virtual void ResetResults(); | |
94 int current_sequence_number() const { return current_sequence_number_; } | |
95 | 90 |
96 private: | 91 private: |
97 void MaybeInvokeCallback(); | 92 void MaybeInvokeCallback(); |
98 | 93 |
99 std::unique_ptr<RequirementsChecker> requirements_checker_; | 94 std::unique_ptr<RequirementsChecker> requirements_checker_; |
100 | 95 |
101 // The Profile where the extension is being installed in. | 96 // The Profile where the extension is being installed in. |
102 Profile* profile_; | 97 Profile* profile_; |
103 | 98 |
104 // The extension to run checks for. | 99 // The extension to run checks for. |
105 scoped_refptr<const Extension> extension_; | 100 scoped_refptr<const Extension> extension_; |
106 | 101 |
107 // Requirement violations. | 102 // Requirement violations. |
108 std::vector<std::string> requirement_errors_; | 103 std::vector<std::string> requirement_errors_; |
109 | 104 |
110 // Result of the blacklist state check. | 105 // Result of the blacklist state check. |
111 BlacklistState blacklist_state_; | 106 BlacklistState blacklist_state_; |
112 | 107 |
113 // Whether the extension can be installed, according to management policies. | 108 // Whether the extension can be installed, according to management policies. |
114 bool policy_allows_load_; | 109 bool policy_allows_load_; |
115 std::string policy_error_; | 110 std::string policy_error_; |
116 | 111 |
117 // The sequence number of the currently running checks. | 112 // Bitmask of enabled checks. |
118 int current_sequence_number_; | 113 int enabled_checks_; |
119 | 114 |
120 // Bitmask of currently running checks. | 115 // Bitmask of currently running checks. |
121 int running_checks_; | 116 int running_checks_; |
122 | 117 |
123 // If true, the callback is invoked when the first check fails. | 118 // If true, the callback is invoked when the first check fails. |
124 bool fail_fast_; | 119 bool fail_fast_; |
125 | 120 |
126 // The callback to invoke when checks are complete. | 121 // The callback to invoke when checks are complete. |
127 Callback callback_; | 122 Callback callback_; |
128 | 123 |
129 base::WeakPtrFactory<ExtensionInstallChecker> weak_ptr_factory_; | 124 base::WeakPtrFactory<ExtensionInstallChecker> weak_ptr_factory_; |
130 | 125 |
131 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallChecker); | 126 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallChecker); |
132 }; | 127 }; |
133 | 128 |
134 } // namespace extensions | 129 } // namespace extensions |
135 | 130 |
136 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_ | 131 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_ |
OLD | NEW |