Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/android/webapk/webapk_icon_hasher.h" | 5 #include "chrome/browser/android/webapk/webapk_icon_hasher.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "net/base/data_url.h" | 10 #include "net/base/data_url.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 // image's raw, unsanitized bytes from the web. |raw_image_data| may contain | 25 // image's raw, unsanitized bytes from the web. |raw_image_data| may contain |
| 26 // malicious data. Decoding unsanitized bitmap data to an SkBitmap in the | 26 // malicious data. Decoding unsanitized bitmap data to an SkBitmap in the |
| 27 // browser process is a security bug. | 27 // browser process is a security bug. |
| 28 uint64_t hash = MurmurHash64A(&raw_image_data.front(), raw_image_data.size(), | 28 uint64_t hash = MurmurHash64A(&raw_image_data.front(), raw_image_data.size(), |
| 29 kMurmur2HashSeed); | 29 kMurmur2HashSeed); |
| 30 return base::Uint64ToString(hash); | 30 return base::Uint64ToString(hash); |
| 31 } | 31 } |
| 32 | 32 |
| 33 } // anonymous namespace | 33 } // anonymous namespace |
| 34 | 34 |
| 35 WebApkIconHasher::WebApkIconHasher() {} | 35 WebApkIconHasher::WebApkIconHasher( |
| 36 net::URLRequestContextGetter* url_request_context_getter, | |
| 37 const GURL& icon_url, | |
| 38 const Murmur2HashCallback& callback) | |
| 39 : url_request_context_getter_(url_request_context_getter), | |
| 40 icon_url_(icon_url), | |
| 41 callback_(callback), | |
| 42 is_download_complete_(false), | |
| 43 weak_ptr_factory_(this) {} | |
| 36 | 44 |
| 37 WebApkIconHasher::~WebApkIconHasher() {} | 45 WebApkIconHasher::~WebApkIconHasher() {} |
| 38 | 46 |
| 39 void WebApkIconHasher::DownloadAndComputeMurmur2Hash( | 47 void WebApkIconHasher::DownloadAndComputeMurmur2Hash() { |
| 40 net::URLRequestContextGetter* request_context_getter, | 48 if (!icon_url_.is_valid()) { |
| 41 const GURL& icon_url, | 49 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 42 const Murmur2HashCallback& callback) { | 50 base::Bind(callback_, "")); |
| 43 if (icon_url.SchemeIs(url::kDataScheme)) { | 51 return; |
| 52 } | |
| 53 | |
| 54 if (icon_url_.SchemeIs(url::kDataScheme)) { | |
| 44 std::string mime_type, char_set, data; | 55 std::string mime_type, char_set, data; |
| 45 std::string hash; | 56 std::string hash; |
| 46 if (net::DataURL::Parse(icon_url, &mime_type, &char_set, &data) && | 57 if (net::DataURL::Parse(icon_url_, &mime_type, &char_set, &data) && |
| 47 !data.empty()) { | 58 !data.empty()) { |
| 48 hash = ComputeMurmur2Hash(data); | 59 hash = ComputeMurmur2Hash(data); |
| 49 } | 60 } |
| 50 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 61 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 51 base::Bind(callback, hash)); | 62 base::Bind(callback_, hash)); |
| 52 return; | 63 return; |
| 53 } | 64 } |
| 54 | 65 |
| 55 callback_ = callback; | 66 download_timeout_timer_.Start( |
| 56 url_fetcher_ = net::URLFetcher::Create(icon_url, net::URLFetcher::GET, this); | 67 FROM_HERE, base::TimeDelta::FromMilliseconds(60000), |
|
dominickn
2017/03/24 02:41:00
Can you put this constant in the anonymous namespa
F
2017/03/27 20:37:08
Done.
| |
| 57 url_fetcher_->SetRequestContext(request_context_getter); | 68 base::Bind(&WebApkIconHasher::OnDownloadTimedOut, |
| 69 weak_ptr_factory_.GetWeakPtr())); | |
| 70 | |
| 71 url_fetcher_ = net::URLFetcher::Create(icon_url_, net::URLFetcher::GET, this); | |
| 72 url_fetcher_->SetRequestContext(url_request_context_getter_); | |
| 58 url_fetcher_->Start(); | 73 url_fetcher_->Start(); |
| 59 } | 74 } |
| 60 | 75 |
| 61 void WebApkIconHasher::OnURLFetchComplete(const net::URLFetcher* source) { | 76 void WebApkIconHasher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 77 if (is_download_complete_) | |
| 78 return; | |
| 79 | |
| 80 is_download_complete_ = true; | |
| 81 | |
| 82 download_timeout_timer_.Stop(); | |
|
dominickn
2017/03/24 02:41:00
Stop the timer first. Remove the extra newline bet
F
2017/03/27 20:37:08
Done.
| |
| 83 | |
| 62 if (!source->GetStatus().is_success() || | 84 if (!source->GetStatus().is_success() || |
| 63 source->GetResponseCode() != net::HTTP_OK) { | 85 source->GetResponseCode() != net::HTTP_OK) { |
| 64 callback_.Run(""); | 86 callback_.Run(""); |
| 65 return; | 87 return; |
| 66 } | 88 } |
| 67 | 89 |
| 68 // WARNING: We are running in the browser process. |raw_image_data| is the | 90 // WARNING: We are running in the browser process. |raw_image_data| is the |
| 69 // image's raw, unsanitized bytes from the web. |raw_image_data| may contain | 91 // image's raw, unsanitized bytes from the web. |raw_image_data| may contain |
| 70 // malicious data. Decoding unsanitized bitmap data to an SkBitmap in the | 92 // malicious data. Decoding unsanitized bitmap data to an SkBitmap in the |
| 71 // browser process is a security bug. | 93 // browser process is a security bug. |
| 72 std::string raw_image_data; | 94 std::string raw_image_data; |
| 73 source->GetResponseAsString(&raw_image_data); | 95 source->GetResponseAsString(&raw_image_data); |
| 74 callback_.Run(ComputeMurmur2Hash(raw_image_data)); | 96 callback_.Run(ComputeMurmur2Hash(raw_image_data)); |
| 75 } | 97 } |
| 98 | |
| 99 void WebApkIconHasher::OnDownloadTimedOut() { | |
| 100 if (is_download_complete_) | |
| 101 return; | |
| 102 | |
| 103 is_download_complete_ = true; | |
| 104 | |
|
dominickn
2017/03/24 02:41:00
Remove the extra newline
F
2017/03/27 20:37:08
Done.
| |
| 105 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 106 base::Bind(callback_, "")); | |
| 107 } | |
| OLD | NEW |