OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/strings/string16.h" | |
16 #include "extensions/browser/preload_check.h" | |
17 #include "extensions/common/extension.h" | |
18 | |
19 class Profile; | |
20 | |
21 namespace extensions { | |
22 | |
23 // Performs common checks for validating whether an extension may be installed. | |
24 // This class should be Start()-ed at most once. | |
25 class ExtensionInstallChecker { | |
26 public: | |
27 // Called when checks are complete. The returned value is a bitmask of | |
28 // failed checks. | |
29 typedef base::Callback<void(int)> Callback; | |
30 | |
31 enum CheckType { | |
32 // Check the blacklist state of the extension. | |
33 CHECK_BLACKLIST = 1 << 0, | |
34 // Check whether the extension has requirement errors. | |
35 CHECK_REQUIREMENTS = 1 << 1, | |
36 // Check whether the extension can be installed and loaded, according to | |
37 // management policies. | |
38 CHECK_MANAGEMENT_POLICY = 1 << 2, | |
39 // Perform all checks. | |
40 CHECK_ALL = (1 << 3) - 1 | |
41 }; | |
42 | |
43 // |enabled_checks| is a bitmask of CheckTypes to run. | |
44 // If |fail_fast| is true, the callback to Start() will be invoked once any | |
45 // check fails. Otherwise it will be invoked when all checks have completed. | |
46 ExtensionInstallChecker(Profile* profile, | |
47 scoped_refptr<const Extension> extension, | |
48 int enabled_checks, | |
49 bool fail_fast); | |
50 virtual ~ExtensionInstallChecker(); | |
51 | |
52 // Starts the set of checks. |callback| will only be called once. | |
53 // This function must be called on the UI thread. The callback also occurs on | |
54 // the UI thread. Checks may run asynchronously in parallel. | |
55 // This function should be invoked at most once. | |
56 void Start(const Callback& callback); | |
57 | |
58 // Returns true if any checks are currently running. | |
59 bool is_running() const { return running_checks_ != 0; } | |
60 | |
61 // Returns the error message for requirement violations, if any were found. | |
62 const base::string16& requirements_error_message() const { | |
63 return requirements_error_message_; | |
64 } | |
65 | |
66 // Returns the blacklist error of the extension. Note that there is only an | |
67 // error if the BlacklistState is BLACKLISTED_MALWARE or BLACKLISTED_UNKNOWN. | |
68 PreloadCheck::Error blacklist_error() const { return blacklist_error_; } | |
69 | |
70 // Returns whether management policy permits installation of the extension. | |
71 const base::string16& policy_error() const { return policy_error_; } | |
72 | |
73 void SetBlacklistCheckForTesting(std::unique_ptr<PreloadCheck> policy_check) { | |
74 blacklist_check_ = std::move(policy_check); | |
75 } | |
76 void SetPolicyCheckForTesting(std::unique_ptr<PreloadCheck> policy_check) { | |
77 policy_check_ = std::move(policy_check); | |
78 } | |
79 void SetRequirementsCheckForTesting( | |
80 std::unique_ptr<PreloadCheck> requirements_check) { | |
81 requirements_check_ = std::move(requirements_check); | |
82 } | |
83 | |
84 protected: | |
85 virtual void CheckManagementPolicy(); | |
86 void OnManagementPolicyCheckDone(PreloadCheck::Errors errors); | |
87 | |
88 virtual void CheckRequirements(); | |
89 void OnRequirementsCheckDone(PreloadCheck::Errors errors); | |
90 | |
91 virtual void CheckBlacklistState(); | |
92 void OnBlacklistStateCheckDone(PreloadCheck::Errors errors); | |
93 | |
94 private: | |
95 void MaybeInvokeCallback(); | |
96 | |
97 // The Profile where the extension is being installed in. | |
98 Profile* profile_; | |
99 | |
100 // The extension to run checks for. | |
101 scoped_refptr<const Extension> extension_; | |
102 | |
103 // Checks requirements specified in the manifest. | |
104 std::unique_ptr<PreloadCheck> requirements_check_; | |
105 base::string16 requirements_error_message_; | |
106 | |
107 // Checks if the extension is blacklisted. | |
108 std::unique_ptr<PreloadCheck> blacklist_check_; | |
109 PreloadCheck::Error blacklist_error_ = PreloadCheck::NONE; | |
110 | |
111 // Checks whether management policies allow the extension to be installed. | |
112 std::unique_ptr<PreloadCheck> policy_check_; | |
113 base::string16 policy_error_; | |
114 | |
115 // Bitmask of enabled checks. | |
116 int enabled_checks_; | |
117 | |
118 // Bitmask of currently running checks. | |
119 // TODO(michaelpg): Consolidate this with enabled_checks_. | |
120 int running_checks_; | |
121 | |
122 // If true, the callback is invoked when the first check fails. | |
123 bool fail_fast_; | |
124 | |
125 // The callback to invoke when checks are complete. | |
126 Callback callback_; | |
127 | |
128 base::WeakPtrFactory<ExtensionInstallChecker> weak_ptr_factory_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallChecker); | |
131 }; | |
132 | |
133 } // namespace extensions | |
134 | |
135 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_CHECKER_H_ | |
OLD | NEW |