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