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