| 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 #include "chrome/browser/extensions/extension_install_checker.h" | 5 #include "chrome/browser/extensions/extension_install_checker.h" |
| 6 | 6 |
| 7 #include "base/bind_helpers.h" |
| 7 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/blacklist.h" | 11 #include "chrome/browser/extensions/blacklist.h" |
| 12 #include "chrome/browser/extensions/blacklist_check.h" |
| 10 #include "chrome/browser/extensions/chrome_requirements_checker.h" | 13 #include "chrome/browser/extensions/chrome_requirements_checker.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 12 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 13 #include "extensions/browser/extension_system.h" | 16 #include "extensions/browser/extension_system.h" |
| 14 #include "extensions/browser/management_policy.h" | 17 #include "extensions/browser/policy_check.h" |
| 15 | 18 |
| 16 namespace extensions { | 19 namespace extensions { |
| 17 | 20 |
| 18 ExtensionInstallChecker::ExtensionInstallChecker( | 21 ExtensionInstallChecker::ExtensionInstallChecker( |
| 19 Profile* profile, | 22 Profile* profile, |
| 20 scoped_refptr<const Extension> extension, | 23 scoped_refptr<const Extension> extension, |
| 21 int enabled_checks, | 24 int enabled_checks, |
| 22 bool fail_fast) | 25 bool fail_fast) |
| 23 : profile_(profile), | 26 : profile_(profile), |
| 24 extension_(extension), | 27 extension_(extension), |
| 25 blacklist_state_(NOT_BLACKLISTED), | 28 blacklist_error_(PreloadCheck::NONE), |
| 26 policy_allows_load_(true), | |
| 27 enabled_checks_(enabled_checks), | 29 enabled_checks_(enabled_checks), |
| 28 running_checks_(0), | 30 running_checks_(0), |
| 29 fail_fast_(fail_fast), | 31 fail_fast_(fail_fast), |
| 30 weak_ptr_factory_(this) {} | 32 weak_ptr_factory_(this) {} |
| 31 | 33 |
| 32 ExtensionInstallChecker::~ExtensionInstallChecker() { | 34 ExtensionInstallChecker::~ExtensionInstallChecker() { |
| 33 } | 35 } |
| 34 | 36 |
| 35 void ExtensionInstallChecker::Start(const Callback& callback) { | 37 void ExtensionInstallChecker::Start(const Callback& callback) { |
| 36 // Profile is null in tests. | 38 // Profile is null in tests. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 64 return; | 66 return; |
| 65 } | 67 } |
| 66 | 68 |
| 67 if (enabled_checks_ & CHECK_BLACKLIST) | 69 if (enabled_checks_ & CHECK_BLACKLIST) |
| 68 CheckBlacklistState(); | 70 CheckBlacklistState(); |
| 69 } | 71 } |
| 70 | 72 |
| 71 void ExtensionInstallChecker::CheckManagementPolicy() { | 73 void ExtensionInstallChecker::CheckManagementPolicy() { |
| 72 DCHECK(extension_.get()); | 74 DCHECK(extension_.get()); |
| 73 | 75 |
| 74 base::string16 error; | 76 // In tests, this check may already be stubbed. |
| 75 bool allow = ExtensionSystem::Get(profile_)->management_policy()->UserMayLoad( | 77 if (!policy_check_) |
| 76 extension_.get(), &error); | 78 policy_check_ = base::MakeUnique<PolicyCheck>(profile_, extension_.get()); |
| 77 OnManagementPolicyCheckDone(allow, base::UTF16ToUTF8(error)); | 79 policy_check_->Start( |
| 80 base::BindOnce(&ExtensionInstallChecker::OnManagementPolicyCheckDone, |
| 81 weak_ptr_factory_.GetWeakPtr())); |
| 78 } | 82 } |
| 79 | 83 |
| 80 void ExtensionInstallChecker::OnManagementPolicyCheckDone( | 84 void ExtensionInstallChecker::OnManagementPolicyCheckDone( |
| 81 bool allows_load, | 85 PreloadCheck::Errors errors) { |
| 82 const std::string& error) { | 86 if (errors.count(PreloadCheck::DISALLOWED_BY_POLICY)) |
| 83 policy_allows_load_ = allows_load; | 87 policy_error_ = base::UTF16ToUTF8(policy_check_->GetErrorMessage()); |
| 84 policy_error_ = error; | |
| 85 DCHECK(policy_allows_load_ || !policy_error_.empty()); | |
| 86 | 88 |
| 87 running_checks_ &= ~CHECK_MANAGEMENT_POLICY; | 89 running_checks_ &= ~CHECK_MANAGEMENT_POLICY; |
| 88 MaybeInvokeCallback(); | 90 MaybeInvokeCallback(); |
| 89 } | 91 } |
| 90 | 92 |
| 91 void ExtensionInstallChecker::CheckRequirements() { | 93 void ExtensionInstallChecker::CheckRequirements() { |
| 92 DCHECK(extension_.get()); | 94 DCHECK(extension_.get()); |
| 93 | 95 |
| 94 requirements_checker_ = base::MakeUnique<ChromeRequirementsChecker>(); | 96 requirements_checker_ = base::MakeUnique<ChromeRequirementsChecker>(); |
| 95 requirements_checker_->Check( | 97 requirements_checker_->Check( |
| 96 extension_, base::Bind(&ExtensionInstallChecker::OnRequirementsCheckDone, | 98 extension_, base::Bind(&ExtensionInstallChecker::OnRequirementsCheckDone, |
| 97 weak_ptr_factory_.GetWeakPtr())); | 99 weak_ptr_factory_.GetWeakPtr())); |
| 98 } | 100 } |
| 99 | 101 |
| 100 void ExtensionInstallChecker::OnRequirementsCheckDone( | 102 void ExtensionInstallChecker::OnRequirementsCheckDone( |
| 101 const std::vector<std::string>& errors) { | 103 const std::vector<std::string>& errors) { |
| 102 DCHECK(is_running()); | 104 DCHECK(is_running()); |
| 103 | 105 |
| 104 requirement_errors_ = errors; | 106 requirement_errors_ = errors; |
| 105 | 107 |
| 106 running_checks_ &= ~CHECK_REQUIREMENTS; | 108 running_checks_ &= ~CHECK_REQUIREMENTS; |
| 107 MaybeInvokeCallback(); | 109 MaybeInvokeCallback(); |
| 108 } | 110 } |
| 109 | 111 |
| 110 void ExtensionInstallChecker::CheckBlacklistState() { | 112 void ExtensionInstallChecker::CheckBlacklistState() { |
| 111 DCHECK(extension_.get()); | 113 DCHECK(extension_.get()); |
| 112 | 114 |
| 113 extensions::Blacklist* blacklist = Blacklist::Get(profile_); | 115 // In tests, this check may already be stubbed. |
| 114 blacklist->IsBlacklisted( | 116 if (!blacklist_check_) { |
| 115 extension_->id(), | 117 blacklist_check_ = base::MakeUnique<BlacklistCheck>( |
| 116 base::Bind(&ExtensionInstallChecker::OnBlacklistStateCheckDone, | 118 Blacklist::Get(profile_), extension_.get()); |
| 117 weak_ptr_factory_.GetWeakPtr())); | 119 } |
| 120 blacklist_check_->Start( |
| 121 base::BindOnce(&ExtensionInstallChecker::OnBlacklistStateCheckDone, |
| 122 weak_ptr_factory_.GetWeakPtr())); |
| 118 } | 123 } |
| 119 | 124 |
| 120 void ExtensionInstallChecker::OnBlacklistStateCheckDone(BlacklistState state) { | 125 void ExtensionInstallChecker::OnBlacklistStateCheckDone( |
| 126 PreloadCheck::Errors errors) { |
| 121 DCHECK(is_running()); | 127 DCHECK(is_running()); |
| 122 | 128 |
| 123 blacklist_state_ = state; | 129 if (errors.empty()) { |
| 130 blacklist_error_ = PreloadCheck::NONE; |
| 131 } else { |
| 132 DCHECK_EQ(1u, errors.size()); |
| 133 blacklist_error_ = *errors.begin(); |
| 134 } |
| 124 | 135 |
| 125 running_checks_ &= ~CHECK_BLACKLIST; | 136 running_checks_ &= ~CHECK_BLACKLIST; |
| 126 MaybeInvokeCallback(); | 137 MaybeInvokeCallback(); |
| 127 } | 138 } |
| 128 | 139 |
| 129 void ExtensionInstallChecker::MaybeInvokeCallback() { | 140 void ExtensionInstallChecker::MaybeInvokeCallback() { |
| 130 if (callback_.is_null()) | 141 if (callback_.is_null()) |
| 131 return; | 142 return; |
| 132 | 143 |
| 133 // Set bits for failed checks. | 144 // Set bits for failed checks. |
| 134 int failed_mask = 0; | 145 int failed_mask = 0; |
| 135 if (blacklist_state_ == BLACKLISTED_MALWARE) | 146 if (blacklist_error_ == PreloadCheck::BLACKLISTED_ID) |
| 136 failed_mask |= CHECK_BLACKLIST; | 147 failed_mask |= CHECK_BLACKLIST; |
| 137 if (!requirement_errors_.empty()) | 148 if (!requirement_errors_.empty()) |
| 138 failed_mask |= CHECK_REQUIREMENTS; | 149 failed_mask |= CHECK_REQUIREMENTS; |
| 139 if (!policy_allows_load_) | 150 if (!policy_error_.empty()) |
| 140 failed_mask |= CHECK_MANAGEMENT_POLICY; | 151 failed_mask |= CHECK_MANAGEMENT_POLICY; |
| 141 | 152 |
| 142 // Invoke callback if all checks are complete or there was at least one | 153 // Invoke callback if all checks are complete or there was at least one |
| 143 // failure and |fail_fast_| is true. | 154 // failure and |fail_fast_| is true. |
| 144 if (!is_running() || (failed_mask && fail_fast_)) { | 155 if (!is_running() || (failed_mask && fail_fast_)) { |
| 156 running_checks_ = 0; |
| 157 |
| 145 // If we are failing fast, discard any pending results. | 158 // If we are failing fast, discard any pending results. |
| 159 blacklist_check_.reset(); |
| 160 policy_check_.reset(); |
| 161 requirements_checker_.reset(); |
| 146 weak_ptr_factory_.InvalidateWeakPtrs(); | 162 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 147 running_checks_ = 0; | |
| 148 base::ResetAndReturn(&callback_).Run(failed_mask); | 163 base::ResetAndReturn(&callback_).Run(failed_mask); |
| 149 } | 164 } |
| 150 } | 165 } |
| 151 | 166 |
| 152 } // namespace extensions | 167 } // namespace extensions |
| OLD | NEW |