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/preload_check.h" | 16 #include "extensions/browser/preload_check.h" |
| 17 #include "extensions/browser/preload_check_group.h" |
17 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
18 | 19 |
19 class Profile; | 20 class Profile; |
20 | 21 |
21 namespace extensions { | 22 namespace extensions { |
22 | 23 |
23 class RequirementsChecker; | 24 class RequirementsChecker; |
24 | 25 |
25 // Performs common checks for an extension. Extensions that violate these checks | 26 // Performs common checks for an extension. Extensions that violate these checks |
26 // would be disabled or not even installed. | 27 // would be disabled or not even installed. |
| 28 // This class is used as an aggregator for classes such as CrxInstaller. |
| 29 // TODO(michaelpg): Remove this class and have clients create the relevant |
| 30 // PreloadChecks directly. |
27 class ExtensionInstallChecker { | 31 class ExtensionInstallChecker { |
28 public: | 32 public: |
29 // Called when checks are complete. The returned value is a bitmask of | 33 // Called when checks are complete. The returned value is a bitmask of |
30 // failed checks. | 34 // failed checks. |
31 typedef base::Callback<void(int)> Callback; | 35 typedef base::Callback<void(int)> Callback; |
32 | 36 |
33 enum CheckType { | 37 enum CheckType { |
34 // Check the blacklist state of the extension. | 38 // Check the blacklist state of the extension. |
35 CHECK_BLACKLIST = 1 << 0, | 39 CHECK_BLACKLIST = 1 << 0, |
36 // Check whether the extension has requirement errors. | 40 // Check whether the extension has requirement errors. |
37 CHECK_REQUIREMENTS = 1 << 1, | 41 CHECK_REQUIREMENTS = 1 << 1, |
38 // Check whether the extension can be installed and loaded, according to | 42 // Check whether the extension can be installed and loaded, according to |
39 // management policies. | 43 // management policies. |
40 CHECK_MANAGEMENT_POLICY = 1 << 2, | 44 CHECK_MANAGEMENT_POLICY = 1 << 2, |
41 // Perform all checks. | 45 // Perform all checks. |
42 CHECK_ALL = (1 << 3) - 1 | 46 CHECK_ALL = (1 << 3) - 1 |
43 }; | 47 }; |
44 | 48 |
45 explicit ExtensionInstallChecker(Profile* profile); | 49 explicit ExtensionInstallChecker(Profile* profile); |
46 virtual ~ExtensionInstallChecker(); | 50 virtual ~ExtensionInstallChecker(); |
47 | 51 |
48 // Start a set of checks. |enabled_checks| is a bitmask of CheckTypes to run. | 52 // Start a set of checks. |enabled_checks| is a bitmask of CheckTypes to run. |
49 // If |fail_fast| is true, the callback will be invoked once any check fails. | 53 // 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| | 54 // Otherwise it will be invoked when all checks have completed. |callback| |
51 // will only be called once. | 55 // will only be called once. |
52 // This function must be called on the UI thread. The callback also occurs on | 56 // This function must be called on the UI thread and should be called at most |
53 // the UI thread. Checks may run asynchronously in parallel. | 57 // once. The callback also occurs on the UI thread. Checks may run |
54 // If checks are currently running, the caller must wait for the callback to | 58 // asynchronously in parallel. |
55 // be invoked before starting another set of checks. | 59 void Start(scoped_refptr<const Extension> extension, int enabled_checks, |
56 void Start(int enabled_checks, bool fail_fast, const Callback& callback); | 60 bool fail_fast, const Callback& callback); |
57 | 61 |
58 Profile* profile() const { return profile_; } | 62 Profile* profile() const { return profile_; } |
59 | 63 |
60 const scoped_refptr<const Extension>& extension() { return extension_; } | 64 const scoped_refptr<const Extension>& extension() { return extension_; } |
61 void set_extension(const scoped_refptr<const Extension>& extension) { | |
62 extension_ = extension; | |
63 } | |
64 | 65 |
65 // Returns true if any checks are currently running. | 66 // Returns true if any checks are currently running. |
66 bool is_running() const { return running_checks_ != 0; } | 67 bool is_running() const { return is_running_; } |
67 | 68 |
68 // Returns the requirement violations. A non-empty list is considered to be | 69 // Returns the requirement violations. A non-empty string is considered to be |
69 // a check failure. | 70 // a check failure. |
70 const std::vector<std::string>& requirement_errors() const { | 71 const base::string16& requirement_error_message() const { |
71 return requirement_errors_; | 72 return requirement_error_message_; |
72 } | 73 } |
73 | 74 |
74 // Returns the blacklist error of the extension. A blacklist state of | 75 // Returns the blacklist error of the extension. A blacklist state of |
75 // BLACKLISTED_MALWARE is considered to be a check failure. | 76 // BLACKLISTED_MALWARE is considered to be a check failure. |
76 PreloadCheck::Error blacklist_error() const { return blacklist_error_; } | 77 PreloadCheck::Error blacklist_error() const { return blacklist_error_; } |
77 | 78 |
78 // Returns whether management policy permits installation of the extension. | 79 // Returns whether management policy permits installation of the extension. |
79 const std::string& policy_error() const { return policy_error_; } | 80 const base::string16& policy_error() const { return policy_error_; } |
80 | 81 |
| 82 void SetRequirementsCheckerForTesting( |
| 83 std::unique_ptr<PreloadCheck> requirements_checker) { |
| 84 requirements_checker_ = std::move(requirements_checker); |
| 85 } |
81 void SetBlacklistCheckForTesting(std::unique_ptr<PreloadCheck> policy_check) { | 86 void SetBlacklistCheckForTesting(std::unique_ptr<PreloadCheck> policy_check) { |
82 blacklist_check_ = std::move(policy_check); | 87 blacklist_check_ = std::move(policy_check); |
83 } | 88 } |
84 void SetPolicyCheckForTesting(std::unique_ptr<PreloadCheck> policy_check) { | 89 void SetPolicyCheckForTesting(std::unique_ptr<PreloadCheck> policy_check) { |
85 policy_check_ = std::move(policy_check); | 90 policy_check_ = std::move(policy_check); |
86 } | 91 } |
87 | 92 |
88 protected: | |
89 virtual void CheckManagementPolicy(); | |
90 void OnManagementPolicyCheckDone(PreloadCheck::Errors errors); | |
91 | |
92 virtual void CheckRequirements(); | |
93 void OnRequirementsCheckDone(int sequence_number, | |
94 const std::vector<std::string>& errors); | |
95 | |
96 virtual void CheckBlacklistState(); | |
97 void OnBlacklistStateCheckDone(int sequence_number, | |
98 PreloadCheck::Errors errors); | |
99 | |
100 virtual void ResetResults(); | |
101 int current_sequence_number() const { return current_sequence_number_; } | |
102 | |
103 private: | 93 private: |
104 void MaybeInvokeCallback(); | 94 void OnInstallChecksComplete(PreloadCheck::Errors errors); |
105 | 95 int ProcessErrors(PreloadCheck::Errors errors); |
106 std::unique_ptr<RequirementsChecker> requirements_checker_; | |
107 | 96 |
108 // The Profile where the extension is being installed in. | 97 // The Profile where the extension is being installed in. |
109 Profile* profile_; | 98 Profile* profile_; |
110 | 99 |
111 // The extension to run checks for. | 100 // The extension to run checks for. |
112 scoped_refptr<const Extension> extension_; | 101 scoped_refptr<const Extension> extension_; |
113 | 102 |
114 // Requirement violations. | 103 // Checks for requirement violations. |
115 std::vector<std::string> requirement_errors_; | 104 std::unique_ptr<PreloadCheck> requirements_checker_; |
| 105 base::string16 requirement_error_message_; |
116 | 106 |
117 // Checks if the extension in blacklisted. | 107 // Checks if the extension is blacklisted. |
118 std::unique_ptr<PreloadCheck> blacklist_check_; | 108 std::unique_ptr<PreloadCheck> blacklist_check_; |
119 PreloadCheck::Error blacklist_error_; | 109 PreloadCheck::Error blacklist_error_; |
120 | 110 |
121 // Checks whether management policies allow the extension to be installed. | 111 // Checks whether management policies prevent installing the extension. |
122 std::unique_ptr<PreloadCheck> policy_check_; | 112 std::unique_ptr<PreloadCheck> policy_check_; |
123 std::string policy_error_; | 113 base::string16 policy_error_; |
124 | 114 |
125 // The sequence number of the currently running checks. | 115 // Whether the checks are currently running. |
126 int current_sequence_number_; | 116 bool is_running_; |
127 | |
128 // Bitmask of currently running checks. | |
129 int running_checks_; | |
130 | 117 |
131 // If true, the callback is invoked when the first check fails. | 118 // If true, the callback is invoked when the first check fails. |
132 bool fail_fast_; | 119 bool fail_fast_; |
133 | 120 |
134 // The callback to invoke when checks are complete. | 121 // The callback to invoke when checks are complete. |
135 Callback callback_; | 122 Callback callback_; |
136 | 123 |
137 base::WeakPtrFactory<ExtensionInstallChecker> weak_ptr_factory_; | 124 // Installation errors from |preload_check_group_|. |
| 125 PreloadCheck::Errors errors_; |
| 126 |
| 127 // Manages asynchronous extension checks on the UI thread. |
| 128 std::unique_ptr<PreloadCheckGroup> preload_check_group_; |
| 129 |
| 130 //base::WeakPtrFactory<ExtensionInstallChecker> weak_ptr_factory_; |
138 | 131 |
139 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallChecker); | 132 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallChecker); |
140 }; | 133 }; |
141 | 134 |
142 } // namespace extensions | 135 } // namespace extensions |
143 | 136 |
144 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_ | 137 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_ |
OLD | NEW |