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