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 profile()57 Profile* profile() const { return profile_; } 58 extension()59 const scoped_refptr<const Extension>& extension() { return extension_; } set_extension(const scoped_refptr<const Extension> & extension)60 void set_extension(const scoped_refptr<const Extension>& extension) { 61 extension_ = extension; 62 } 63 64 // Returns true if any checks are currently running. is_running()65 bool is_running() const { return running_checks_ != 0; } 66 67 // Returns the requirement violations. A non-empty list is considered to be 68 // a check failure. requirement_errors()69 const std::vector<std::string>& requirement_errors() const { 70 return requirement_errors_; 71 } 72 73 // Returns the blacklist state of the extension. A blacklist state of 74 // BLACKLISTED_MALWARE is considered to be a check failure. blacklist_state()75 BlacklistState blacklist_state() const { return blacklist_state_; } 76 77 // Returns whether management policy permits installation of the extension. policy_allows_load()78 bool policy_allows_load() const { return policy_allows_load_; } policy_error()79 const std::string& policy_error() const { return policy_error_; } 80 81 protected: 82 virtual void CheckManagementPolicy(); 83 void OnManagementPolicyCheckDone(bool allows_load, const std::string& error); 84 85 virtual void CheckRequirements(); 86 void OnRequirementsCheckDone(int sequence_number, 87 std::vector<std::string> errors); 88 89 virtual void CheckBlacklistState(); 90 void OnBlacklistStateCheckDone(int sequence_number, BlacklistState state); 91 92 virtual void ResetResults(); current_sequence_number()93 int current_sequence_number() const { return current_sequence_number_; } 94 95 private: 96 void MaybeInvokeCallback(); 97 98 scoped_ptr<RequirementsChecker> requirements_checker_; 99 100 // The Profile where the extension is being installed in. 101 Profile* profile_; 102 103 // The extension to run checks for. 104 scoped_refptr<const Extension> extension_; 105 106 // Requirement violations. 107 std::vector<std::string> requirement_errors_; 108 109 // Result of the blacklist state check. 110 BlacklistState blacklist_state_; 111 112 // Whether the extension can be installed, according to management policies. 113 bool policy_allows_load_; 114 std::string policy_error_; 115 116 // The sequence number of the currently running checks. 117 int current_sequence_number_; 118 119 // Bitmask of currently running 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_ 136