Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: chrome/browser/extensions/extension_install_checker.cc

Issue 2751013002: Simplify ExtensionInstallChecker into a single-use class (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/blacklist.h" 8 #include "chrome/browser/extensions/blacklist.h"
9 #include "chrome/browser/extensions/chrome_requirements_checker.h" 9 #include "chrome/browser/extensions/chrome_requirements_checker.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
12 #include "extensions/browser/extension_system.h" 12 #include "extensions/browser/extension_system.h"
13 #include "extensions/browser/management_policy.h" 13 #include "extensions/browser/management_policy.h"
14 14
15 namespace extensions { 15 namespace extensions {
16 16
17 ExtensionInstallChecker::ExtensionInstallChecker(Profile* profile) 17 ExtensionInstallChecker::ExtensionInstallChecker(
18 Profile* profile,
19 scoped_refptr<const Extension> extension)
18 : profile_(profile), 20 : profile_(profile),
21 extension_(extension),
19 blacklist_state_(NOT_BLACKLISTED), 22 blacklist_state_(NOT_BLACKLISTED),
20 policy_allows_load_(true), 23 policy_allows_load_(true),
21 current_sequence_number_(0), 24 enabled_checks_(0),
22 running_checks_(0), 25 running_checks_(0),
23 fail_fast_(false), 26 fail_fast_(false),
24 weak_ptr_factory_(this) { 27 weak_ptr_factory_(this) {}
25 }
26 28
27 ExtensionInstallChecker::~ExtensionInstallChecker() { 29 ExtensionInstallChecker::~ExtensionInstallChecker() {
28 } 30 }
29 31
30 void ExtensionInstallChecker::Start(int enabled_checks, 32 void ExtensionInstallChecker::Start(int enabled_checks,
31 bool fail_fast, 33 bool fail_fast,
32 const Callback& callback) { 34 const Callback& callback) {
35 DCHECK_EQ(enabled_checks_, 0)
36 << "ExtensionInstallChecker::Start() can be called at most once";
37
33 // Profile is null in tests. 38 // Profile is null in tests.
34 if (profile_) { 39 if (profile_) {
35 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
36 if (!extension_.get()) { 41 if (!extension_.get()) {
37 NOTREACHED(); 42 NOTREACHED();
38 return; 43 return;
39 } 44 }
40 } 45 }
41 46
42 if (is_running() || !enabled_checks || callback.is_null()) { 47 if (is_running() || !enabled_checks || callback.is_null()) {
43 NOTREACHED(); 48 NOTREACHED();
44 return; 49 return;
45 } 50 }
46 51
52 enabled_checks_ = enabled_checks;
Devlin 2017/03/16 19:29:40 With this class becoming single use, we can move a
michaelpg 2017/03/17 04:25:23 Done.
47 running_checks_ = enabled_checks; 53 running_checks_ = enabled_checks;
48 fail_fast_ = fail_fast; 54 fail_fast_ = fail_fast;
49 callback_ = callback; 55 callback_ = callback;
50 ResetResults();
51 56
52 // Execute the management policy check first as it is synchronous. 57 // Execute the management policy check first as it is synchronous.
53 if (enabled_checks & CHECK_MANAGEMENT_POLICY) { 58 if (enabled_checks & CHECK_MANAGEMENT_POLICY) {
54 CheckManagementPolicy(); 59 CheckManagementPolicy();
55 if (!is_running()) 60 if (!is_running())
Devlin 2017/03/16 19:29:40 not your code, but we're here. :) these is_runnin
michaelpg 2017/03/17 04:25:23 this particular early return triggers if the manag
56 return; 61 return;
57 } 62 }
58 63
59 if (enabled_checks & CHECK_REQUIREMENTS) { 64 if (enabled_checks & CHECK_REQUIREMENTS) {
60 CheckRequirements(); 65 CheckRequirements();
61 if (!is_running()) 66 if (!is_running())
62 return; 67 return;
63 } 68 }
64 69
65 if (enabled_checks & CHECK_BLACKLIST) 70 if (enabled_checks & CHECK_BLACKLIST)
(...skipping 19 matching lines...) Expand all
85 running_checks_ &= ~CHECK_MANAGEMENT_POLICY; 90 running_checks_ &= ~CHECK_MANAGEMENT_POLICY;
86 MaybeInvokeCallback(); 91 MaybeInvokeCallback();
87 } 92 }
88 93
89 void ExtensionInstallChecker::CheckRequirements() { 94 void ExtensionInstallChecker::CheckRequirements() {
90 DCHECK(extension_.get()); 95 DCHECK(extension_.get());
91 96
92 if (!requirements_checker_.get()) 97 if (!requirements_checker_.get())
93 requirements_checker_.reset(new ChromeRequirementsChecker()); 98 requirements_checker_.reset(new ChromeRequirementsChecker());
94 requirements_checker_->Check( 99 requirements_checker_->Check(
95 extension_, 100 extension_, base::Bind(&ExtensionInstallChecker::OnRequirementsCheckDone,
96 base::Bind(&ExtensionInstallChecker::OnRequirementsCheckDone, 101 weak_ptr_factory_.GetWeakPtr()));
97 weak_ptr_factory_.GetWeakPtr(),
98 current_sequence_number_));
99 } 102 }
100 103
101 void ExtensionInstallChecker::OnRequirementsCheckDone( 104 void ExtensionInstallChecker::OnRequirementsCheckDone(
102 int sequence_number,
103 const std::vector<std::string>& errors) { 105 const std::vector<std::string>& errors) {
104 // Some pending results may arrive after fail fast. 106 // Some pending results may arrive after fail fast.
105 if (sequence_number != current_sequence_number_) 107 if (!is_running())
106 return; 108 return;
107 109
108 requirement_errors_ = errors; 110 requirement_errors_ = errors;
109 111
110 running_checks_ &= ~CHECK_REQUIREMENTS; 112 running_checks_ &= ~CHECK_REQUIREMENTS;
111 MaybeInvokeCallback(); 113 MaybeInvokeCallback();
112 } 114 }
113 115
114 void ExtensionInstallChecker::CheckBlacklistState() { 116 void ExtensionInstallChecker::CheckBlacklistState() {
115 DCHECK(extension_.get()); 117 DCHECK(extension_.get());
116 118
117 extensions::Blacklist* blacklist = Blacklist::Get(profile_); 119 extensions::Blacklist* blacklist = Blacklist::Get(profile_);
118 blacklist->IsBlacklisted( 120 blacklist->IsBlacklisted(
119 extension_->id(), 121 extension_->id(),
120 base::Bind(&ExtensionInstallChecker::OnBlacklistStateCheckDone, 122 base::Bind(&ExtensionInstallChecker::OnBlacklistStateCheckDone,
121 weak_ptr_factory_.GetWeakPtr(), 123 weak_ptr_factory_.GetWeakPtr()));
122 current_sequence_number_));
123 } 124 }
124 125
125 void ExtensionInstallChecker::OnBlacklistStateCheckDone(int sequence_number, 126 void ExtensionInstallChecker::OnBlacklistStateCheckDone(BlacklistState state) {
126 BlacklistState state) {
127 // Some pending results may arrive after fail fast. 127 // Some pending results may arrive after fail fast.
128 if (sequence_number != current_sequence_number_) 128 if (!is_running())
129 return; 129 return;
130 130
131 blacklist_state_ = state; 131 blacklist_state_ = state;
132 132
133 running_checks_ &= ~CHECK_BLACKLIST; 133 running_checks_ &= ~CHECK_BLACKLIST;
134 MaybeInvokeCallback(); 134 MaybeInvokeCallback();
135 } 135 }
136 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
144 void ExtensionInstallChecker::MaybeInvokeCallback() { 137 void ExtensionInstallChecker::MaybeInvokeCallback() {
145 if (callback_.is_null()) 138 if (callback_.is_null())
146 return; 139 return;
147 140
148 // Set bits for failed checks. 141 // Set bits for failed checks.
149 int failed_mask = 0; 142 int failed_mask = 0;
150 if (blacklist_state_ == BLACKLISTED_MALWARE) 143 if (blacklist_state_ == BLACKLISTED_MALWARE)
151 failed_mask |= CHECK_BLACKLIST; 144 failed_mask |= CHECK_BLACKLIST;
152 if (!requirement_errors_.empty()) 145 if (!requirement_errors_.empty())
153 failed_mask |= CHECK_REQUIREMENTS; 146 failed_mask |= CHECK_REQUIREMENTS;
154 if (!policy_allows_load_) 147 if (!policy_allows_load_)
155 failed_mask |= CHECK_MANAGEMENT_POLICY; 148 failed_mask |= CHECK_MANAGEMENT_POLICY;
156 149
157 // 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
158 // failure and |fail_fast_| is true. 151 // failure and |fail_fast_| is true.
159 if (!is_running() || (failed_mask && fail_fast_)) { 152 if (!is_running() || (failed_mask && fail_fast_)) {
160 // If we are failing fast, discard any pending results. 153 // If we are failing fast, discard any pending results.
161 weak_ptr_factory_.InvalidateWeakPtrs(); 154 weak_ptr_factory_.InvalidateWeakPtrs();
162 running_checks_ = 0; 155 running_checks_ = 0;
163 ++current_sequence_number_;
164 156
165 Callback callback_copy = callback_; 157 Callback callback_copy = callback_;
166 callback_.Reset(); 158 callback_.Reset();
167 159
168 // This instance may be owned by the callback recipient and deleted here, 160 // This instance may be owned by the callback recipient and deleted here,
169 // so reset |callback_| first and invoke a copy of the callback. 161 // so reset |callback_| first and invoke a copy of the callback.
170 callback_copy.Run(failed_mask); 162 callback_copy.Run(failed_mask);
Devlin 2017/03/16 19:29:40 sorry for the drive-by: base::ResetAndReturn(&call
michaelpg 2017/03/17 04:25:23 Awesome! Done and suggested documentation in https
171 } 163 }
172 } 164 }
173 165
174 } // namespace extensions 166 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698