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 #include "chrome/browser/extensions/extension_install_checker.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/extensions/blacklist.h" |
| 9 #include "chrome/browser/extensions/requirements_checker.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "extensions/browser/extension_system.h" |
| 13 #include "extensions/browser/management_policy.h" |
| 14 |
| 15 namespace extensions { |
| 16 |
| 17 ExtensionInstallChecker::ExtensionInstallChecker(Profile* profile) |
| 18 : profile_(profile), |
| 19 blacklist_state_(NOT_BLACKLISTED), |
| 20 policy_allows_load_(true), |
| 21 running_checks_(0), |
| 22 fail_fast_(false), |
| 23 weak_ptr_factory_(this) { |
| 24 } |
| 25 |
| 26 ExtensionInstallChecker::~ExtensionInstallChecker() { |
| 27 } |
| 28 |
| 29 void ExtensionInstallChecker::Start(int enabled_checks, |
| 30 bool fail_fast, |
| 31 const Callback& callback) { |
| 32 // Profile is null in tests. |
| 33 if (profile_) { |
| 34 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 35 if (!extension_.get()) { |
| 36 NOTREACHED(); |
| 37 return; |
| 38 } |
| 39 } |
| 40 |
| 41 if (is_running() || !enabled_checks || callback.is_null()) { |
| 42 NOTREACHED(); |
| 43 return; |
| 44 } |
| 45 |
| 46 running_checks_ = enabled_checks; |
| 47 fail_fast_ = fail_fast; |
| 48 callback_ = callback; |
| 49 ResetResults(); |
| 50 |
| 51 // Execute the management policy check first as it is synchronous. |
| 52 if (enabled_checks & CHECK_MANAGEMENT_POLICY) { |
| 53 CheckManagementPolicy(); |
| 54 if (!is_running()) |
| 55 return; |
| 56 } |
| 57 |
| 58 if (enabled_checks & CHECK_REQUIREMENTS) { |
| 59 CheckRequirements(); |
| 60 if (!is_running()) |
| 61 return; |
| 62 } |
| 63 |
| 64 if (enabled_checks & CHECK_BLACKLIST) |
| 65 CheckBlacklistState(); |
| 66 } |
| 67 |
| 68 void ExtensionInstallChecker::CheckManagementPolicy() { |
| 69 DCHECK(extension_.get()); |
| 70 |
| 71 base::string16 error; |
| 72 bool allow = ExtensionSystem::Get(profile_)->management_policy()->UserMayLoad( |
| 73 extension_.get(), &error); |
| 74 OnManagementPolicyCheckDone(allow, base::UTF16ToUTF8(error)); |
| 75 } |
| 76 |
| 77 void ExtensionInstallChecker::OnManagementPolicyCheckDone( |
| 78 bool allows_load, |
| 79 const std::string& error) { |
| 80 policy_allows_load_ = allows_load; |
| 81 policy_error_ = error; |
| 82 DCHECK(policy_allows_load_ || !policy_error_.empty()); |
| 83 |
| 84 running_checks_ &= ~CHECK_MANAGEMENT_POLICY; |
| 85 MaybeInvokeCallback(); |
| 86 } |
| 87 |
| 88 void ExtensionInstallChecker::CheckRequirements() { |
| 89 DCHECK(extension_.get()); |
| 90 |
| 91 if (!requirements_checker_.get()) |
| 92 requirements_checker_.reset(new RequirementsChecker()); |
| 93 requirements_checker_->Check( |
| 94 extension_, |
| 95 base::Bind(&ExtensionInstallChecker::OnRequirementsCheckDone, |
| 96 weak_ptr_factory_.GetWeakPtr())); |
| 97 } |
| 98 |
| 99 void ExtensionInstallChecker::OnRequirementsCheckDone( |
| 100 std::vector<std::string> errors) { |
| 101 // Some pending results may arrive after fail fast. |
| 102 if (!is_running()) |
| 103 return; |
| 104 |
| 105 requirement_errors_ = errors; |
| 106 |
| 107 running_checks_ &= ~CHECK_REQUIREMENTS; |
| 108 MaybeInvokeCallback(); |
| 109 } |
| 110 |
| 111 void ExtensionInstallChecker::CheckBlacklistState() { |
| 112 DCHECK(extension_.get()); |
| 113 |
| 114 extensions::Blacklist* blacklist = |
| 115 ExtensionSystem::Get(profile_)->blacklist(); |
| 116 blacklist->IsBlacklisted( |
| 117 extension_->id(), |
| 118 base::Bind(&ExtensionInstallChecker::OnBlacklistStateCheckDone, |
| 119 weak_ptr_factory_.GetWeakPtr())); |
| 120 } |
| 121 |
| 122 void ExtensionInstallChecker::OnBlacklistStateCheckDone(BlacklistState state) { |
| 123 // Some pending results may arrive after fail fast. |
| 124 if (!is_running()) |
| 125 return; |
| 126 |
| 127 blacklist_state_ = state; |
| 128 |
| 129 running_checks_ &= ~CHECK_BLACKLIST; |
| 130 MaybeInvokeCallback(); |
| 131 } |
| 132 |
| 133 void ExtensionInstallChecker::ResetResults() { |
| 134 requirement_errors_.clear(); |
| 135 blacklist_state_ = NOT_BLACKLISTED; |
| 136 policy_allows_load_ = true; |
| 137 policy_error_.clear(); |
| 138 } |
| 139 |
| 140 void ExtensionInstallChecker::MaybeInvokeCallback() { |
| 141 if (callback_.is_null()) |
| 142 return; |
| 143 |
| 144 // Set bits for failed checks. |
| 145 int failed_mask = 0; |
| 146 if (blacklist_state_ == BLACKLISTED_MALWARE) |
| 147 failed_mask |= CHECK_BLACKLIST; |
| 148 if (!requirement_errors_.empty()) |
| 149 failed_mask |= CHECK_REQUIREMENTS; |
| 150 if (!policy_allows_load_) |
| 151 failed_mask |= CHECK_MANAGEMENT_POLICY; |
| 152 |
| 153 // Invoke callback if all checks are complete or there was at least one |
| 154 // failure and |fail_fast_| is true. |
| 155 if (!is_running() || (failed_mask && fail_fast_)) { |
| 156 // If we are failing fast, discard any pending results. |
| 157 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 158 running_checks_ = 0; |
| 159 |
| 160 callback_.Run(failed_mask); |
| 161 callback_.Reset(); |
| 162 } |
| 163 } |
| 164 |
| 165 } // namespace extensions |
OLD | NEW |