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/webapps/add_to_homescreen_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/android/shortcut_helper.h"
12 #include "chrome/browser/favicon/favicon_service_factory.h"
13 #include "chrome/browser/manifest/manifest_icon_downloader.h"
14 #include "chrome/browser/manifest/manifest_icon_selector.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/chrome_constants.h"
17 #include "chrome/common/render_messages.h"
18 #include "chrome/common/web_application_info.h"
19 #include "components/dom_distiller/core/url_utils.h"
20 #include "components/favicon/core/favicon_service.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/user_metrics.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/browser/web_contents_observer.h"
25 #include "content/public/common/frame_navigate_params.h"
26 #include "content/public/common/manifest.h"
27 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScreenOrientationLockType.h"
28 #include "ui/gfx/codec/png_codec.h"
29 #include "ui/gfx/favicon_size.h"
30 #include "ui/gfx/screen.h"
33 using content::Manifest
;
35 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher(
36 content::WebContents
* web_contents
,
37 int ideal_splash_image_size_in_dp
,
38 int ideal_icon_size_in_dp
,
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 ideal_splash_image_size_in_dp_(ideal_splash_image_size_in_dp
),
49 ideal_icon_size_in_dp_(ideal_icon_size_in_dp
) {
50 // Send a message to the renderer to retrieve information about the page.
51 is_waiting_for_web_application_info_
= true;
52 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id()));
55 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo(
56 const WebApplicationInfo
& received_web_app_info
) {
57 is_waiting_for_web_application_info_
= false;
58 if (!web_contents() || !weak_observer_
) return;
60 // Sanitize received_web_app_info.
61 WebApplicationInfo web_app_info
= received_web_app_info
;
63 web_app_info
.title
.substr(0, chrome::kMaxMetaTagAttributeLength
);
64 web_app_info
.description
=
65 web_app_info
.description
.substr(0, chrome::kMaxMetaTagAttributeLength
);
67 // Simply set the user-editable title to be the page's title
68 shortcut_info_
.user_title
= web_app_info
.title
.empty()
69 ? web_contents()->GetTitle()
71 shortcut_info_
.short_name
= shortcut_info_
.user_title
;
72 shortcut_info_
.name
= shortcut_info_
.user_title
;
74 if (web_app_info
.mobile_capable
== WebApplicationInfo::MOBILE_CAPABLE
||
75 web_app_info
.mobile_capable
== WebApplicationInfo::MOBILE_CAPABLE_APPLE
) {
76 shortcut_info_
.display
= blink::WebDisplayModeStandalone
;
79 // Record what type of shortcut was added by the user.
80 switch (web_app_info
.mobile_capable
) {
81 case WebApplicationInfo::MOBILE_CAPABLE
:
82 content::RecordAction(
83 base::UserMetricsAction("webapps.AddShortcut.AppShortcut"));
85 case WebApplicationInfo::MOBILE_CAPABLE_APPLE
:
86 content::RecordAction(
87 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple"));
89 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED
:
90 content::RecordAction(
91 base::UserMetricsAction("webapps.AddShortcut.Bookmark"));
95 web_contents()->GetManifest(
96 base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest
, this));
99 void AddToHomescreenDataFetcher::OnDidGetManifest(
100 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 ideal_icon_size_in_dp_
,
112 gfx::Screen::GetScreenFor(web_contents()->GetNativeView()));
114 // If fetching the Manifest icon fails, fallback to the best favicon
116 if (!ManifestIconDownloader::Download(
119 ideal_icon_size_in_dp_
,
120 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched
,
125 // Save the splash screen URL for the later download.
126 splash_screen_url_
= ManifestIconSelector::FindBestMatchingIcon(
128 ideal_splash_image_size_in_dp_
,
129 gfx::Screen::GetScreenFor(web_contents()->GetNativeView()));
131 weak_observer_
->OnUserTitleAvailable(shortcut_info_
.user_title
);
133 // Kick off a timeout for downloading the icon. If an icon isn't set within
134 // the timeout, fall back to using a dynamically-generated launcher icon.
135 icon_timeout_timer_
.Start(FROM_HERE
,
136 base::TimeDelta::FromMilliseconds(3000),
138 &AddToHomescreenDataFetcher::OnFaviconFetched
,
140 favicon_base::FaviconRawBitmapResult()));
143 bool AddToHomescreenDataFetcher::OnMessageReceived(
144 const IPC::Message
& message
) {
145 if (!is_waiting_for_web_application_info_
) return false;
149 IPC_BEGIN_MESSAGE_MAP(AddToHomescreenDataFetcher
, message
)
150 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo
,
151 OnDidGetWebApplicationInfo
)
152 IPC_MESSAGE_UNHANDLED(handled
= false)
153 IPC_END_MESSAGE_MAP()
158 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() {
159 DCHECK(!weak_observer_
);
162 void AddToHomescreenDataFetcher::FetchSplashScreenImage(
163 const std::string
& webapp_id
) {
164 ShortcutHelper::FetchSplashScreenImage(
167 ideal_splash_image_size_in_dp_
,
171 void AddToHomescreenDataFetcher::FetchFavicon() {
172 if (!web_contents() || !weak_observer_
) return;
175 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
177 // Grab the best, largest icon we can find to represent this bookmark.
178 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its
179 // rewrite is further along.
180 std::vector
<int> icon_types
;
181 icon_types
.push_back(favicon_base::FAVICON
);
182 icon_types
.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON
|
183 favicon_base::TOUCH_ICON
);
184 favicon::FaviconService
* favicon_service
=
185 FaviconServiceFactory::GetForProfile(profile
,
186 ServiceAccessType::EXPLICIT_ACCESS
);
188 // Using favicon if its size is not smaller than platform required size,
189 // otherwise using the largest icon among all avaliable icons.
190 int ideal_icon_size_in_px
= ideal_icon_size_in_dp_
*
191 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())->
192 GetPrimaryDisplay().device_scale_factor();
193 int threshold_to_get_any_largest_icon
= ideal_icon_size_in_px
- 1;
194 favicon_service
->GetLargestRawFaviconForPageURL(
197 threshold_to_get_any_largest_icon
,
198 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched
, this),
199 &favicon_task_tracker_
);
202 void AddToHomescreenDataFetcher::OnFaviconFetched(
203 const favicon_base::FaviconRawBitmapResult
& bitmap_result
) {
204 if (!web_contents() || !weak_observer_
|| is_icon_saved_
)
207 content::BrowserThread::PostTask(
208 content::BrowserThread::IO
,
210 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIcon
,
215 void AddToHomescreenDataFetcher::CreateLauncherIcon(
216 const favicon_base::FaviconRawBitmapResult
& bitmap_result
) {
217 if (!web_contents() || !weak_observer_
) return;
219 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
220 SkBitmap icon_bitmap
;
221 if (bitmap_result
.is_valid()) {
222 gfx::PNGCodec::Decode(bitmap_result
.bitmap_data
->front(),
223 bitmap_result
.bitmap_data
->size(),
227 if (weak_observer_
) {
228 icon_bitmap
= weak_observer_
->FinalizeLauncherIcon(icon_bitmap
,
232 content::BrowserThread::PostTask(
233 content::BrowserThread::UI
,
235 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver
,
240 void AddToHomescreenDataFetcher::OnManifestIconFetched(const SkBitmap
& icon
) {
241 if (icon
.drawsNothing()) {
245 NotifyObserver(icon
);
248 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap
& bitmap
) {
249 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
250 if (!web_contents() || !weak_observer_
|| is_icon_saved_
)
253 is_icon_saved_
= true;
254 shortcut_icon_
= bitmap
;
256 weak_observer_
->OnDataAvailable(shortcut_info_
, shortcut_icon_
);