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/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/memory/ptr_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/extensions/blacklist.h" | 10 #include "chrome/browser/extensions/blacklist.h" |
| 11 #include "chrome/browser/extensions/blacklist_check.h" |
10 #include "chrome/browser/extensions/chrome_requirements_checker.h" | 12 #include "chrome/browser/extensions/chrome_requirements_checker.h" |
11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
12 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
13 #include "extensions/browser/extension_system.h" | 15 #include "extensions/browser/extension_system.h" |
14 #include "extensions/browser/management_policy.h" | 16 #include "extensions/browser/policy_check.h" |
15 | 17 |
16 namespace extensions { | 18 namespace extensions { |
17 | 19 |
18 ExtensionInstallChecker::ExtensionInstallChecker( | 20 ExtensionInstallChecker::ExtensionInstallChecker( |
19 Profile* profile, | 21 Profile* profile, |
20 scoped_refptr<const Extension> extension, | 22 scoped_refptr<const Extension> extension, |
21 int enabled_checks, | 23 int enabled_checks, |
22 bool fail_fast) | 24 bool fail_fast) |
23 : profile_(profile), | 25 : profile_(profile), |
24 extension_(extension), | 26 extension_(extension), |
25 blacklist_state_(NOT_BLACKLISTED), | 27 blacklist_error_(PreloadCheck::NONE), |
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 27 matching lines...) Expand all Loading... |
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 DCHECK(extension_.get()); |
73 | 74 |
74 base::string16 error; | 75 if (!policy_check_) |
75 bool allow = ExtensionSystem::Get(profile_)->management_policy()->UserMayLoad( | 76 policy_check_ = base::MakeUnique<PolicyCheck>(profile_, extension_.get()); |
76 extension_.get(), &error); | 77 policy_check_->Start( |
77 OnManagementPolicyCheckDone(allow, base::UTF16ToUTF8(error)); | 78 base::BindOnce(&ExtensionInstallChecker::OnManagementPolicyCheckDone, |
| 79 weak_ptr_factory_.GetWeakPtr())); |
78 } | 80 } |
79 | 81 |
80 void ExtensionInstallChecker::OnManagementPolicyCheckDone( | 82 void ExtensionInstallChecker::OnManagementPolicyCheckDone( |
81 bool allows_load, | 83 PreloadCheck::Errors errors) { |
82 const std::string& error) { | 84 if (errors.count(PreloadCheck::DISALLOWED_BY_POLICY)) |
83 policy_allows_load_ = allows_load; | 85 policy_error_ = base::UTF16ToUTF8(policy_check_->GetErrorMessage()); |
84 policy_error_ = error; | |
85 DCHECK(policy_allows_load_ || !policy_error_.empty()); | |
86 | 86 |
87 running_checks_ &= ~CHECK_MANAGEMENT_POLICY; | 87 running_checks_ &= ~CHECK_MANAGEMENT_POLICY; |
88 MaybeInvokeCallback(); | 88 MaybeInvokeCallback(); |
89 } | 89 } |
90 | 90 |
91 void ExtensionInstallChecker::CheckRequirements() { | 91 void ExtensionInstallChecker::CheckRequirements() { |
92 DCHECK(extension_.get()); | 92 DCHECK(extension_.get()); |
93 | 93 |
94 requirements_checker_ = base::MakeUnique<ChromeRequirementsChecker>(); | 94 requirements_checker_ = base::MakeUnique<ChromeRequirementsChecker>(); |
95 requirements_checker_->Check( | 95 requirements_checker_->Check( |
96 extension_, base::Bind(&ExtensionInstallChecker::OnRequirementsCheckDone, | 96 extension_, base::Bind(&ExtensionInstallChecker::OnRequirementsCheckDone, |
97 weak_ptr_factory_.GetWeakPtr())); | 97 weak_ptr_factory_.GetWeakPtr())); |
98 } | 98 } |
99 | 99 |
100 void ExtensionInstallChecker::OnRequirementsCheckDone( | 100 void ExtensionInstallChecker::OnRequirementsCheckDone( |
101 const std::vector<std::string>& errors) { | 101 const std::vector<std::string>& errors) { |
102 DCHECK(is_running()); | 102 DCHECK(is_running()); |
103 | 103 |
104 requirement_errors_ = errors; | 104 requirement_errors_ = errors; |
105 | 105 |
106 running_checks_ &= ~CHECK_REQUIREMENTS; | 106 running_checks_ &= ~CHECK_REQUIREMENTS; |
107 MaybeInvokeCallback(); | 107 MaybeInvokeCallback(); |
108 } | 108 } |
109 | 109 |
110 void ExtensionInstallChecker::CheckBlacklistState() { | 110 void ExtensionInstallChecker::CheckBlacklistState() { |
111 DCHECK(extension_.get()); | 111 DCHECK(extension_.get()); |
112 | 112 |
113 extensions::Blacklist* blacklist = Blacklist::Get(profile_); | 113 if (!blacklist_check_) { |
114 blacklist->IsBlacklisted( | 114 blacklist_check_ = base::MakeUnique<BlacklistCheck>( |
115 extension_->id(), | 115 Blacklist::Get(profile_), extension_.get()); |
116 base::Bind(&ExtensionInstallChecker::OnBlacklistStateCheckDone, | 116 } |
117 weak_ptr_factory_.GetWeakPtr())); | 117 blacklist_check_->Start( |
| 118 base::BindOnce(&ExtensionInstallChecker::OnBlacklistStateCheckDone, |
| 119 weak_ptr_factory_.GetWeakPtr())); |
118 } | 120 } |
119 | 121 |
120 void ExtensionInstallChecker::OnBlacklistStateCheckDone(BlacklistState state) { | 122 void ExtensionInstallChecker::OnBlacklistStateCheckDone( |
| 123 PreloadCheck::Errors errors) { |
121 DCHECK(is_running()); | 124 DCHECK(is_running()); |
122 | 125 |
123 blacklist_state_ = state; | 126 if (errors.empty()) |
| 127 blacklist_error_ = PreloadCheck::NONE; |
| 128 else |
| 129 blacklist_error_ = *errors.begin(); |
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_)) { |
145 // If we are failing fast, discard any pending results. | 151 // If we are failing fast, discard any pending results. |
146 weak_ptr_factory_.InvalidateWeakPtrs(); | 152 weak_ptr_factory_.InvalidateWeakPtrs(); |
147 running_checks_ = 0; | 153 running_checks_ = 0; |
| 154 policy_check_.reset(); |
| 155 blacklist_check_.reset(); |
148 base::ResetAndReturn(&callback_).Run(failed_mask); | 156 base::ResetAndReturn(&callback_).Run(failed_mask); |
149 } | 157 } |
150 } | 158 } |
151 | 159 |
152 } // namespace extensions | 160 } // namespace extensions |
OLD | NEW |