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

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

Issue 436563004: Fix for 0-length file problem in content verification (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixes from review comments Created 6 years, 4 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/computed_hashes_unittest.cc ('k') | no next file » | 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_hash_fetcher.h" 5 #include "extensions/browser/content_hash_fetcher.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/files/file_enumerator.h" 11 #include "base/files/file_enumerator.h"
12 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
15 #include "base/stl_util.h"
16 #include "base/synchronization/lock.h" 15 #include "base/synchronization/lock.h"
17 #include "base/task_runner_util.h" 16 #include "base/task_runner_util.h"
18 #include "base/timer/elapsed_timer.h" 17 #include "base/timer/elapsed_timer.h"
19 #include "base/version.h" 18 #include "base/version.h"
20 #include "content/public/browser/browser_context.h" 19 #include "content/public/browser/browser_context.h"
21 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
22 #include "crypto/secure_hash.h"
23 #include "crypto/sha2.h" 21 #include "crypto/sha2.h"
24 #include "extensions/browser/computed_hashes.h" 22 #include "extensions/browser/computed_hashes.h"
25 #include "extensions/browser/content_hash_tree.h" 23 #include "extensions/browser/content_hash_tree.h"
26 #include "extensions/browser/content_verifier_delegate.h" 24 #include "extensions/browser/content_verifier_delegate.h"
27 #include "extensions/browser/extension_registry.h" 25 #include "extensions/browser/extension_registry.h"
28 #include "extensions/browser/verified_contents.h" 26 #include "extensions/browser/verified_contents.h"
29 #include "extensions/common/constants.h" 27 #include "extensions/common/constants.h"
30 #include "extensions/common/extension.h" 28 #include "extensions/common/extension.h"
31 #include "extensions/common/file_util.h" 29 #include "extensions/common/file_util.h"
32 #include "net/base/load_flags.h" 30 #include "net/base/load_flags.h"
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 358
361 std::string contents; 359 std::string contents;
362 if (!base::ReadFileToString(full_path, &contents)) { 360 if (!base::ReadFileToString(full_path, &contents)) {
363 LOG(ERROR) << "Could not read " << full_path.MaybeAsASCII(); 361 LOG(ERROR) << "Could not read " << full_path.MaybeAsASCII();
364 continue; 362 continue;
365 } 363 }
366 364
367 // Iterate through taking the hash of each block of size (block_size_) of 365 // Iterate through taking the hash of each block of size (block_size_) of
368 // the file. 366 // the file.
369 std::vector<std::string> hashes; 367 std::vector<std::string> hashes;
370 size_t offset = 0; 368 ComputedHashes::ComputeHashesForContent(contents, block_size_, &hashes);
371 while (offset < contents.size()) {
372 if (IsCancelled())
373 return false;
374 const char* block_start = contents.data() + offset;
375 size_t bytes_to_read =
376 std::min(contents.size() - offset, static_cast<size_t>(block_size_));
377 DCHECK(bytes_to_read > 0);
378 scoped_ptr<crypto::SecureHash> hash(
379 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
380 hash->Update(block_start, bytes_to_read);
381
382 hashes.push_back(std::string());
383 std::string* buffer = &hashes.back();
384 buffer->resize(crypto::kSHA256Length);
385 hash->Finish(string_as_array(buffer), buffer->size());
386
387 // Get ready for next iteration.
388 offset += bytes_to_read;
389 }
390 std::string root = 369 std::string root =
391 ComputeTreeHashRoot(hashes, block_size_ / crypto::kSHA256Length); 370 ComputeTreeHashRoot(hashes, block_size_ / crypto::kSHA256Length);
392 if (expected_root && *expected_root != root) { 371 if (expected_root && *expected_root != root) {
393 VLOG(1) << "content mismatch for " << relative_path.AsUTF8Unsafe(); 372 VLOG(1) << "content mismatch for " << relative_path.AsUTF8Unsafe();
394 hash_mismatch_paths_.insert(relative_path); 373 hash_mismatch_paths_.insert(relative_path);
395 continue; 374 continue;
396 } 375 }
397 376
398 writer.AddHashes(relative_path, block_size_, hashes); 377 writer.AddHashes(relative_path, block_size_, hashes);
399 } 378 }
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 470
492 for (JobMap::iterator i = jobs_.begin(); i != jobs_.end(); ++i) { 471 for (JobMap::iterator i = jobs_.begin(); i != jobs_.end(); ++i) {
493 if (i->second.get() == job) { 472 if (i->second.get() == job) {
494 jobs_.erase(i); 473 jobs_.erase(i);
495 break; 474 break;
496 } 475 }
497 } 476 }
498 } 477 }
499 478
500 } // namespace extensions 479 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/computed_hashes_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698