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/modules/screen_orientation/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 // Simply set the user-editable title to be the page's title
69 shortcut_info_
.user_title
= web_app_info
.title
.empty()
70 ? web_contents()->GetTitle()
72 shortcut_info_
.short_name
= shortcut_info_
.user_title
;
73 shortcut_info_
.name
= shortcut_info_
.user_title
;
75 if (web_app_info
.mobile_capable
== WebApplicationInfo::MOBILE_CAPABLE
||
76 web_app_info
.mobile_capable
== WebApplicationInfo::MOBILE_CAPABLE_APPLE
) {
77 shortcut_info_
.display
= blink::WebDisplayModeStandalone
;
80 // Record what type of shortcut was added by the user.
81 switch (web_app_info
.mobile_capable
) {
82 case WebApplicationInfo::MOBILE_CAPABLE
:
83 content::RecordAction(
84 base::UserMetricsAction("webapps.AddShortcut.AppShortcut"));
86 case WebApplicationInfo::MOBILE_CAPABLE_APPLE
:
87 content::RecordAction(
88 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple"));
90 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED
:
91 content::RecordAction(
92 base::UserMetricsAction("webapps.AddShortcut.Bookmark"));
96 web_contents()->GetManifest(base::Bind(&ShortcutDataFetcher::OnDidGetManifest
,
100 void ShortcutDataFetcher::OnDidGetManifest(const content::Manifest
& manifest
) {
101 if (!web_contents() || !weak_observer_
) return;
103 if (!manifest
.IsEmpty()) {
104 content::RecordAction(
105 base::UserMetricsAction("webapps.AddShortcut.Manifest"));
106 shortcut_info_
.UpdateFromManifest(manifest
);
109 GURL icon_src
= ManifestIconSelector::FindBestMatchingIcon(
111 kPreferredIconSizeInDp
,
112 gfx::Screen::GetScreenFor(web_contents()->GetNativeView()));
113 if (icon_src
.is_valid()) {
114 // Grab the best icon from the manifest.
115 web_contents()->DownloadImage(
118 preferred_icon_size_in_px_
,
120 base::Bind(&ShortcutDataFetcher::OnManifestIconFetched
,
123 // Grab the best favicon for the page.
127 weak_observer_
->OnUserTitleAvailable(shortcut_info_
.user_title
);
129 // Kick off a timeout for downloading the icon. If an icon isn't set within
130 // the timeout, fall back to using a dynamically-generated launcher icon.
131 icon_timeout_timer_
.Start(FROM_HERE
,
132 base::TimeDelta::FromMilliseconds(3000),
133 base::Bind(&ShortcutDataFetcher::OnFaviconFetched
,
135 favicon_base::FaviconRawBitmapResult()));
138 bool ShortcutDataFetcher::OnMessageReceived(const IPC::Message
& message
) {
139 if (!is_waiting_for_web_application_info_
) return false;
143 IPC_BEGIN_MESSAGE_MAP(ShortcutDataFetcher
, message
)
144 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo
,
145 OnDidGetWebApplicationInfo
)
146 IPC_MESSAGE_UNHANDLED(handled
= false)
147 IPC_END_MESSAGE_MAP()
152 ShortcutDataFetcher::~ShortcutDataFetcher() {
153 DCHECK(!weak_observer_
);
156 void ShortcutDataFetcher::FetchFavicon() {
157 if (!web_contents() || !weak_observer_
) return;
160 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
162 // Grab the best, largest icon we can find to represent this bookmark.
163 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its
164 // rewrite is further along.
165 std::vector
<int> icon_types
;
166 icon_types
.push_back(favicon_base::FAVICON
);
167 icon_types
.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON
|
168 favicon_base::TOUCH_ICON
);
169 favicon::FaviconService
* favicon_service
=
170 FaviconServiceFactory::GetForProfile(profile
,
171 ServiceAccessType::EXPLICIT_ACCESS
);
173 // Using favicon if its size is not smaller than platform required size,
174 // otherwise using the largest icon among all avaliable icons.
175 int threshold_to_get_any_largest_icon
= preferred_icon_size_in_px_
- 1;
176 favicon_service
->GetLargestRawFaviconForPageURL(
179 threshold_to_get_any_largest_icon
,
180 base::Bind(&ShortcutDataFetcher::OnFaviconFetched
, this),
181 &favicon_task_tracker_
);
184 void ShortcutDataFetcher::OnFaviconFetched(
185 const favicon_base::FaviconRawBitmapResult
& bitmap_result
) {
186 if (!web_contents() || !weak_observer_
|| is_icon_saved_
) {
190 content::BrowserThread::PostTask(
191 content::BrowserThread::IO
,
193 base::Bind(&ShortcutDataFetcher::CreateLauncherIcon
,
198 void ShortcutDataFetcher::CreateLauncherIcon(
199 const favicon_base::FaviconRawBitmapResult
& bitmap_result
) {
200 if (!web_contents() || !weak_observer_
) return;
202 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
203 SkBitmap icon_bitmap
;
204 if (bitmap_result
.is_valid()) {
205 gfx::PNGCodec::Decode(bitmap_result
.bitmap_data
->front(),
206 bitmap_result
.bitmap_data
->size(),
210 if (weak_observer_
) {
211 icon_bitmap
= weak_observer_
->FinalizeLauncherIcon(icon_bitmap
,
215 content::BrowserThread::PostTask(
216 content::BrowserThread::UI
,
218 base::Bind(&ShortcutDataFetcher::NotifyObserver
, this, icon_bitmap
));
221 void ShortcutDataFetcher::OnManifestIconFetched(
223 int http_status_code
,
225 const std::vector
<SkBitmap
>& bitmaps
,
226 const std::vector
<gfx::Size
>& sizes
) {
227 if (!web_contents() || !weak_observer_
) return;
229 // If getting the candidate manifest icon failed, the ShortcutHelper should
230 // fallback to the favicon.
231 // Otherwise, it sets the state as if there was no manifest icon pending.
232 if (bitmaps
.empty()) {
237 // There might be multiple bitmaps returned. The one to pick is bigger or
238 // equal to the preferred size. |bitmaps| is ordered from bigger to smaller.
239 int preferred_bitmap_index
= 0;
240 for (size_t i
= 0; i
< bitmaps
.size(); ++i
) {
241 if (bitmaps
[i
].height() < preferred_icon_size_in_px_
)
243 preferred_bitmap_index
= i
;
246 NotifyObserver(bitmaps
[preferred_bitmap_index
]);
249 void ShortcutDataFetcher::NotifyObserver(const SkBitmap
& bitmap
) {
250 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
251 if (!web_contents() || !weak_observer_
|| is_icon_saved_
)
254 is_icon_saved_
= true;
255 shortcut_icon_
= bitmap
;
257 weak_observer_
->OnDataAvailable(shortcut_info_
, shortcut_icon_
);