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 | |
73 if (web_app_info.mobile_capable == WebApplicationInfo::MOBILE_CAPABLE || | |
74 web_app_info.mobile_capable == WebApplicationInfo::MOBILE_CAPABLE_APPLE) { | |
75 shortcut_info_.display = content::Manifest::DISPLAY_MODE_STANDALONE; | |
76 } | |
77 | |
78 // Record what type of shortcut was added by the user. | |
79 switch (web_app_info.mobile_capable) { | |
80 case WebApplicationInfo::MOBILE_CAPABLE: | |
81 content::RecordAction( | |
82 base::UserMetricsAction("webapps.AddShortcut.AppShortcut")); | |
83 break; | |
84 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: | |
85 content::RecordAction( | |
86 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); | |
87 break; | |
88 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: | |
89 content::RecordAction( | |
90 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); | |
91 break; | |
92 } | |
93 | |
94 web_contents()->GetManifest(base::Bind(&ShortcutDataFetcher::OnDidGetManifest, | |
95 this)); | |
96 } | |
97 | |
98 void ShortcutDataFetcher::OnDidGetManifest(const content::Manifest& manifest) { | |
99 if (!web_contents() || !weak_observer_) return; | |
100 | |
101 if (!manifest.IsEmpty()) { | |
102 content::RecordAction( | |
103 base::UserMetricsAction("webapps.AddShortcut.Manifest")); | |
104 } | |
105 | |
106 shortcut_info_.UpdateFromManifest(manifest); | |
107 | |
108 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | |
109 manifest.icons, | |
110 kPreferredIconSizeInDp, | |
111 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | |
112 if (icon_src.is_valid()) { | |
113 // Grab the best icon from the manifest. | |
114 web_contents()->DownloadImage( | |
115 icon_src, | |
116 false, | |
117 preferred_icon_size_in_px_, | |
118 false, | |
119 base::Bind(&ShortcutDataFetcher::OnManifestIconFetched, | |
120 this)); | |
121 } else { | |
122 // Grab the best favicon for the page. | |
123 FetchFavicon(); | |
124 } | |
125 | |
126 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); | |
127 | |
128 // Kick off a timeout for downloading the icon. If an icon isn't set within | |
129 // the timeout, fall back to using a dynamically-generated launcher icon. | |
130 icon_timeout_timer_.Start(FROM_HERE, | |
131 base::TimeDelta::FromMilliseconds(3000), | |
132 base::Bind(&ShortcutDataFetcher::OnFaviconFetched, | |
133 this, | |
134 favicon_base::FaviconRawBitmapResult())); | |
135 } | |
136 | |
137 bool ShortcutDataFetcher::OnMessageReceived(const IPC::Message& message) { | |
138 if (!is_waiting_for_web_application_info_) return false; | |
139 | |
140 bool handled = true; | |
141 | |
142 IPC_BEGIN_MESSAGE_MAP(ShortcutDataFetcher, message) | |
143 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, | |
144 OnDidGetWebApplicationInfo) | |
145 IPC_MESSAGE_UNHANDLED(handled = false) | |
146 IPC_END_MESSAGE_MAP() | |
147 | |
148 return handled; | |
149 } | |
150 | |
151 ShortcutDataFetcher::~ShortcutDataFetcher() { | |
152 DCHECK(!weak_observer_); | |
153 } | |
154 | |
155 void ShortcutDataFetcher::FetchFavicon() { | |
156 if (!web_contents() || !weak_observer_) return; | |
157 | |
158 Profile* profile = | |
159 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
160 | |
161 // Grab the best, largest icon we can find to represent this bookmark. | |
162 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its | |
163 // rewrite is further along. | |
164 std::vector<int> icon_types; | |
165 icon_types.push_back(favicon_base::FAVICON); | |
166 icon_types.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON | | |
167 favicon_base::TOUCH_ICON); | |
168 favicon::FaviconService* favicon_service = | |
169 FaviconServiceFactory::GetForProfile(profile, | |
170 ServiceAccessType::EXPLICIT_ACCESS); | |
171 | |
172 // Using favicon if its size is not smaller than platform required size, | |
173 // otherwise using the largest icon among all avaliable icons. | |
174 int threshold_to_get_any_largest_icon = preferred_icon_size_in_px_ - 1; | |
175 favicon_service->GetLargestRawFaviconForPageURL( | |
176 shortcut_info_.url, | |
177 icon_types, | |
178 threshold_to_get_any_largest_icon, | |
179 base::Bind(&ShortcutDataFetcher::OnFaviconFetched, this), | |
180 &favicon_task_tracker_); | |
181 } | |
182 | |
183 void ShortcutDataFetcher::OnFaviconFetched( | |
184 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | |
185 if (!web_contents() || !weak_observer_ || is_icon_saved_) { | |
186 return; | |
187 } | |
188 | |
189 content::BrowserThread::PostTask( | |
190 content::BrowserThread::IO, | |
191 FROM_HERE, | |
192 base::Bind(&ShortcutDataFetcher::CreateLauncherIcon, | |
193 this, | |
194 bitmap_result)); | |
195 } | |
196 | |
197 void ShortcutDataFetcher::CreateLauncherIcon( | |
198 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | |
199 if (!web_contents() || !weak_observer_) return; | |
200 | |
201 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
202 SkBitmap icon_bitmap; | |
203 if (bitmap_result.is_valid()) { | |
204 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), | |
205 bitmap_result.bitmap_data->size(), | |
206 &icon_bitmap); | |
207 } | |
208 | |
209 if (weak_observer_) { | |
210 icon_bitmap = weak_observer_->FinalizeLauncherIcon(icon_bitmap, | |
211 shortcut_info_.url); | |
212 } | |
213 | |
214 content::BrowserThread::PostTask( | |
215 content::BrowserThread::UI, | |
216 FROM_HERE, | |
217 base::Bind(&ShortcutDataFetcher::NotifyObserver, this, icon_bitmap)); | |
218 } | |
219 | |
220 void ShortcutDataFetcher::OnManifestIconFetched( | |
221 int id, | |
222 int http_status_code, | |
223 const GURL& url, | |
224 const std::vector<SkBitmap>& bitmaps, | |
225 const std::vector<gfx::Size>& sizes) { | |
226 if (!web_contents() || !weak_observer_) return; | |
227 | |
228 // If getting the candidate manifest icon failed, the ShortcutHelper should | |
229 // fallback to the favicon. | |
230 // Otherwise, it sets the state as if there was no manifest icon pending. | |
231 if (bitmaps.empty()) { | |
232 FetchFavicon(); | |
233 return; | |
234 } | |
235 | |
236 // There might be multiple bitmaps returned. The one to pick is bigger or | |
237 // equal to the preferred size. |bitmaps| is ordered from bigger to smaller. | |
238 int preferred_bitmap_index = 0; | |
239 for (size_t i = 0; i < bitmaps.size(); ++i) { | |
240 if (bitmaps[i].height() < preferred_icon_size_in_px_) | |
241 break; | |
242 preferred_bitmap_index = i; | |
243 } | |
244 | |
245 NotifyObserver(bitmaps[preferred_bitmap_index]); | |
246 } | |
247 | |
248 void ShortcutDataFetcher::NotifyObserver(const SkBitmap& bitmap) { | |
249 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
250 if (!web_contents() || !weak_observer_ || is_icon_saved_) | |
251 return; | |
252 | |
253 is_icon_saved_ = true; | |
254 shortcut_icon_ = bitmap; | |
255 is_ready_ = true; | |
256 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | |
257 } | |
OLD | NEW |