OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/android/shortcut_data_fetcher.h" | |
6 | |
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/WebScree
nOrientationLockType.h" | |
26 #include "ui/gfx/codec/png_codec.h" | |
27 #include "ui/gfx/favicon_size.h" | |
28 #include "ui/gfx/screen.h" | |
29 #include "url/gurl.h" | |
30 | |
31 using content::Manifest; | |
32 | |
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; | |
36 | |
37 ShortcutDataFetcher::ShortcutDataFetcher( | |
38 content::WebContents* web_contents, | |
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 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())); | |
54 } | |
55 | |
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; | |
60 | |
61 // Sanitize received_web_app_info. | |
62 WebApplicationInfo web_app_info = received_web_app_info; | |
63 web_app_info.title = | |
64 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength); | |
65 web_app_info.description = | |
66 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength); | |
67 | |
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() | |
71 : web_app_info.title; | |
72 shortcut_info_.short_name = shortcut_info_.user_title; | |
73 shortcut_info_.name = shortcut_info_.user_title; | |
74 | |
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; | |
78 } | |
79 | |
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")); | |
85 break; | |
86 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: | |
87 content::RecordAction( | |
88 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); | |
89 break; | |
90 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: | |
91 content::RecordAction( | |
92 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); | |
93 break; | |
94 } | |
95 | |
96 web_contents()->GetManifest(base::Bind(&ShortcutDataFetcher::OnDidGetManifest, | |
97 this)); | |
98 } | |
99 | |
100 void ShortcutDataFetcher::OnDidGetManifest(const content::Manifest& manifest) { | |
101 if (!web_contents() || !weak_observer_) return; | |
102 | |
103 if (!manifest.IsEmpty()) { | |
104 content::RecordAction( | |
105 base::UserMetricsAction("webapps.AddShortcut.Manifest")); | |
106 shortcut_info_.UpdateFromManifest(manifest); | |
107 } | |
108 | |
109 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | |
110 manifest.icons, | |
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( | |
116 icon_src, | |
117 false, | |
118 preferred_icon_size_in_px_, | |
119 false, | |
120 base::Bind(&ShortcutDataFetcher::OnManifestIconFetched, | |
121 this)); | |
122 } else { | |
123 // Grab the best favicon for the page. | |
124 FetchFavicon(); | |
125 } | |
126 | |
127 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); | |
128 | |
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, | |
134 this, | |
135 favicon_base::FaviconRawBitmapResult())); | |
136 } | |
137 | |
138 bool ShortcutDataFetcher::OnMessageReceived(const IPC::Message& message) { | |
139 if (!is_waiting_for_web_application_info_) return false; | |
140 | |
141 bool handled = true; | |
142 | |
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() | |
148 | |
149 return handled; | |
150 } | |
151 | |
152 ShortcutDataFetcher::~ShortcutDataFetcher() { | |
153 DCHECK(!weak_observer_); | |
154 } | |
155 | |
156 void ShortcutDataFetcher::FetchFavicon() { | |
157 if (!web_contents() || !weak_observer_) return; | |
158 | |
159 Profile* profile = | |
160 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
161 | |
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); | |
172 | |
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( | |
177 shortcut_info_.url, | |
178 icon_types, | |
179 threshold_to_get_any_largest_icon, | |
180 base::Bind(&ShortcutDataFetcher::OnFaviconFetched, this), | |
181 &favicon_task_tracker_); | |
182 } | |
183 | |
184 void ShortcutDataFetcher::OnFaviconFetched( | |
185 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | |
186 if (!web_contents() || !weak_observer_ || is_icon_saved_) { | |
187 return; | |
188 } | |
189 | |
190 content::BrowserThread::PostTask( | |
191 content::BrowserThread::IO, | |
192 FROM_HERE, | |
193 base::Bind(&ShortcutDataFetcher::CreateLauncherIcon, | |
194 this, | |
195 bitmap_result)); | |
196 } | |
197 | |
198 void ShortcutDataFetcher::CreateLauncherIcon( | |
199 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | |
200 if (!web_contents() || !weak_observer_) return; | |
201 | |
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(), | |
207 &icon_bitmap); | |
208 } | |
209 | |
210 if (weak_observer_) { | |
211 icon_bitmap = weak_observer_->FinalizeLauncherIcon(icon_bitmap, | |
212 shortcut_info_.url); | |
213 } | |
214 | |
215 content::BrowserThread::PostTask( | |
216 content::BrowserThread::UI, | |
217 FROM_HERE, | |
218 base::Bind(&ShortcutDataFetcher::NotifyObserver, this, icon_bitmap)); | |
219 } | |
220 | |
221 void ShortcutDataFetcher::OnManifestIconFetched( | |
222 int id, | |
223 int http_status_code, | |
224 const GURL& url, | |
225 const std::vector<SkBitmap>& bitmaps, | |
226 const std::vector<gfx::Size>& sizes) { | |
227 if (!web_contents() || !weak_observer_) return; | |
228 | |
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()) { | |
233 FetchFavicon(); | |
234 return; | |
235 } | |
236 | |
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_) | |
242 break; | |
243 preferred_bitmap_index = i; | |
244 } | |
245 | |
246 NotifyObserver(bitmaps[preferred_bitmap_index]); | |
247 } | |
248 | |
249 void ShortcutDataFetcher::NotifyObserver(const SkBitmap& bitmap) { | |
250 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
251 if (!web_contents() || !weak_observer_ || is_icon_saved_) | |
252 return; | |
253 | |
254 is_icon_saved_ = true; | |
255 shortcut_icon_ = bitmap; | |
256 is_ready_ = true; | |
257 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | |
258 } | |
OLD | NEW |