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

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

Issue 2751013002: Simplify ExtensionInstallChecker into a single-use class (Closed)
Patch Set: rebase on enable_extensions=0 fix 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/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),
Devlin 2017/03/20 15:36:46 We could combine running_checks_ and enabled_check
michaelpg 2017/03/22 00:17:12 Added a TODO. running_checks_ has a use currently
Devlin 2017/03/22 15:37:13 TODO sounds fine, but I don't see one added - am I
michaelpg 2017/03/22 20:46:10 Weird. Uploaded again and it's there (in the heade
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);
36 if (!extension_.get()) { 39 if (!extension_.get()) {
37 NOTREACHED(); 40 NOTREACHED();
38 return; 41 return;
39 } 42 }
40 } 43 }
41 44
42 if (is_running() || !enabled_checks || callback.is_null()) { 45 if (is_running() || !enabled_checks_ || callback.is_null()) {
43 NOTREACHED(); 46 NOTREACHED();
Devlin 2017/03/20 15:36:46 Unrelated to this CL, but usually we only want to
michaelpg 2017/03/22 00:17:12 Done.
44 return; 47 return;
45 } 48 }
46 49
47 running_checks_ = enabled_checks; 50 running_checks_ = enabled_checks_;
48 fail_fast_ = fail_fast;
49 callback_ = callback; 51 callback_ = callback;
50 ResetResults();
51 52
52 // Execute the management policy check first as it is synchronous. 53 // Execute the management policy check first as it is synchronous.
53 if (enabled_checks & CHECK_MANAGEMENT_POLICY) { 54 if (enabled_checks_ & CHECK_MANAGEMENT_POLICY) {
54 CheckManagementPolicy(); 55 CheckManagementPolicy();
55 if (!is_running()) 56 if (!is_running())
56 return; 57 return;
57 } 58 }
58 59
59 if (enabled_checks & CHECK_REQUIREMENTS) { 60 if (enabled_checks_ & CHECK_REQUIREMENTS) {
60 CheckRequirements(); 61 CheckRequirements();
61 if (!is_running()) 62 if (!is_running())
62 return; 63 return;
63 } 64 }
64 65
65 if (enabled_checks & CHECK_BLACKLIST) 66 if (enabled_checks_ & CHECK_BLACKLIST)
66 CheckBlacklistState(); 67 CheckBlacklistState();
67 } 68 }
68 69
69 void ExtensionInstallChecker::CheckManagementPolicy() { 70 void ExtensionInstallChecker::CheckManagementPolicy() {
70 DCHECK(extension_.get()); 71 DCHECK(extension_.get());
71 72
72 base::string16 error; 73 base::string16 error;
73 bool allow = ExtensionSystem::Get(profile_)->management_policy()->UserMayLoad( 74 bool allow = ExtensionSystem::Get(profile_)->management_policy()->UserMayLoad(
74 extension_.get(), &error); 75 extension_.get(), &error);
75 OnManagementPolicyCheckDone(allow, base::UTF16ToUTF8(error)); 76 OnManagementPolicyCheckDone(allow, base::UTF16ToUTF8(error));
76 } 77 }
77 78
78 void ExtensionInstallChecker::OnManagementPolicyCheckDone( 79 void ExtensionInstallChecker::OnManagementPolicyCheckDone(
79 bool allows_load, 80 bool allows_load,
80 const std::string& error) { 81 const std::string& error) {
81 policy_allows_load_ = allows_load; 82 policy_allows_load_ = allows_load;
82 policy_error_ = error; 83 policy_error_ = error;
83 DCHECK(policy_allows_load_ || !policy_error_.empty()); 84 DCHECK(policy_allows_load_ || !policy_error_.empty());
84 85
85 running_checks_ &= ~CHECK_MANAGEMENT_POLICY; 86 running_checks_ &= ~CHECK_MANAGEMENT_POLICY;
86 MaybeInvokeCallback(); 87 MaybeInvokeCallback();
87 } 88 }
88 89
89 void ExtensionInstallChecker::CheckRequirements() { 90 void ExtensionInstallChecker::CheckRequirements() {
90 DCHECK(extension_.get()); 91 DCHECK(extension_.get());
91 92
92 if (!requirements_checker_.get()) 93 if (!requirements_checker_.get())
93 requirements_checker_.reset(new ChromeRequirementsChecker()); 94 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 // Some pending results may arrive after fail fast.
105 if (sequence_number != current_sequence_number_) 103 if (!is_running())
Devlin 2017/03/20 15:36:46 Actually, I think we can make this (D)CHECK is_run
michaelpg 2017/03/22 00:17:12 Done.
106 return; 104 return;
107 105
108 requirement_errors_ = errors; 106 requirement_errors_ = errors;
109 107
110 running_checks_ &= ~CHECK_REQUIREMENTS; 108 running_checks_ &= ~CHECK_REQUIREMENTS;
111 MaybeInvokeCallback(); 109 MaybeInvokeCallback();
112 } 110 }
113 111
114 void ExtensionInstallChecker::CheckBlacklistState() { 112 void ExtensionInstallChecker::CheckBlacklistState() {
115 DCHECK(extension_.get()); 113 DCHECK(extension_.get());
116 114
117 extensions::Blacklist* blacklist = Blacklist::Get(profile_); 115 extensions::Blacklist* blacklist = Blacklist::Get(profile_);
118 blacklist->IsBlacklisted( 116 blacklist->IsBlacklisted(
119 extension_->id(), 117 extension_->id(),
120 base::Bind(&ExtensionInstallChecker::OnBlacklistStateCheckDone, 118 base::Bind(&ExtensionInstallChecker::OnBlacklistStateCheckDone,
121 weak_ptr_factory_.GetWeakPtr(), 119 weak_ptr_factory_.GetWeakPtr()));
122 current_sequence_number_));
123 } 120 }
124 121
125 void ExtensionInstallChecker::OnBlacklistStateCheckDone(int sequence_number, 122 void ExtensionInstallChecker::OnBlacklistStateCheckDone(BlacklistState state) {
126 BlacklistState state) {
127 // Some pending results may arrive after fail fast. 123 // Some pending results may arrive after fail fast.
128 if (sequence_number != current_sequence_number_) 124 if (!is_running())
Devlin 2017/03/20 15:36:46 ditto
michaelpg 2017/03/22 00:17:12 Done.
129 return; 125 return;
130 126
131 blacklist_state_ = state; 127 blacklist_state_ = state;
132 128
133 running_checks_ &= ~CHECK_BLACKLIST; 129 running_checks_ &= ~CHECK_BLACKLIST;
134 MaybeInvokeCallback(); 130 MaybeInvokeCallback();
135 } 131 }
136 132
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() { 133 void ExtensionInstallChecker::MaybeInvokeCallback() {
145 if (callback_.is_null()) 134 if (callback_.is_null())
146 return; 135 return;
147 136
148 // Set bits for failed checks. 137 // Set bits for failed checks.
149 int failed_mask = 0; 138 int failed_mask = 0;
150 if (blacklist_state_ == BLACKLISTED_MALWARE) 139 if (blacklist_state_ == BLACKLISTED_MALWARE)
151 failed_mask |= CHECK_BLACKLIST; 140 failed_mask |= CHECK_BLACKLIST;
152 if (!requirement_errors_.empty()) 141 if (!requirement_errors_.empty())
153 failed_mask |= CHECK_REQUIREMENTS; 142 failed_mask |= CHECK_REQUIREMENTS;
154 if (!policy_allows_load_) 143 if (!policy_allows_load_)
155 failed_mask |= CHECK_MANAGEMENT_POLICY; 144 failed_mask |= CHECK_MANAGEMENT_POLICY;
156 145
157 // Invoke callback if all checks are complete or there was at least one 146 // Invoke callback if all checks are complete or there was at least one
158 // failure and |fail_fast_| is true. 147 // failure and |fail_fast_| is true.
159 if (!is_running() || (failed_mask && fail_fast_)) { 148 if (!is_running() || (failed_mask && fail_fast_)) {
160 // If we are failing fast, discard any pending results. 149 // If we are failing fast, discard any pending results.
161 weak_ptr_factory_.InvalidateWeakPtrs(); 150 weak_ptr_factory_.InvalidateWeakPtrs();
162 running_checks_ = 0; 151 running_checks_ = 0;
163 ++current_sequence_number_; 152 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 } 153 }
172 } 154 }
173 155
174 } // namespace extensions 156 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698