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

Side by Side Diff: extensions/browser/content_verifier.cc

Issue 407043002: Content Verification: Don't access UI-thread objects on the IO thread (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: added DCHECK Created 6 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « extensions/browser/content_verifier.h ('k') | extensions/browser/content_verifier_delegate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "extensions/browser/content_verifier.h" 5 #include "extensions/browser/content_verifier.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
12 #include "extensions/browser/content_hash_fetcher.h" 12 #include "extensions/browser/content_hash_fetcher.h"
13 #include "extensions/browser/content_hash_reader.h" 13 #include "extensions/browser/content_hash_reader.h"
14 #include "extensions/browser/content_verifier_delegate.h" 14 #include "extensions/browser/content_verifier_delegate.h"
15 #include "extensions/browser/content_verifier_io_data.h"
15 #include "extensions/browser/extension_registry.h" 16 #include "extensions/browser/extension_registry.h"
16 #include "extensions/common/constants.h" 17 #include "extensions/common/constants.h"
17 #include "extensions/common/extension_l10n_util.h" 18 #include "extensions/common/extension_l10n_util.h"
18 19
19 namespace extensions { 20 namespace extensions {
20 21
21 ContentVerifier::ContentVerifier(content::BrowserContext* context, 22 ContentVerifier::ContentVerifier(content::BrowserContext* context,
22 ContentVerifierDelegate* delegate) 23 ContentVerifierDelegate* delegate)
23 : context_(context), 24 : shutdown_(false),
25 context_(context),
24 delegate_(delegate), 26 delegate_(delegate),
25 fetcher_(new ContentHashFetcher( 27 fetcher_(new ContentHashFetcher(
26 context, 28 context,
27 delegate, 29 delegate,
28 base::Bind(&ContentVerifier::OnFetchComplete, this))) { 30 base::Bind(&ContentVerifier::OnFetchComplete, this))),
31 observer_(this),
32 io_data_(new ContentVerifierIOData) {
29 } 33 }
30 34
31 ContentVerifier::~ContentVerifier() { 35 ContentVerifier::~ContentVerifier() {
32 } 36 }
33 37
34 void ContentVerifier::Start() { 38 void ContentVerifier::Start() {
35 fetcher_->Start(); 39 ExtensionRegistry* registry = ExtensionRegistry::Get(context_);
40 observer_.Add(registry);
36 } 41 }
37 42
38 void ContentVerifier::Shutdown() { 43 void ContentVerifier::Shutdown() {
44 shutdown_ = true;
45 content::BrowserThread::PostTask(
46 content::BrowserThread::IO,
47 FROM_HERE,
48 base::Bind(&ContentVerifierIOData::Clear, io_data_));
49 observer_.RemoveAll();
39 fetcher_.reset(); 50 fetcher_.reset();
40 delegate_.reset();
41 } 51 }
42 52
43 ContentVerifyJob* ContentVerifier::CreateJobFor( 53 ContentVerifyJob* ContentVerifier::CreateJobFor(
44 const std::string& extension_id, 54 const std::string& extension_id,
45 const base::FilePath& extension_root, 55 const base::FilePath& extension_root,
46 const base::FilePath& relative_path) { 56 const base::FilePath& relative_path) {
47 if (!fetcher_ || !delegate_) 57 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
58
59 const ContentVerifierIOData::ExtensionData* data =
60 io_data_->GetData(extension_id);
61 if (!data)
48 return NULL; 62 return NULL;
49 63
50 ExtensionRegistry* registry = ExtensionRegistry::Get(context_);
51 const Extension* extension =
52 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING);
53
54 std::set<base::FilePath> paths; 64 std::set<base::FilePath> paths;
55 paths.insert(relative_path); 65 paths.insert(relative_path);
56 if (!ShouldVerifyAnyPaths(extension, paths)) 66 if (!ShouldVerifyAnyPaths(extension_id, extension_root, paths))
57 return NULL; 67 return NULL;
58 68
59 // TODO(asargent) - we can probably get some good performance wins by having 69 // TODO(asargent) - we can probably get some good performance wins by having
60 // a cache of ContentHashReader's that we hold onto past the end of each job. 70 // a cache of ContentHashReader's that we hold onto past the end of each job.
61 return new ContentVerifyJob( 71 return new ContentVerifyJob(
62 new ContentHashReader(extension_id, 72 new ContentHashReader(extension_id,
63 *extension->version(), 73 data->version,
64 extension_root, 74 extension_root,
65 relative_path, 75 relative_path,
66 delegate_->PublicKey()), 76 delegate_->PublicKey()),
67 base::Bind(&ContentVerifier::VerifyFailed, this, extension->id())); 77 base::Bind(&ContentVerifier::VerifyFailed, this, extension_id));
68 } 78 }
69 79
70 void ContentVerifier::VerifyFailed(const std::string& extension_id, 80 void ContentVerifier::VerifyFailed(const std::string& extension_id,
71 ContentVerifyJob::FailureReason reason) { 81 ContentVerifyJob::FailureReason reason) {
72 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { 82 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
73 content::BrowserThread::PostTask( 83 content::BrowserThread::PostTask(
74 content::BrowserThread::UI, 84 content::BrowserThread::UI,
75 FROM_HERE, 85 FROM_HERE,
76 base::Bind(&ContentVerifier::VerifyFailed, this, extension_id, reason)); 86 base::Bind(&ContentVerifier::VerifyFailed, this, extension_id, reason));
77 return; 87 return;
78 } 88 }
89 if (shutdown_)
90 return;
79 91
80 VLOG(1) << "VerifyFailed " << extension_id << " reason:" << reason; 92 VLOG(1) << "VerifyFailed " << extension_id << " reason:" << reason;
81 93
82 ExtensionRegistry* registry = ExtensionRegistry::Get(context_); 94 ExtensionRegistry* registry = ExtensionRegistry::Get(context_);
83 const Extension* extension = 95 const Extension* extension =
84 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING); 96 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING);
85 97
86 if (!delegate_ || !extension) 98 if (!extension)
87 return;
88
89 ContentVerifierDelegate::Mode mode = delegate_->ShouldBeVerified(*extension);
90 if (mode < ContentVerifierDelegate::ENFORCE)
91 return; 99 return;
92 100
93 if (reason == ContentVerifyJob::MISSING_ALL_HASHES) { 101 if (reason == ContentVerifyJob::MISSING_ALL_HASHES) {
94 // If we failed because there were no hashes yet for this extension, just 102 // If we failed because there were no hashes yet for this extension, just
95 // request some. 103 // request some.
96 fetcher_->DoFetch(extension, true /* force */); 104 fetcher_->DoFetch(extension, true /* force */);
97 } else { 105 } else {
98 delegate_->VerifyFailed(extension_id); 106 delegate_->VerifyFailed(extension_id);
99 } 107 }
100 } 108 }
101 109
110 void ContentVerifier::OnExtensionLoaded(
111 content::BrowserContext* browser_context,
112 const Extension* extension) {
113 if (shutdown_)
114 return;
115
116 ContentVerifierDelegate::Mode mode = delegate_->ShouldBeVerified(*extension);
117 if (mode != ContentVerifierDelegate::NONE) {
118 scoped_ptr<ContentVerifierIOData::ExtensionData> data(
119 new ContentVerifierIOData::ExtensionData(
120 delegate_->GetBrowserImagePaths(extension),
121 extension->version() ? *extension->version() : base::Version()));
122 content::BrowserThread::PostTask(content::BrowserThread::IO,
123 FROM_HERE,
124 base::Bind(&ContentVerifierIOData::AddData,
125 io_data_,
126 extension->id(),
127 base::Passed(&data)));
128 fetcher_->ExtensionLoaded(extension);
129 }
130 }
131
132 void ContentVerifier::OnExtensionUnloaded(
133 content::BrowserContext* browser_context,
134 const Extension* extension,
135 UnloadedExtensionInfo::Reason reason) {
136 if (shutdown_)
137 return;
138 content::BrowserThread::PostTask(
139 content::BrowserThread::IO,
140 FROM_HERE,
141 base::Bind(
142 &ContentVerifierIOData::RemoveData, io_data_, extension->id()));
143 if (fetcher_)
144 fetcher_->ExtensionUnloaded(extension);
145 }
146
147 void ContentVerifier::OnFetchCompleteHelper(const std::string& extension_id,
148 bool shouldVerifyAnyPathsResult) {
149 if (shouldVerifyAnyPathsResult)
150 delegate_->VerifyFailed(extension_id);
151 }
152
102 void ContentVerifier::OnFetchComplete( 153 void ContentVerifier::OnFetchComplete(
103 const std::string& extension_id, 154 const std::string& extension_id,
104 bool success, 155 bool success,
105 bool was_force_check, 156 bool was_force_check,
106 const std::set<base::FilePath>& hash_mismatch_paths) { 157 const std::set<base::FilePath>& hash_mismatch_paths) {
158 if (shutdown_)
159 return;
160
107 VLOG(1) << "OnFetchComplete " << extension_id << " success:" << success; 161 VLOG(1) << "OnFetchComplete " << extension_id << " success:" << success;
108 162
109 ExtensionRegistry* registry = ExtensionRegistry::Get(context_); 163 ExtensionRegistry* registry = ExtensionRegistry::Get(context_);
110 const Extension* extension = 164 const Extension* extension =
111 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING); 165 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING);
112 if (!delegate_ || !extension) 166 if (!delegate_ || !extension)
113 return; 167 return;
114 168
115 ContentVerifierDelegate::Mode mode = delegate_->ShouldBeVerified(*extension); 169 ContentVerifierDelegate::Mode mode = delegate_->ShouldBeVerified(*extension);
116 if (mode < ContentVerifierDelegate::ENFORCE) 170 if (was_force_check && !success &&
117 return; 171 mode == ContentVerifierDelegate::ENFORCE_STRICT) {
118 172 // We weren't able to get verified_contents.json or weren't able to compute
119 if (!success && mode < ContentVerifierDelegate::ENFORCE_STRICT) 173 // hashes.
120 return;
121
122 if ((was_force_check && !success) ||
123 ShouldVerifyAnyPaths(extension, hash_mismatch_paths))
124 delegate_->VerifyFailed(extension_id); 174 delegate_->VerifyFailed(extension_id);
175 } else {
176 content::BrowserThread::PostTaskAndReplyWithResult(
177 content::BrowserThread::IO,
178 FROM_HERE,
179 base::Bind(&ContentVerifier::ShouldVerifyAnyPaths,
180 this,
181 extension_id,
182 extension->path(),
183 hash_mismatch_paths),
184 base::Bind(
185 &ContentVerifier::OnFetchCompleteHelper, this, extension_id));
186 }
125 } 187 }
126 188
127 bool ContentVerifier::ShouldVerifyAnyPaths( 189 bool ContentVerifier::ShouldVerifyAnyPaths(
128 const Extension* extension, 190 const std::string& extension_id,
191 const base::FilePath& extension_root,
129 const std::set<base::FilePath>& relative_paths) { 192 const std::set<base::FilePath>& relative_paths) {
130 if (!delegate_ || !extension || !extension->version()) 193 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
194 const ContentVerifierIOData::ExtensionData* data =
195 io_data_->GetData(extension_id);
196 if (!data)
131 return false; 197 return false;
132 198
133 ContentVerifierDelegate::Mode mode = delegate_->ShouldBeVerified(*extension); 199 const std::set<base::FilePath>& browser_images = data->browser_image_paths;
134 if (mode < ContentVerifierDelegate::ENFORCE)
135 return false;
136 200
137 // Images used in the browser get transcoded during install, so skip 201 base::FilePath locales_dir = extension_root.Append(kLocaleFolder);
138 // checking them for now. TODO(asargent) - see if we can cache this list
139 // for a given extension id/version pair.
140 std::set<base::FilePath> browser_images =
141 delegate_->GetBrowserImagePaths(extension);
142
143 base::FilePath locales_dir = extension->path().Append(kLocaleFolder);
144 scoped_ptr<std::set<std::string> > all_locales; 202 scoped_ptr<std::set<std::string> > all_locales;
145 203
146 for (std::set<base::FilePath>::const_iterator i = relative_paths.begin(); 204 for (std::set<base::FilePath>::const_iterator i = relative_paths.begin();
147 i != relative_paths.end(); 205 i != relative_paths.end();
148 ++i) { 206 ++i) {
149 const base::FilePath& relative_path = *i; 207 const base::FilePath& relative_path = *i;
150 208
151 if (relative_path == base::FilePath(kManifestFilename)) 209 if (relative_path == base::FilePath(kManifestFilename))
152 continue; 210 continue;
153 211
154 if (ContainsKey(browser_images, relative_path)) 212 if (ContainsKey(browser_images, relative_path))
155 continue; 213 continue;
156 214
157 base::FilePath full_path = extension->path().Append(relative_path); 215 base::FilePath full_path = extension_root.Append(relative_path);
158 if (locales_dir.IsParent(full_path)) { 216 if (locales_dir.IsParent(full_path)) {
159 if (!all_locales) { 217 if (!all_locales) {
160 // TODO(asargent) - see if we can cache this list longer to avoid 218 // TODO(asargent) - see if we can cache this list longer to avoid
161 // having to fetch it more than once for a given run of the 219 // having to fetch it more than once for a given run of the
162 // browser. Maybe it can never change at runtime? (Or if it can, maybe 220 // browser. Maybe it can never change at runtime? (Or if it can, maybe
163 // there is an event we can listen for to know to drop our cache). 221 // there is an event we can listen for to know to drop our cache).
164 all_locales.reset(new std::set<std::string>); 222 all_locales.reset(new std::set<std::string>);
165 extension_l10n_util::GetAllLocales(all_locales.get()); 223 extension_l10n_util::GetAllLocales(all_locales.get());
166 } 224 }
167 225
168 // Since message catalogs get transcoded during installation, we want 226 // Since message catalogs get transcoded during installation, we want
169 // to skip those paths. 227 // to skip those paths.
170 if (full_path.DirName().DirName() == locales_dir && 228 if (full_path.DirName().DirName() == locales_dir &&
171 !extension_l10n_util::ShouldSkipValidation( 229 !extension_l10n_util::ShouldSkipValidation(
172 locales_dir, full_path.DirName(), *all_locales)) 230 locales_dir, full_path.DirName(), *all_locales))
173 continue; 231 continue;
174 } 232 }
175 return true; 233 return true;
176 } 234 }
177 return false; 235 return false;
178 } 236 }
179 237
180 } // namespace extensions 238 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/content_verifier.h ('k') | extensions/browser/content_verifier_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698