1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/android/shortcut_data_fetcher.h"
7 #include "base/basictypes.h"
8 #include "base/location.h"
9 #include "base/strings/string16.h"
10 #include "base/task/cancelable_task_tracker.h"
11 #include "chrome/browser/favicon/favicon_service_factory.h"
12 #include "chrome/browser/manifest/manifest_icon_selector.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/render_messages.h"
16 #include "chrome/common/web_application_info.h"
17 #include "components/dom_distiller/core/url_utils.h"
18 #include "components/favicon/core/favicon_service.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/user_metrics.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_contents_observer.h"
23 #include "content/public/common/frame_navigate_params.h"
24 #include "content/public/common/manifest.h"
25 #include "third_party/WebKit/public/platform/WebScreenOrientationLockType.h"
26 #include "ui/gfx/codec/png_codec.h"
27 #include "ui/gfx/favicon_size.h"
28 #include "ui/gfx/screen.h"
31 using content::Manifest
;
33 // Android's preferred icon size in DP is 48, as defined in
34 // http://developer.android.com/design/style/iconography.html
35 const int ShortcutDataFetcher::kPreferredIconSizeInDp
= 48;
37 ShortcutDataFetcher::ShortcutDataFetcher(
38 content::WebContents
* web_contents
,
40 : WebContentsObserver(web_contents
),
41 weak_observer_(observer
),
42 is_waiting_for_web_application_info_(false),
43 is_icon_saved_(false),
45 icon_timeout_timer_(false, false),
46 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(
47 web_contents
->GetURL())),
48 preferred_icon_size_in_px_(kPreferredIconSizeInDp
*
49 gfx::Screen::GetScreenFor(web_contents
->GetNativeView())->
50 GetPrimaryDisplay().device_scale_factor()) {
51 // Send a message to the renderer to retrieve information about the page.
52 is_waiting_for_web_application_info_
= true;
53 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id()));
56 void ShortcutDataFetcher::OnDidGetWebApplicationInfo(
57 const WebApplicationInfo
& received_web_app_info
) {
58 is_waiting_for_web_application_info_
= false;
59 if (!web_contents() || !weak_observer_
) return;
61 // Sanitize received_web_app_info.
62 WebApplicationInfo web_app_info
= received_web_app_info
;
64 web_app_info
.title
.substr(0, chrome::kMaxMetaTagAttributeLength
);
65 web_app_info
.description
=
66 web_app_info
.description
.substr(0, chrome::kMaxMetaTagAttributeLength
);
68 shortcut_info_
.title
= web_app_info
.title
.empty() ? web_contents()->GetTitle()
71 if (web_app_info
.mobile_capable
== WebApplicationInfo::MOBILE_CAPABLE
||
72 web_app_info
.mobile_capable
== WebApplicationInfo::MOBILE_CAPABLE_APPLE
) {
73 shortcut_info_
.display
= content::Manifest::DISPLAY_MODE_STANDALONE
;
76 // Record what type of shortcut was added by the user.
77 switch (web_app_info
.mobile_capable
) {
78 case WebApplicationInfo::MOBILE_CAPABLE
:
79 content::RecordAction(
80 base::UserMetricsAction("webapps.AddShortcut.AppShortcut"));
82 case WebApplicationInfo::MOBILE_CAPABLE_APPLE
:
83 content::RecordAction(
84 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple"));
86 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED
:
87 content::RecordAction(
88 base::UserMetricsAction("webapps.AddShortcut.Bookmark"));
92 web_contents()->GetManifest(base::Bind(&ShortcutDataFetcher::OnDidGetManifest
,
96 void ShortcutDataFetcher::OnDidGetManifest(const content::Manifest
& manifest
) {
97 if (!web_contents() || !weak_observer_
) return;
99 if (!manifest
.IsEmpty()) {
100 content::RecordAction(
101 base::UserMetricsAction("webapps.AddShortcut.Manifest"));
104 shortcut_info_
.UpdateFromManifest(manifest
);
106 GURL icon_src
= ManifestIconSelector::FindBestMatchingIcon(
108 kPreferredIconSizeInDp
,
109 gfx::Screen::GetScreenFor(web_contents()->GetNativeView()));
110 if (icon_src
.is_valid()) {
111 // Grab the best icon from the manifest.
112 web_contents()->DownloadImage(
115 preferred_icon_size_in_px_
,
117 base::Bind(&ShortcutDataFetcher::OnManifestIconFetched
,
120 // Grab the best favicon for the page.
124 weak_observer_
->OnTitleAvailable(shortcut_info_
.title
);
126 // Kick off a timeout for downloading the icon. If an icon isn't set within
127 // the timeout, fall back to using a dynamically-generated launcher icon.
128 icon_timeout_timer_
.Start(FROM_HERE
,
129 base::TimeDelta::FromMilliseconds(3000),
130 base::Bind(&ShortcutDataFetcher::OnFaviconFetched
,
132 favicon_base::FaviconRawBitmapResult()));
135 bool ShortcutDataFetcher::OnMessageReceived(const IPC::Message
& message
) {
136 if (!is_waiting_for_web_application_info_
) return false;
140 IPC_BEGIN_MESSAGE_MAP(ShortcutDataFetcher
, message
)
141 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo
,
142 OnDidGetWebApplicationInfo
)
143 IPC_MESSAGE_UNHANDLED(handled
= false)
144 IPC_END_MESSAGE_MAP()
149 ShortcutDataFetcher::~ShortcutDataFetcher() {
150 DCHECK(!weak_observer_
);
153 void ShortcutDataFetcher::FetchFavicon() {
154 if (!web_contents() || !weak_observer_
) return;
157 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
159 // Grab the best, largest icon we can find to represent this bookmark.
160 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its
161 // rewrite is further along.
162 std::vector
<int> icon_types
;
163 icon_types
.push_back(favicon_base::FAVICON
);
164 icon_types
.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON
|
165 favicon_base::TOUCH_ICON
);
166 favicon::FaviconService
* favicon_service
=
167 FaviconServiceFactory::GetForProfile(profile
,
168 ServiceAccessType::EXPLICIT_ACCESS
);
170 // Using favicon if its size is not smaller than platform required size,
171 // otherwise using the largest icon among all avaliable icons.
172 int threshold_to_get_any_largest_icon
= preferred_icon_size_in_px_
- 1;
173 favicon_service
->GetLargestRawFaviconForPageURL(
176 threshold_to_get_any_largest_icon
,
177 base::Bind(&ShortcutDataFetcher::OnFaviconFetched
, this),
178 &favicon_task_tracker_
);
181 void ShortcutDataFetcher::OnFaviconFetched(
182 const favicon_base::FaviconRawBitmapResult
& bitmap_result
) {
183 if (!web_contents() || !weak_observer_
|| is_icon_saved_
) {
187 content::BrowserThread::PostTask(
188 content::BrowserThread::IO
,
190 base::Bind(&ShortcutDataFetcher::CreateLauncherIcon
,
195 void ShortcutDataFetcher::CreateLauncherIcon(
196 const favicon_base::FaviconRawBitmapResult
& bitmap_result
) {
197 if (!web_contents() || !weak_observer_
) return;
199 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
200 SkBitmap icon_bitmap
;
201 if (bitmap_result
.is_valid()) {
202 gfx::PNGCodec::Decode(bitmap_result
.bitmap_data
->front(),
203 bitmap_result
.bitmap_data
->size(),
207 if (weak_observer_
) {
208 icon_bitmap
= weak_observer_
->FinalizeLauncherIcon(icon_bitmap
,
212 content::BrowserThread::PostTask(
213 content::BrowserThread::UI
,
215 base::Bind(&ShortcutDataFetcher::NotifyObserver
, this, icon_bitmap
));
218 void ShortcutDataFetcher::OnManifestIconFetched(
220 int http_status_code
,
222 const std::vector
<SkBitmap
>& bitmaps
,
223 const std::vector
<gfx::Size
>& sizes
) {
224 if (!web_contents() || !weak_observer_
) return;
226 // If getting the candidate manifest icon failed, the ShortcutHelper should
227 // fallback to the favicon.
228 // Otherwise, it sets the state as if there was no manifest icon pending.
229 if (bitmaps
.empty()) {
234 // There might be multiple bitmaps returned. The one to pick is bigger or
235 // equal to the preferred size. |bitmaps| is ordered from bigger to smaller.
236 int preferred_bitmap_index
= 0;
237 for (size_t i
= 0; i
< bitmaps
.size(); ++i
) {
238 if (bitmaps
[i
].height() < preferred_icon_size_in_px_
)
240 preferred_bitmap_index
= i
;
243 NotifyObserver(bitmaps
[preferred_bitmap_index
]);
246 void ShortcutDataFetcher::NotifyObserver(const SkBitmap
& bitmap
) {
247 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
248 if (!web_contents() || !weak_observer_
|| is_icon_saved_
)
251 is_icon_saved_
= true;
252 shortcut_icon_
= bitmap
;
254 weak_observer_
->OnDataAvailable(shortcut_info_
, shortcut_icon_
);