Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / android / webapps / add_to_homescreen_data_fetcher.cc
blob46b0edff59d2879bac447d8f482b7a5d1a38e64a
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"
31 #include "url/gurl.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,
39 Observer* observer)
40 : WebContentsObserver(web_contents),
41 weak_observer_(observer),
42 is_waiting_for_web_application_info_(false),
43 is_icon_saved_(false),
44 is_ready_(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;
62 web_app_info.title =
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()
70 : web_app_info.title;
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"));
84 break;
85 case WebApplicationInfo::MOBILE_CAPABLE_APPLE:
86 content::RecordAction(
87 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple"));
88 break;
89 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED:
90 content::RecordAction(
91 base::UserMetricsAction("webapps.AddShortcut.Bookmark"));
92 break;
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(
110 manifest.icons,
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
115 // for the page.
116 if (!ManifestIconDownloader::Download(
117 web_contents(),
118 icon_src,
119 ideal_icon_size_in_dp_,
120 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched,
121 this))) {
122 FetchFavicon();
125 // Save the splash screen URL for the later download.
126 splash_screen_url_ = ManifestIconSelector::FindBestMatchingIcon(
127 manifest.icons,
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),
137 base::Bind(
138 &AddToHomescreenDataFetcher::OnFaviconFetched,
139 this,
140 favicon_base::FaviconRawBitmapResult()));
143 bool AddToHomescreenDataFetcher::OnMessageReceived(
144 const IPC::Message& message) {
145 if (!is_waiting_for_web_application_info_) return false;
147 bool handled = true;
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()
155 return handled;
158 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() {
159 DCHECK(!weak_observer_);
162 void AddToHomescreenDataFetcher::FetchSplashScreenImage(
163 const std::string& webapp_id) {
164 ShortcutHelper::FetchSplashScreenImage(
165 web_contents(),
166 splash_screen_url_,
167 ideal_splash_image_size_in_dp_,
168 webapp_id);
171 void AddToHomescreenDataFetcher::FetchFavicon() {
172 if (!web_contents() || !weak_observer_) return;
174 Profile* profile =
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(
195 shortcut_info_.url,
196 icon_types,
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_)
205 return;
207 content::BrowserThread::PostTask(
208 content::BrowserThread::IO,
209 FROM_HERE,
210 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIcon,
211 this,
212 bitmap_result));
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(),
224 &icon_bitmap);
227 if (weak_observer_) {
228 icon_bitmap = weak_observer_->FinalizeLauncherIcon(icon_bitmap,
229 shortcut_info_.url);
232 content::BrowserThread::PostTask(
233 content::BrowserThread::UI,
234 FROM_HERE,
235 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver,
236 this,
237 icon_bitmap));
240 void AddToHomescreenDataFetcher::OnManifestIconFetched(const SkBitmap& icon) {
241 if (icon.drawsNothing()) {
242 FetchFavicon();
243 return;
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_)
251 return;
253 is_icon_saved_ = true;
254 shortcut_icon_ = bitmap;
255 is_ready_ = true;
256 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_);