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_icon_size_in_dp
,
38 int minimum_icon_size_in_dp
,
39 int ideal_splash_image_size_in_dp
,
40 int minimum_splash_image_size_in_dp
,
42 : WebContentsObserver(web_contents
),
43 weak_observer_(observer
),
44 is_waiting_for_web_application_info_(false),
45 is_icon_saved_(false),
47 icon_timeout_timer_(false, false),
48 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(
49 web_contents
->GetURL())),
50 ideal_icon_size_in_dp_(ideal_icon_size_in_dp
),
51 minimum_icon_size_in_dp_(minimum_icon_size_in_dp
),
52 ideal_splash_image_size_in_dp_(ideal_splash_image_size_in_dp
),
53 minimum_splash_image_size_in_dp_(minimum_splash_image_size_in_dp
) {
54 DCHECK(minimum_icon_size_in_dp
<= ideal_icon_size_in_dp
);
55 DCHECK(minimum_splash_image_size_in_dp
<= ideal_splash_image_size_in_dp
);
57 // Send a message to the renderer to retrieve information about the page.
58 is_waiting_for_web_application_info_
= true;
59 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id()));
62 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo(
63 const WebApplicationInfo
& received_web_app_info
) {
64 is_waiting_for_web_application_info_
= false;
65 if (!web_contents() || !weak_observer_
) return;
67 // Sanitize received_web_app_info.
68 WebApplicationInfo web_app_info
= received_web_app_info
;
70 web_app_info
.title
.substr(0, chrome::kMaxMetaTagAttributeLength
);
71 web_app_info
.description
=
72 web_app_info
.description
.substr(0, chrome::kMaxMetaTagAttributeLength
);
74 // Simply set the user-editable title to be the page's title
75 shortcut_info_
.user_title
= web_app_info
.title
.empty()
76 ? web_contents()->GetTitle()
78 shortcut_info_
.short_name
= shortcut_info_
.user_title
;
79 shortcut_info_
.name
= shortcut_info_
.user_title
;
81 if (web_app_info
.mobile_capable
== WebApplicationInfo::MOBILE_CAPABLE
||
82 web_app_info
.mobile_capable
== WebApplicationInfo::MOBILE_CAPABLE_APPLE
) {
83 shortcut_info_
.display
= blink::WebDisplayModeStandalone
;
86 // Record what type of shortcut was added by the user.
87 switch (web_app_info
.mobile_capable
) {
88 case WebApplicationInfo::MOBILE_CAPABLE
:
89 content::RecordAction(
90 base::UserMetricsAction("webapps.AddShortcut.AppShortcut"));
92 case WebApplicationInfo::MOBILE_CAPABLE_APPLE
:
93 content::RecordAction(
94 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple"));
96 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED
:
97 content::RecordAction(
98 base::UserMetricsAction("webapps.AddShortcut.Bookmark"));
102 web_contents()->GetManifest(
103 base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest
, this));
106 void AddToHomescreenDataFetcher::OnDidGetManifest(
107 const content::Manifest
& manifest
) {
108 if (!web_contents() || !weak_observer_
) return;
110 if (!manifest
.IsEmpty()) {
111 content::RecordAction(
112 base::UserMetricsAction("webapps.AddShortcut.Manifest"));
113 shortcut_info_
.UpdateFromManifest(manifest
);
116 GURL icon_src
= ManifestIconSelector::FindBestMatchingIcon(
118 ideal_icon_size_in_dp_
,
119 minimum_icon_size_in_dp_
,
120 gfx::Screen::GetScreenFor(web_contents()->GetNativeView()));
122 // If fetching the Manifest icon fails, fallback to the best favicon
124 if (!ManifestIconDownloader::Download(
127 ideal_icon_size_in_dp_
,
128 minimum_icon_size_in_dp_
,
129 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched
,
134 // Save the splash screen URL for the later download.
135 splash_screen_url_
= ManifestIconSelector::FindBestMatchingIcon(
137 ideal_splash_image_size_in_dp_
,
138 minimum_splash_image_size_in_dp_
,
139 gfx::Screen::GetScreenFor(web_contents()->GetNativeView()));
141 weak_observer_
->OnUserTitleAvailable(shortcut_info_
.user_title
);
143 // Kick off a timeout for downloading the icon. If an icon isn't set within
144 // the timeout, fall back to using a dynamically-generated launcher icon.
145 icon_timeout_timer_
.Start(FROM_HERE
,
146 base::TimeDelta::FromMilliseconds(3000),
148 &AddToHomescreenDataFetcher::OnFaviconFetched
,
150 favicon_base::FaviconRawBitmapResult()));
153 bool AddToHomescreenDataFetcher::OnMessageReceived(
154 const IPC::Message
& message
) {
155 if (!is_waiting_for_web_application_info_
) return false;
159 IPC_BEGIN_MESSAGE_MAP(AddToHomescreenDataFetcher
, message
)
160 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo
,
161 OnDidGetWebApplicationInfo
)
162 IPC_MESSAGE_UNHANDLED(handled
= false)
163 IPC_END_MESSAGE_MAP()
168 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() {
169 DCHECK(!weak_observer_
);
172 void AddToHomescreenDataFetcher::FetchSplashScreenImage(
173 const std::string
& webapp_id
) {
174 ShortcutHelper::FetchSplashScreenImage(
177 ideal_splash_image_size_in_dp_
,
178 minimum_splash_image_size_in_dp_
,
182 void AddToHomescreenDataFetcher::FetchFavicon() {
183 if (!web_contents() || !weak_observer_
) return;
186 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
188 // Grab the best, largest icon we can find to represent this bookmark.
189 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its
190 // rewrite is further along.
191 std::vector
<int> icon_types
;
192 icon_types
.push_back(favicon_base::FAVICON
);
193 icon_types
.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON
|
194 favicon_base::TOUCH_ICON
);
195 favicon::FaviconService
* favicon_service
=
196 FaviconServiceFactory::GetForProfile(profile
,
197 ServiceAccessType::EXPLICIT_ACCESS
);
199 // Using favicon if its size is not smaller than platform required size,
200 // otherwise using the largest icon among all avaliable icons.
201 int ideal_icon_size_in_px
= ideal_icon_size_in_dp_
*
202 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())->
203 GetPrimaryDisplay().device_scale_factor();
204 int threshold_to_get_any_largest_icon
= ideal_icon_size_in_px
- 1;
205 favicon_service
->GetLargestRawFaviconForPageURL(
208 threshold_to_get_any_largest_icon
,
209 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched
, this),
210 &favicon_task_tracker_
);
213 void AddToHomescreenDataFetcher::OnFaviconFetched(
214 const favicon_base::FaviconRawBitmapResult
& bitmap_result
) {
215 if (!web_contents() || !weak_observer_
|| is_icon_saved_
)
218 content::BrowserThread::PostTask(
219 content::BrowserThread::IO
,
221 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIcon
,
226 void AddToHomescreenDataFetcher::CreateLauncherIcon(
227 const favicon_base::FaviconRawBitmapResult
& bitmap_result
) {
228 if (!web_contents() || !weak_observer_
) return;
230 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
231 SkBitmap icon_bitmap
;
232 if (bitmap_result
.is_valid()) {
233 gfx::PNGCodec::Decode(bitmap_result
.bitmap_data
->front(),
234 bitmap_result
.bitmap_data
->size(),
238 if (weak_observer_
) {
239 icon_bitmap
= weak_observer_
->FinalizeLauncherIcon(icon_bitmap
,
243 content::BrowserThread::PostTask(
244 content::BrowserThread::UI
,
246 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver
,
251 void AddToHomescreenDataFetcher::OnManifestIconFetched(const SkBitmap
& icon
) {
252 if (icon
.drawsNothing()) {
256 NotifyObserver(icon
);
259 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap
& bitmap
) {
260 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
261 if (!web_contents() || !weak_observer_
|| is_icon_saved_
)
264 is_icon_saved_
= true;
265 shortcut_icon_
= bitmap
;
267 weak_observer_
->OnDataAvailable(shortcut_info_
, shortcut_icon_
);