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

Unified Diff: chrome/browser/android/webapk/webapk_icon_hasher.cc

Issue 2771793003: Move timeout timer into WebApkIconHasher (Closed)
Patch Set: Remove weakptr and use unretained Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/webapk/webapk_icon_hasher.cc
diff --git a/chrome/browser/android/webapk/webapk_icon_hasher.cc b/chrome/browser/android/webapk/webapk_icon_hasher.cc
index c685e08496ba5099e3fe8ac550af9eca5317ab21..2a1a4133f1ef3ee1ce3ccbb7bea2b35a9fb421be 100644
--- a/chrome/browser/android/webapk/webapk_icon_hasher.cc
+++ b/chrome/browser/android/webapk/webapk_icon_hasher.cc
@@ -12,13 +12,15 @@
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_context_getter.h"
#include "third_party/smhasher/src/MurmurHash2.h"
-#include "url/gurl.h"
namespace {
// The seed to use when taking the murmur2 hash of the icon.
const uint64_t kMurmur2HashSeed = 0;
+// The default number of milliseconds to wait for the icon download to complete.
+const int kDownloadTimeoutInMilliseconds = 60000;
+
// Computes Murmur2 hash of |raw_image_data|.
std::string ComputeMurmur2Hash(const std::string& raw_image_data) {
// WARNING: We are running in the browser process. |raw_image_data| is the
@@ -32,33 +34,49 @@ std::string ComputeMurmur2Hash(const std::string& raw_image_data) {
} // anonymous namespace
-WebApkIconHasher::WebApkIconHasher() {}
+WebApkIconHasher::WebApkIconHasher(
+ net::URLRequestContextGetter* url_request_context_getter,
+ const GURL& icon_url,
+ const Murmur2HashCallback& callback)
+ : url_request_context_getter_(url_request_context_getter),
+ icon_url_(icon_url),
+ callback_(callback) {}
WebApkIconHasher::~WebApkIconHasher() {}
-void WebApkIconHasher::DownloadAndComputeMurmur2Hash(
- net::URLRequestContextGetter* request_context_getter,
- const GURL& icon_url,
- const Murmur2HashCallback& callback) {
- if (icon_url.SchemeIs(url::kDataScheme)) {
+void WebApkIconHasher::DownloadAndComputeMurmur2Hash() {
+ if (!icon_url_.is_valid()) {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
+ base::Bind(callback_, ""));
+ return;
+ }
+
+ if (icon_url_.SchemeIs(url::kDataScheme)) {
std::string mime_type, char_set, data;
std::string hash;
- if (net::DataURL::Parse(icon_url, &mime_type, &char_set, &data) &&
+ if (net::DataURL::Parse(icon_url_, &mime_type, &char_set, &data) &&
!data.empty()) {
hash = ComputeMurmur2Hash(data);
}
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
- base::Bind(callback, hash));
+ base::Bind(callback_, hash));
return;
}
- callback_ = callback;
- url_fetcher_ = net::URLFetcher::Create(icon_url, net::URLFetcher::GET, this);
- url_fetcher_->SetRequestContext(request_context_getter);
+ download_timeout_timer_.Start(
+ FROM_HERE,
+ base::TimeDelta::FromMilliseconds(kDownloadTimeoutInMilliseconds),
+ base::Bind(&WebApkIconHasher::OnDownloadTimedOut,
+ base::Unretained(this)));
+
+ url_fetcher_ = net::URLFetcher::Create(icon_url_, net::URLFetcher::GET, this);
+ url_fetcher_->SetRequestContext(url_request_context_getter_);
url_fetcher_->Start();
}
void WebApkIconHasher::OnURLFetchComplete(const net::URLFetcher* source) {
+ download_timeout_timer_.Stop();
+
if (!source->GetStatus().is_success() ||
source->GetResponseCode() != net::HTTP_OK) {
callback_.Run("");
@@ -73,3 +91,10 @@ void WebApkIconHasher::OnURLFetchComplete(const net::URLFetcher* source) {
source->GetResponseAsString(&raw_image_data);
callback_.Run(ComputeMurmur2Hash(raw_image_data));
}
+
+void WebApkIconHasher::OnDownloadTimedOut() {
+ url_fetcher_.reset();
+
+ base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
+ base::Bind(callback_, ""));
+}
« no previous file with comments | « chrome/browser/android/webapk/webapk_icon_hasher.h ('k') | chrome/browser/android/webapk/webapk_icon_hasher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698