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