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 21 matching lines...) Expand all Loading... | |
| 32 return base::Uint64ToString(hash); | 32 return base::Uint64ToString(hash); |
| 33 } | 33 } |
| 34 | 34 |
| 35 } // anonymous namespace | 35 } // anonymous namespace |
| 36 | 36 |
| 37 // static | 37 // static |
| 38 void WebApkIconHasher::DownloadAndComputeMurmur2Hash( | 38 void WebApkIconHasher::DownloadAndComputeMurmur2Hash( |
| 39 net::URLRequestContextGetter* request_context_getter, | 39 net::URLRequestContextGetter* request_context_getter, |
| 40 const GURL& icon_url, | 40 const GURL& icon_url, |
| 41 const Murmur2HashCallback& callback) { | 41 const Murmur2HashCallback& callback) { |
| 42 DownloadAndComputeMurmur2HashWithTimeout(request_context_getter, icon_url, | |
| 43 kDownloadTimeoutInMilliseconds, | |
| 44 callback); | |
| 45 } | |
| 46 | |
| 47 // static | |
| 48 void WebApkIconHasher::DownloadAndComputeMurmur2HashWithTimeout( | |
|
dominickn
2017/04/19 00:04:07
Nit: I would call this DownloadAndComputeMurmur2Ha
pkotwicz
2017/04/19 20:34:52
I don't thinkat that DownloadAndComputeMurmur2Hash
| |
| 49 net::URLRequestContextGetter* request_context_getter, | |
| 50 const GURL& icon_url, | |
| 51 int timeout_ms, | |
| 52 const Murmur2HashCallback& callback) { | |
| 42 if (!icon_url.is_valid()) { | 53 if (!icon_url.is_valid()) { |
| 43 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 54 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 44 base::Bind(callback, "")); | 55 base::Bind(callback, "")); |
| 45 return; | 56 return; |
| 46 } | 57 } |
| 47 | 58 |
| 48 if (icon_url.SchemeIs(url::kDataScheme)) { | 59 if (icon_url.SchemeIs(url::kDataScheme)) { |
| 49 std::string mime_type, char_set, data; | 60 std::string mime_type, char_set, data; |
| 50 std::string hash; | 61 std::string hash; |
| 51 if (net::DataURL::Parse(icon_url, &mime_type, &char_set, &data) && | 62 if (net::DataURL::Parse(icon_url, &mime_type, &char_set, &data) && |
| 52 !data.empty()) { | 63 !data.empty()) { |
| 53 hash = ComputeMurmur2Hash(data); | 64 hash = ComputeMurmur2Hash(data); |
| 54 } | 65 } |
| 55 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 66 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 56 base::Bind(callback, hash)); | 67 base::Bind(callback, hash)); |
| 57 return; | 68 return; |
| 58 } | 69 } |
| 59 | 70 |
| 60 // The icon hasher will delete itself when it is done. | 71 // The icon hasher will delete itself when it is done. |
| 61 new WebApkIconHasher(request_context_getter, icon_url, callback); | 72 new WebApkIconHasher(request_context_getter, icon_url, timeout_ms, callback); |
| 62 } | 73 } |
| 63 | 74 |
| 64 WebApkIconHasher::WebApkIconHasher( | 75 WebApkIconHasher::WebApkIconHasher( |
| 65 net::URLRequestContextGetter* url_request_context_getter, | 76 net::URLRequestContextGetter* url_request_context_getter, |
| 66 const GURL& icon_url, | 77 const GURL& icon_url, |
| 78 int timeout_ms, | |
| 67 const Murmur2HashCallback& callback) | 79 const Murmur2HashCallback& callback) |
| 68 : callback_(callback) { | 80 : callback_(callback) { |
| 69 download_timeout_timer_.Start( | 81 download_timeout_timer_.Start( |
| 70 FROM_HERE, | 82 FROM_HERE, base::TimeDelta::FromMilliseconds(timeout_ms), |
| 71 base::TimeDelta::FromMilliseconds(kDownloadTimeoutInMilliseconds), | |
| 72 base::Bind(&WebApkIconHasher::OnDownloadTimedOut, | 83 base::Bind(&WebApkIconHasher::OnDownloadTimedOut, |
| 73 base::Unretained(this))); | 84 base::Unretained(this))); |
| 74 | 85 |
| 75 url_fetcher_ = net::URLFetcher::Create(icon_url, net::URLFetcher::GET, this); | 86 url_fetcher_ = net::URLFetcher::Create(icon_url, net::URLFetcher::GET, this); |
| 76 url_fetcher_->SetRequestContext(url_request_context_getter); | 87 url_fetcher_->SetRequestContext(url_request_context_getter); |
| 77 url_fetcher_->Start(); | 88 url_fetcher_->Start(); |
| 78 } | 89 } |
| 79 | 90 |
| 80 WebApkIconHasher::~WebApkIconHasher() {} | 91 WebApkIconHasher::~WebApkIconHasher() {} |
| 81 | 92 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 100 void WebApkIconHasher::OnDownloadTimedOut() { | 111 void WebApkIconHasher::OnDownloadTimedOut() { |
| 101 url_fetcher_.reset(); | 112 url_fetcher_.reset(); |
| 102 | 113 |
| 103 RunCallback(""); | 114 RunCallback(""); |
| 104 } | 115 } |
| 105 | 116 |
| 106 void WebApkIconHasher::RunCallback(const std::string& icon_murmur2_hash) { | 117 void WebApkIconHasher::RunCallback(const std::string& icon_murmur2_hash) { |
| 107 callback_.Run(icon_murmur2_hash); | 118 callback_.Run(icon_murmur2_hash); |
| 108 delete this; | 119 delete this; |
| 109 } | 120 } |
| OLD | NEW |