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 #ifndef CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_ICON_HASHER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_ICON_HASHER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_ICON_HASHER_H_ | 6 #define CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_ICON_HASHER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 14 #include "net/url_request/url_fetcher_delegate.h" | 14 #include "net/url_request/url_fetcher_delegate.h" |
| 15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 class URLFetcher; | 18 class URLFetcher; |
| 19 class URLRequestContextGetter; | 19 class URLRequestContextGetter; |
| 20 } | 20 } |
| 21 | 21 |
| 22 // Downloads an icon and takes a Murmur2 hash of the downloaded image. | 22 // Downloads an icon and takes a Murmur2 hash of the downloaded image. |
| 23 class WebApkIconHasher : public net::URLFetcherDelegate { | 23 class WebApkIconHasher : public net::URLFetcherDelegate { |
| 24 public: | 24 public: |
| 25 using Murmur2HashCallback = | 25 using Murmur2HashCallback = |
| 26 base::Callback<void(const std::string& /* icon_murmur2_hash */)>; | 26 base::Callback<void(const std::string& /* icon_murmur2_hash */)>; |
| 27 | 27 |
| 28 ~WebApkIconHasher() override; | |
|
dominickn
2017/03/29 23:27:00
Can this be declared private? Or is it called by t
F
2017/03/30 00:20:13
Done. This can be declared private.
| |
| 29 | |
| 30 // Creates a self-owned WebApkIconHasher instance. The instance downloads | |
| 31 // |icon_url| and calls |callback| with the Murmur2 hash of the downloaded | |
| 32 // image. The hash is taken over the raw image bytes (no image | |
| 33 // encoding/decoding beforehand). |callback| is called with an empty string if | |
| 34 // the image cannot not be downloaded in time (e.g. 404 HTTP error code). | |
| 35 static void DownloadAndComputeMurmur2Hash( | |
| 36 net::URLRequestContextGetter* request_context_getter, | |
| 37 const GURL& icon_url, | |
| 38 const Murmur2HashCallback& callback); | |
| 39 | |
| 40 private: | |
| 28 WebApkIconHasher( | 41 WebApkIconHasher( |
| 29 net::URLRequestContextGetter* request_context_getter, | 42 net::URLRequestContextGetter* request_context_getter, |
| 30 const GURL& icon_url, | 43 const GURL& icon_url, |
| 31 const Murmur2HashCallback& callback); | 44 const Murmur2HashCallback& callback); |
| 32 ~WebApkIconHasher() override; | |
| 33 | 45 |
| 34 // Downloads |icon_url_|. Calls |callback_| with the Murmur2 hash of the | 46 // Downloads |icon_url_|. Calls |callback_| with the Murmur2 hash of the |
| 35 // downloaded image. The hash is taken over the raw image bytes (no image | 47 // downloaded image. The hash is taken over the raw image bytes (no image |
| 36 // encoding/decoding beforehand). |callback_| is called with an empty string | 48 // encoding/decoding beforehand). |callback_| is called with an empty string |
| 37 // if the image cannot not be downloaded in time (e.g. 404 HTTP error code). | 49 // if the image cannot not be downloaded in time (e.g. 404 HTTP error code). |
| 38 void DownloadAndComputeMurmur2Hash(); | 50 void DownloadAndComputeMurmur2Hash(); |
|
dominickn
2017/03/29 23:27:00
You could remove this method and do all of its wor
F
2017/03/30 00:20:13
After experimenting with it, I think maybe we shou
dominickn
2017/03/30 00:26:46
That's a good point. You could get around it by mo
pkotwicz
2017/03/30 14:47:25
In my opinion, we should try to avoid having const
F
2017/03/30 15:52:46
For now I kept the current code structure and adde
| |
| 39 | 51 |
| 40 private: | |
| 41 // net::URLFetcherDelegate: | 52 // net::URLFetcherDelegate: |
| 42 void OnURLFetchComplete(const net::URLFetcher* source) override; | 53 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 43 | 54 |
| 44 // Called if downloading the icon takes too long. | 55 // Called if downloading the icon takes too long. |
| 45 void OnDownloadTimedOut(); | 56 void OnDownloadTimedOut(); |
| 46 | 57 |
| 47 // For retrieving URLRequestContext. Owned by the caller of this class. | 58 // For retrieving URLRequestContext. Owned by the caller of this class. |
| 48 net::URLRequestContextGetter* url_request_context_getter_; | 59 net::URLRequestContextGetter* url_request_context_getter_; |
| 49 | 60 |
| 50 // The icon URL. | 61 // The icon URL. |
| 51 GURL icon_url_; | 62 GURL icon_url_; |
| 52 | 63 |
| 53 // Called with the image hash. | 64 // Called with the image hash. |
| 54 Murmur2HashCallback callback_; | 65 Murmur2HashCallback callback_; |
| 55 | 66 |
| 56 std::unique_ptr<net::URLFetcher> url_fetcher_; | 67 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 57 | 68 |
| 58 // Fails WebApkIconHasher if the download takes too long. | 69 // Fails WebApkIconHasher if the download takes too long. |
| 59 base::OneShotTimer download_timeout_timer_; | 70 base::OneShotTimer download_timeout_timer_; |
| 60 | 71 |
| 61 DISALLOW_COPY_AND_ASSIGN(WebApkIconHasher); | 72 DISALLOW_COPY_AND_ASSIGN(WebApkIconHasher); |
| 62 }; | 73 }; |
| 63 | 74 |
| 64 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_ICON_HASHER_H_ | 75 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_ICON_HASHER_H_ |
| OLD | NEW |