Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc

Issue 1288903002: Refactor ShortcutHelper and merge in BookmarkUtils (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move test case over too Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/shortcut_data_fetcher.h" 5 #include "chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/strings/string16.h" 9 #include "base/strings/string16.h"
10 #include "base/task/cancelable_task_tracker.h" 10 #include "base/task/cancelable_task_tracker.h"
11 #include "chrome/browser/favicon/favicon_service_factory.h" 11 #include "chrome/browser/favicon/favicon_service_factory.h"
12 #include "chrome/browser/manifest/manifest_icon_selector.h" 12 #include "chrome/browser/manifest/manifest_icon_selector.h"
13 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/chrome_constants.h" 14 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/render_messages.h" 15 #include "chrome/common/render_messages.h"
16 #include "chrome/common/web_application_info.h" 16 #include "chrome/common/web_application_info.h"
17 #include "components/dom_distiller/core/url_utils.h" 17 #include "components/dom_distiller/core/url_utils.h"
18 #include "components/favicon/core/favicon_service.h" 18 #include "components/favicon/core/favicon_service.h"
19 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/user_metrics.h" 20 #include "content/public/browser/user_metrics.h"
21 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_contents_observer.h" 22 #include "content/public/browser/web_contents_observer.h"
23 #include "content/public/common/frame_navigate_params.h" 23 #include "content/public/common/frame_navigate_params.h"
24 #include "content/public/common/manifest.h" 24 #include "content/public/common/manifest.h"
25 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree nOrientationLockType.h" 25 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree nOrientationLockType.h"
26 #include "ui/gfx/codec/png_codec.h" 26 #include "ui/gfx/codec/png_codec.h"
27 #include "ui/gfx/favicon_size.h" 27 #include "ui/gfx/favicon_size.h"
28 #include "ui/gfx/screen.h" 28 #include "ui/gfx/screen.h"
29 #include "url/gurl.h" 29 #include "url/gurl.h"
30 30
31 using content::Manifest; 31 using content::Manifest;
32 32
33 // Android's preferred icon size in DP is 48, as defined in 33 // Android's preferred icon size in DP is 48, as defined in
34 // http://developer.android.com/design/style/iconography.html 34 // http://developer.android.com/design/style/iconography.html
35 const int ShortcutDataFetcher::kPreferredIconSizeInDp = 48; 35 const int AddToHomescreenDataFetcher::kPreferredIconSizeInDp = 48;
36 36
37 ShortcutDataFetcher::ShortcutDataFetcher( 37 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher(
38 content::WebContents* web_contents, 38 content::WebContents* web_contents,
39 Observer* observer) 39 Observer* observer)
40 : WebContentsObserver(web_contents), 40 : WebContentsObserver(web_contents),
41 weak_observer_(observer), 41 weak_observer_(observer),
42 is_waiting_for_web_application_info_(false), 42 is_waiting_for_web_application_info_(false),
43 is_icon_saved_(false), 43 is_icon_saved_(false),
44 is_ready_(false), 44 is_ready_(false),
45 icon_timeout_timer_(false, false), 45 icon_timeout_timer_(false, false),
46 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( 46 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(
47 web_contents->GetURL())), 47 web_contents->GetURL())),
48 preferred_icon_size_in_px_(kPreferredIconSizeInDp * 48 preferred_icon_size_in_px_(kPreferredIconSizeInDp *
49 gfx::Screen::GetScreenFor(web_contents->GetNativeView())-> 49 gfx::Screen::GetScreenFor(web_contents->GetNativeView())->
50 GetPrimaryDisplay().device_scale_factor()) { 50 GetPrimaryDisplay().device_scale_factor()) {
51 // Send a message to the renderer to retrieve information about the page. 51 // Send a message to the renderer to retrieve information about the page.
52 is_waiting_for_web_application_info_ = true; 52 is_waiting_for_web_application_info_ = true;
53 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); 53 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id()));
54 } 54 }
55 55
56 void ShortcutDataFetcher::OnDidGetWebApplicationInfo( 56 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo(
57 const WebApplicationInfo& received_web_app_info) { 57 const WebApplicationInfo& received_web_app_info) {
58 is_waiting_for_web_application_info_ = false; 58 is_waiting_for_web_application_info_ = false;
59 if (!web_contents() || !weak_observer_) return; 59 if (!web_contents() || !weak_observer_) return;
60 60
61 // Sanitize received_web_app_info. 61 // Sanitize received_web_app_info.
62 WebApplicationInfo web_app_info = received_web_app_info; 62 WebApplicationInfo web_app_info = received_web_app_info;
63 web_app_info.title = 63 web_app_info.title =
64 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength); 64 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength);
65 web_app_info.description = 65 web_app_info.description =
66 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength); 66 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength);
(...skipping 17 matching lines...) Expand all
84 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: 84 case WebApplicationInfo::MOBILE_CAPABLE_APPLE:
85 content::RecordAction( 85 content::RecordAction(
86 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); 86 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple"));
87 break; 87 break;
88 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: 88 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED:
89 content::RecordAction( 89 content::RecordAction(
90 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); 90 base::UserMetricsAction("webapps.AddShortcut.Bookmark"));
91 break; 91 break;
92 } 92 }
93 93
94 web_contents()->GetManifest(base::Bind(&ShortcutDataFetcher::OnDidGetManifest, 94 web_contents()->GetManifest(
95 this)); 95 base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest, this));
96 } 96 }
97 97
98 void ShortcutDataFetcher::OnDidGetManifest(const content::Manifest& manifest) { 98 void AddToHomescreenDataFetcher::OnDidGetManifest(
99 const content::Manifest& manifest) {
99 if (!web_contents() || !weak_observer_) return; 100 if (!web_contents() || !weak_observer_) return;
100 101
101 if (!manifest.IsEmpty()) { 102 if (!manifest.IsEmpty()) {
102 content::RecordAction( 103 content::RecordAction(
103 base::UserMetricsAction("webapps.AddShortcut.Manifest")); 104 base::UserMetricsAction("webapps.AddShortcut.Manifest"));
104 } 105 }
105 106
106 shortcut_info_.UpdateFromManifest(manifest); 107 shortcut_info_.UpdateFromManifest(manifest);
107 108
108 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( 109 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon(
109 manifest.icons, 110 manifest.icons,
110 kPreferredIconSizeInDp, 111 kPreferredIconSizeInDp,
111 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); 112 gfx::Screen::GetScreenFor(web_contents()->GetNativeView()));
112 if (icon_src.is_valid()) { 113 if (icon_src.is_valid()) {
113 // Grab the best icon from the manifest. 114 // Grab the best icon from the manifest.
114 web_contents()->DownloadImage( 115 web_contents()->DownloadImage(
115 icon_src, 116 icon_src,
116 false, 117 false,
117 preferred_icon_size_in_px_, 118 preferred_icon_size_in_px_,
118 false, 119 false,
119 base::Bind(&ShortcutDataFetcher::OnManifestIconFetched, 120 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched,
120 this)); 121 this));
121 } else { 122 } else {
122 // Grab the best favicon for the page. 123 // Grab the best favicon for the page.
123 FetchFavicon(); 124 FetchFavicon();
124 } 125 }
125 126
126 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); 127 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title);
127 128
128 // Kick off a timeout for downloading the icon. If an icon isn't set within 129 // 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 // the timeout, fall back to using a dynamically-generated launcher icon.
130 icon_timeout_timer_.Start(FROM_HERE, 131 icon_timeout_timer_.Start(FROM_HERE,
131 base::TimeDelta::FromMilliseconds(3000), 132 base::TimeDelta::FromMilliseconds(3000),
132 base::Bind(&ShortcutDataFetcher::OnFaviconFetched, 133 base::Bind(
133 this, 134 &AddToHomescreenDataFetcher::OnFaviconFetched,
134 favicon_base::FaviconRawBitmapResult())); 135 this,
136 favicon_base::FaviconRawBitmapResult()));
135 } 137 }
136 138
137 bool ShortcutDataFetcher::OnMessageReceived(const IPC::Message& message) { 139 bool AddToHomescreenDataFetcher::OnMessageReceived(
140 const IPC::Message& message) {
138 if (!is_waiting_for_web_application_info_) return false; 141 if (!is_waiting_for_web_application_info_) return false;
139 142
140 bool handled = true; 143 bool handled = true;
141 144
142 IPC_BEGIN_MESSAGE_MAP(ShortcutDataFetcher, message) 145 IPC_BEGIN_MESSAGE_MAP(AddToHomescreenDataFetcher, message)
143 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, 146 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo,
144 OnDidGetWebApplicationInfo) 147 OnDidGetWebApplicationInfo)
145 IPC_MESSAGE_UNHANDLED(handled = false) 148 IPC_MESSAGE_UNHANDLED(handled = false)
146 IPC_END_MESSAGE_MAP() 149 IPC_END_MESSAGE_MAP()
147 150
148 return handled; 151 return handled;
149 } 152 }
150 153
151 ShortcutDataFetcher::~ShortcutDataFetcher() { 154 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() {
152 DCHECK(!weak_observer_); 155 DCHECK(!weak_observer_);
153 } 156 }
154 157
155 void ShortcutDataFetcher::FetchFavicon() { 158 void AddToHomescreenDataFetcher::FetchFavicon() {
156 if (!web_contents() || !weak_observer_) return; 159 if (!web_contents() || !weak_observer_) return;
157 160
158 Profile* profile = 161 Profile* profile =
159 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 162 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
160 163
161 // Grab the best, largest icon we can find to represent this bookmark. 164 // Grab the best, largest icon we can find to represent this bookmark.
162 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its 165 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its
163 // rewrite is further along. 166 // rewrite is further along.
164 std::vector<int> icon_types; 167 std::vector<int> icon_types;
165 icon_types.push_back(favicon_base::FAVICON); 168 icon_types.push_back(favicon_base::FAVICON);
166 icon_types.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON | 169 icon_types.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON |
167 favicon_base::TOUCH_ICON); 170 favicon_base::TOUCH_ICON);
168 favicon::FaviconService* favicon_service = 171 favicon::FaviconService* favicon_service =
169 FaviconServiceFactory::GetForProfile(profile, 172 FaviconServiceFactory::GetForProfile(profile,
170 ServiceAccessType::EXPLICIT_ACCESS); 173 ServiceAccessType::EXPLICIT_ACCESS);
171 174
172 // Using favicon if its size is not smaller than platform required size, 175 // Using favicon if its size is not smaller than platform required size,
173 // otherwise using the largest icon among all avaliable icons. 176 // otherwise using the largest icon among all avaliable icons.
174 int threshold_to_get_any_largest_icon = preferred_icon_size_in_px_ - 1; 177 int threshold_to_get_any_largest_icon = preferred_icon_size_in_px_ - 1;
175 favicon_service->GetLargestRawFaviconForPageURL( 178 favicon_service->GetLargestRawFaviconForPageURL(
176 shortcut_info_.url, 179 shortcut_info_.url,
177 icon_types, 180 icon_types,
178 threshold_to_get_any_largest_icon, 181 threshold_to_get_any_largest_icon,
179 base::Bind(&ShortcutDataFetcher::OnFaviconFetched, this), 182 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this),
180 &favicon_task_tracker_); 183 &favicon_task_tracker_);
181 } 184 }
182 185
183 void ShortcutDataFetcher::OnFaviconFetched( 186 void AddToHomescreenDataFetcher::OnFaviconFetched(
184 const favicon_base::FaviconRawBitmapResult& bitmap_result) { 187 const favicon_base::FaviconRawBitmapResult& bitmap_result) {
185 if (!web_contents() || !weak_observer_ || is_icon_saved_) { 188 if (!web_contents() || !weak_observer_ || is_icon_saved_) {
186 return; 189 return;
187 } 190 }
188 191
189 content::BrowserThread::PostTask( 192 content::BrowserThread::PostTask(
190 content::BrowserThread::IO, 193 content::BrowserThread::IO,
191 FROM_HERE, 194 FROM_HERE,
192 base::Bind(&ShortcutDataFetcher::CreateLauncherIcon, 195 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIcon,
193 this, 196 this,
194 bitmap_result)); 197 bitmap_result));
195 } 198 }
196 199
197 void ShortcutDataFetcher::CreateLauncherIcon( 200 void AddToHomescreenDataFetcher::CreateLauncherIcon(
198 const favicon_base::FaviconRawBitmapResult& bitmap_result) { 201 const favicon_base::FaviconRawBitmapResult& bitmap_result) {
199 if (!web_contents() || !weak_observer_) return; 202 if (!web_contents() || !weak_observer_) return;
200 203
201 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 204 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
202 SkBitmap icon_bitmap; 205 SkBitmap icon_bitmap;
203 if (bitmap_result.is_valid()) { 206 if (bitmap_result.is_valid()) {
204 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), 207 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(),
205 bitmap_result.bitmap_data->size(), 208 bitmap_result.bitmap_data->size(),
206 &icon_bitmap); 209 &icon_bitmap);
207 } 210 }
208 211
209 if (weak_observer_) { 212 if (weak_observer_) {
210 icon_bitmap = weak_observer_->FinalizeLauncherIcon(icon_bitmap, 213 icon_bitmap = weak_observer_->FinalizeLauncherIcon(icon_bitmap,
211 shortcut_info_.url); 214 shortcut_info_.url);
212 } 215 }
213 216
214 content::BrowserThread::PostTask( 217 content::BrowserThread::PostTask(
215 content::BrowserThread::UI, 218 content::BrowserThread::UI,
216 FROM_HERE, 219 FROM_HERE,
217 base::Bind(&ShortcutDataFetcher::NotifyObserver, this, icon_bitmap)); 220 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver,
221 this,
222 icon_bitmap));
218 } 223 }
219 224
220 void ShortcutDataFetcher::OnManifestIconFetched( 225 void AddToHomescreenDataFetcher::OnManifestIconFetched(
221 int id, 226 int id,
222 int http_status_code, 227 int http_status_code,
223 const GURL& url, 228 const GURL& url,
224 const std::vector<SkBitmap>& bitmaps, 229 const std::vector<SkBitmap>& bitmaps,
225 const std::vector<gfx::Size>& sizes) { 230 const std::vector<gfx::Size>& sizes) {
226 if (!web_contents() || !weak_observer_) return; 231 if (!web_contents() || !weak_observer_) return;
227 232
228 // If getting the candidate manifest icon failed, the ShortcutHelper should 233 // If getting the candidate manifest icon failed, the ShortcutHelper should
229 // fallback to the favicon. 234 // fallback to the favicon.
230 // Otherwise, it sets the state as if there was no manifest icon pending. 235 // Otherwise, it sets the state as if there was no manifest icon pending.
231 if (bitmaps.empty()) { 236 if (bitmaps.empty()) {
232 FetchFavicon(); 237 FetchFavicon();
233 return; 238 return;
234 } 239 }
235 240
236 // There might be multiple bitmaps returned. The one to pick is bigger or 241 // 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. 242 // equal to the preferred size. |bitmaps| is ordered from bigger to smaller.
238 int preferred_bitmap_index = 0; 243 int preferred_bitmap_index = 0;
239 for (size_t i = 0; i < bitmaps.size(); ++i) { 244 for (size_t i = 0; i < bitmaps.size(); ++i) {
240 if (bitmaps[i].height() < preferred_icon_size_in_px_) 245 if (bitmaps[i].height() < preferred_icon_size_in_px_)
241 break; 246 break;
242 preferred_bitmap_index = i; 247 preferred_bitmap_index = i;
243 } 248 }
244 249
245 NotifyObserver(bitmaps[preferred_bitmap_index]); 250 NotifyObserver(bitmaps[preferred_bitmap_index]);
246 } 251 }
247 252
248 void ShortcutDataFetcher::NotifyObserver(const SkBitmap& bitmap) { 253 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) {
249 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 254 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
250 if (!web_contents() || !weak_observer_ || is_icon_saved_) 255 if (!web_contents() || !weak_observer_ || is_icon_saved_)
251 return; 256 return;
252 257
253 is_icon_saved_ = true; 258 is_icon_saved_ = true;
254 shortcut_icon_ = bitmap; 259 shortcut_icon_ = bitmap;
255 is_ready_ = true; 260 is_ready_ = true;
256 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); 261 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_);
257 } 262 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698