| 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 26 matching lines...) Expand all Loading... |
| 37 WebApkIconHasher::WebApkIconHasher( | 37 WebApkIconHasher::WebApkIconHasher( |
| 38 net::URLRequestContextGetter* url_request_context_getter, | 38 net::URLRequestContextGetter* url_request_context_getter, |
| 39 const GURL& icon_url, | 39 const GURL& icon_url, |
| 40 const Murmur2HashCallback& callback) | 40 const Murmur2HashCallback& callback) |
| 41 : url_request_context_getter_(url_request_context_getter), | 41 : url_request_context_getter_(url_request_context_getter), |
| 42 icon_url_(icon_url), | 42 icon_url_(icon_url), |
| 43 callback_(callback) {} | 43 callback_(callback) {} |
| 44 | 44 |
| 45 WebApkIconHasher::~WebApkIconHasher() {} | 45 WebApkIconHasher::~WebApkIconHasher() {} |
| 46 | 46 |
| 47 // static |
| 48 void WebApkIconHasher::DownloadAndComputeMurmur2Hash( |
| 49 net::URLRequestContextGetter* request_context_getter, |
| 50 const GURL& icon_url, |
| 51 const Murmur2HashCallback& callback) { |
| 52 // The icon hasher will delete itself when it is done. |
| 53 WebApkIconHasher* webapk_icon_hasher = |
| 54 new WebApkIconHasher(request_context_getter, icon_url, callback); |
| 55 webapk_icon_hasher->DownloadAndComputeMurmur2Hash(); |
| 56 } |
| 57 |
| 47 void WebApkIconHasher::DownloadAndComputeMurmur2Hash() { | 58 void WebApkIconHasher::DownloadAndComputeMurmur2Hash() { |
| 48 if (!icon_url_.is_valid()) { | 59 if (!icon_url_.is_valid()) { |
| 49 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 60 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 50 base::Bind(callback_, "")); | 61 base::Bind(callback_, "")); |
| 51 return; | 62 return; |
| 52 } | 63 } |
| 53 | 64 |
| 54 if (icon_url_.SchemeIs(url::kDataScheme)) { | 65 if (icon_url_.SchemeIs(url::kDataScheme)) { |
| 55 std::string mime_type, char_set, data; | 66 std::string mime_type, char_set, data; |
| 56 std::string hash; | 67 std::string hash; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 73 url_fetcher_->SetRequestContext(url_request_context_getter_); | 84 url_fetcher_->SetRequestContext(url_request_context_getter_); |
| 74 url_fetcher_->Start(); | 85 url_fetcher_->Start(); |
| 75 } | 86 } |
| 76 | 87 |
| 77 void WebApkIconHasher::OnURLFetchComplete(const net::URLFetcher* source) { | 88 void WebApkIconHasher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 78 download_timeout_timer_.Stop(); | 89 download_timeout_timer_.Stop(); |
| 79 | 90 |
| 80 if (!source->GetStatus().is_success() || | 91 if (!source->GetStatus().is_success() || |
| 81 source->GetResponseCode() != net::HTTP_OK) { | 92 source->GetResponseCode() != net::HTTP_OK) { |
| 82 callback_.Run(""); | 93 callback_.Run(""); |
| 94 delete this; |
| 83 return; | 95 return; |
| 84 } | 96 } |
| 85 | 97 |
| 86 // WARNING: We are running in the browser process. |raw_image_data| is the | 98 // WARNING: We are running in the browser process. |raw_image_data| is the |
| 87 // image's raw, unsanitized bytes from the web. |raw_image_data| may contain | 99 // image's raw, unsanitized bytes from the web. |raw_image_data| may contain |
| 88 // malicious data. Decoding unsanitized bitmap data to an SkBitmap in the | 100 // malicious data. Decoding unsanitized bitmap data to an SkBitmap in the |
| 89 // browser process is a security bug. | 101 // browser process is a security bug. |
| 90 std::string raw_image_data; | 102 std::string raw_image_data; |
| 91 source->GetResponseAsString(&raw_image_data); | 103 source->GetResponseAsString(&raw_image_data); |
| 92 callback_.Run(ComputeMurmur2Hash(raw_image_data)); | 104 callback_.Run(ComputeMurmur2Hash(raw_image_data)); |
| 105 delete this; |
| 93 } | 106 } |
| 94 | 107 |
| 95 void WebApkIconHasher::OnDownloadTimedOut() { | 108 void WebApkIconHasher::OnDownloadTimedOut() { |
| 96 url_fetcher_.reset(); | 109 url_fetcher_.reset(); |
| 97 | 110 |
| 98 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 111 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 99 base::Bind(callback_, "")); | 112 base::Bind(callback_, "")); |
| 113 delete this; |
| 100 } | 114 } |
| OLD | NEW |