Commit 9988636c authored by dfalcantara's avatar dfalcantara Committed by Commit bot

Introduce timeout for downloading a favicon for adding to Home screen

If an icon can't be downloaded fast enough, generate a
default icon instead.

BUG=457424

Review URL: https://codereview.chromium.org/1124433005

Cr-Commit-Position: refs/heads/master@{#328970}
parent d81963c3
......@@ -39,7 +39,10 @@ ShortcutDataFetcher::ShortcutDataFetcher(
Observer* observer)
: WebContentsObserver(web_contents),
weak_observer_(observer),
is_waiting_for_web_application_info_(false),
is_icon_saved_(false),
is_ready_(false),
icon_timeout_timer_(false, false),
shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(
web_contents->GetURL())),
preferred_icon_size_in_px_(kPreferredIconSizeInDp *
......@@ -119,6 +122,14 @@ void ShortcutDataFetcher::OnDidGetManifest(const content::Manifest& manifest) {
}
weak_observer_->OnTitleAvailable(shortcut_info_.title);
// Kick off a timeout for downloading the icon. If an icon isn't set within
// the timeout, fall back to using a dynamically-generated launcher icon.
icon_timeout_timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(3000),
base::Bind(&ShortcutDataFetcher::OnFaviconFetched,
this,
favicon_base::FaviconRawBitmapResult()));
}
bool ShortcutDataFetcher::OnMessageReceived(const IPC::Message& message) {
......@@ -164,12 +175,14 @@ void ShortcutDataFetcher::FetchFavicon() {
icon_types,
threshold_to_get_any_largest_icon,
base::Bind(&ShortcutDataFetcher::OnFaviconFetched, this),
&cancelable_task_tracker_);
&favicon_task_tracker_);
}
void ShortcutDataFetcher::OnFaviconFetched(
const favicon_base::FaviconRawBitmapResult& bitmap_result) {
if (!web_contents() || !weak_observer_) return;
if (!web_contents() || !weak_observer_ || is_icon_saved_) {
return;
}
content::BrowserThread::PostTask(
content::BrowserThread::IO,
......@@ -192,14 +205,14 @@ void ShortcutDataFetcher::CreateLauncherIcon(
}
if (weak_observer_) {
shortcut_icon_ = weak_observer_->FinalizeLauncherIcon(icon_bitmap,
shortcut_info_.url);
icon_bitmap = weak_observer_->FinalizeLauncherIcon(icon_bitmap,
shortcut_info_.url);
}
content::BrowserThread::PostTask(
content::BrowserThread::UI,
FROM_HERE,
base::Bind(&ShortcutDataFetcher::NotifyObserver, this));
base::Bind(&ShortcutDataFetcher::NotifyObserver, this, icon_bitmap));
}
void ShortcutDataFetcher::OnManifestIconFetched(
......@@ -227,14 +240,16 @@ void ShortcutDataFetcher::OnManifestIconFetched(
preferred_bitmap_index = i;
}
shortcut_icon_ = bitmaps[preferred_bitmap_index];
NotifyObserver();
NotifyObserver(bitmaps[preferred_bitmap_index]);
}
void ShortcutDataFetcher::NotifyObserver() {
if (!web_contents() || !weak_observer_) return;
void ShortcutDataFetcher::NotifyObserver(const SkBitmap& bitmap) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!web_contents() || !weak_observer_ || is_icon_saved_)
return;
is_icon_saved_ = true;
shortcut_icon_ = bitmap;
is_ready_ = true;
weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_);
}
......@@ -7,6 +7,7 @@
#include "base/basictypes.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/timer/timer.h"
#include "chrome/browser/android/shortcut_info.h"
#include "chrome/common/web_application_info.h"
#include "components/favicon_base/favicon_types.h"
......@@ -89,15 +90,19 @@ class ShortcutDataFetcher
const std::vector<gfx::Size>& sizes);
// Notifies the observer that the shortcut data is all available.
void NotifyObserver();
void NotifyObserver(const SkBitmap& icon);
Observer* weak_observer_;
bool is_waiting_for_web_application_info_;
bool is_icon_saved_;
bool is_ready_;
base::Timer icon_timeout_timer_;
ShortcutInfo shortcut_info_;
// The icon must only be set on the UI thread for thread safety.
SkBitmap shortcut_icon_;
base::CancelableTaskTracker cancelable_task_tracker_;
base::CancelableTaskTracker favicon_task_tracker_;
const int preferred_icon_size_in_px_;
static const int kPreferredIconSizeInDp;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment