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