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

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

Issue 2611623003: Use exact pixel sizes instead of dip in webapp/WebAPK installability code (Closed)
Patch Set: Rebased version of pkotwicz@'s original patch Created 3 years, 11 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/webapps/add_to_homescreen_data_fetcher.h" 5 #include "chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 11 matching lines...) Expand all
22 #include "chrome/common/web_application_info.h" 22 #include "chrome/common/web_application_info.h"
23 #include "components/dom_distiller/core/url_utils.h" 23 #include "components/dom_distiller/core/url_utils.h"
24 #include "components/favicon/core/favicon_service.h" 24 #include "components/favicon/core/favicon_service.h"
25 #include "components/favicon_base/favicon_types.h" 25 #include "components/favicon_base/favicon_types.h"
26 #include "content/public/browser/browser_thread.h" 26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/user_metrics.h" 27 #include "content/public/browser/user_metrics.h"
28 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
29 #include "content/public/browser/web_contents_observer.h" 29 #include "content/public/browser/web_contents_observer.h"
30 #include "content/public/common/manifest.h" 30 #include "content/public/common/manifest.h"
31 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree nOrientationLockType.h" 31 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree nOrientationLockType.h"
32 #include "ui/display/display.h"
33 #include "ui/display/screen.h"
34 #include "ui/gfx/codec/png_codec.h" 32 #include "ui/gfx/codec/png_codec.h"
35 #include "ui/gfx/favicon_size.h" 33 #include "ui/gfx/favicon_size.h"
36 #include "url/gurl.h" 34 #include "url/gurl.h"
37 35
38 namespace { 36 namespace {
39 37
40 // Looks up the original, online URL of the site requested. The URL from the 38 // Looks up the original, online URL of the site requested. The URL from the
41 // WebContents may be a distilled article which is not appropriate for a home 39 // WebContents may be a distilled article which is not appropriate for a home
42 // screen shortcut. 40 // screen shortcut.
43 GURL GetShortcutUrl(content::BrowserContext* browser_context, 41 GURL GetShortcutUrl(content::BrowserContext* browser_context,
44 const GURL& actual_url) { 42 const GURL& actual_url) {
45 return dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url); 43 return dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url);
46 } 44 }
47 45
48 InstallableParams ParamsToPerformInstallableCheck(int ideal_icon_size_in_dp, 46 InstallableParams ParamsToPerformInstallableCheck(int ideal_icon_size_in_px,
49 int minimum_icon_size_in_dp, 47 int minimum_icon_size_in_px,
50 bool check_installable) { 48 bool check_installable) {
51 InstallableParams params; 49 InstallableParams params;
52 params.ideal_icon_size_in_dp = ideal_icon_size_in_dp; 50 params.ideal_icon_size_in_px = ideal_icon_size_in_px;
53 params.minimum_icon_size_in_dp = minimum_icon_size_in_dp; 51 params.minimum_icon_size_in_px = minimum_icon_size_in_px;
54 params.check_installable = check_installable; 52 params.check_installable = check_installable;
55 params.fetch_valid_icon = true; 53 params.fetch_valid_icon = true;
56 return params; 54 return params;
57 } 55 }
58 56
59 } // namespace 57 } // namespace
60 58
61 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( 59 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher(
62 content::WebContents* web_contents, 60 content::WebContents* web_contents,
63 int ideal_icon_size_in_dp, 61 int ideal_icon_size_in_px,
64 int minimum_icon_size_in_dp, 62 int minimum_icon_size_in_px,
65 int ideal_splash_image_size_in_dp, 63 int ideal_splash_image_size_in_px,
66 int minimum_splash_image_size_in_dp, 64 int minimum_splash_image_size_in_px,
67 bool check_webapk_compatibility, 65 bool check_webapk_compatibility,
68 Observer* observer) 66 Observer* observer)
69 : WebContentsObserver(web_contents), 67 : WebContentsObserver(web_contents),
70 weak_observer_(observer), 68 weak_observer_(observer),
71 shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(), 69 shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(),
72 web_contents->GetLastCommittedURL())), 70 web_contents->GetLastCommittedURL())),
73 data_timeout_timer_(false, false), 71 data_timeout_timer_(false, false),
74 ideal_icon_size_in_dp_(ideal_icon_size_in_dp), 72 ideal_icon_size_in_px_(ideal_icon_size_in_px),
75 minimum_icon_size_in_dp_(minimum_icon_size_in_dp), 73 minimum_icon_size_in_px_(minimum_icon_size_in_px),
76 ideal_splash_image_size_in_dp_(ideal_splash_image_size_in_dp), 74 ideal_splash_image_size_in_px_(ideal_splash_image_size_in_px),
77 minimum_splash_image_size_in_dp_(minimum_splash_image_size_in_dp), 75 minimum_splash_image_size_in_px_(minimum_splash_image_size_in_px),
78 check_webapk_compatibility_(check_webapk_compatibility), 76 check_webapk_compatibility_(check_webapk_compatibility),
79 is_waiting_for_web_application_info_(true), 77 is_waiting_for_web_application_info_(true),
80 is_installable_check_complete_(false), 78 is_installable_check_complete_(false),
81 is_icon_saved_(false), 79 is_icon_saved_(false),
82 is_ready_(false) { 80 is_ready_(false) {
83 DCHECK(minimum_icon_size_in_dp <= ideal_icon_size_in_dp); 81 DCHECK(minimum_icon_size_in_px <= ideal_icon_size_in_px);
84 DCHECK(minimum_splash_image_size_in_dp <= ideal_splash_image_size_in_dp); 82 DCHECK(minimum_splash_image_size_in_px <= ideal_splash_image_size_in_px);
85 83
86 // Send a message to the renderer to retrieve information about the page. 84 // Send a message to the renderer to retrieve information about the page.
87 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); 85 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id()));
88 } 86 }
89 87
90 base::Closure AddToHomescreenDataFetcher::FetchSplashScreenImageCallback( 88 base::Closure AddToHomescreenDataFetcher::FetchSplashScreenImageCallback(
91 const std::string& webapp_id) { 89 const std::string& webapp_id) {
92 return base::Bind(&ShortcutHelper::FetchSplashScreenImage, web_contents(), 90 return base::Bind(&ShortcutHelper::FetchSplashScreenImage, web_contents(),
93 splash_screen_url_, ideal_splash_image_size_in_dp_, 91 splash_screen_url_, ideal_splash_image_size_in_px_,
94 minimum_splash_image_size_in_dp_, webapp_id); 92 minimum_splash_image_size_in_px_, webapp_id);
95 } 93 }
96 94
97 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( 95 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo(
98 const WebApplicationInfo& received_web_app_info) { 96 const WebApplicationInfo& received_web_app_info) {
99 is_waiting_for_web_application_info_ = false; 97 is_waiting_for_web_application_info_ = false;
100 if (!web_contents() || !weak_observer_) 98 if (!web_contents() || !weak_observer_)
101 return; 99 return;
102 100
103 // Sanitize received_web_app_info. 101 // Sanitize received_web_app_info.
104 WebApplicationInfo web_app_info = received_web_app_info; 102 WebApplicationInfo web_app_info = received_web_app_info;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 InstallableManager::FromWebContents(web_contents()); 136 InstallableManager::FromWebContents(web_contents());
139 DCHECK(manager); 137 DCHECK(manager);
140 138
141 // Kick off a timeout for downloading data. If we haven't finished within the 139 // Kick off a timeout for downloading data. If we haven't finished within the
142 // timeout, fall back to using a dynamically-generated launcher icon. 140 // timeout, fall back to using a dynamically-generated launcher icon.
143 data_timeout_timer_.Start( 141 data_timeout_timer_.Start(
144 FROM_HERE, base::TimeDelta::FromMilliseconds(4000), 142 FROM_HERE, base::TimeDelta::FromMilliseconds(4000),
145 base::Bind(&AddToHomescreenDataFetcher::OnDataTimedout, this)); 143 base::Bind(&AddToHomescreenDataFetcher::OnDataTimedout, this));
146 144
147 manager->GetData( 145 manager->GetData(
148 ParamsToPerformInstallableCheck(ideal_icon_size_in_dp_, 146 ParamsToPerformInstallableCheck(ideal_icon_size_in_px_,
149 minimum_icon_size_in_dp_, 147 minimum_icon_size_in_px_,
150 check_webapk_compatibility_), 148 check_webapk_compatibility_),
151 base::Bind(&AddToHomescreenDataFetcher::OnDidPerformInstallableCheck, 149 base::Bind(&AddToHomescreenDataFetcher::OnDidPerformInstallableCheck,
152 this)); 150 this));
153 } 151 }
154 152
155 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { 153 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() {
156 DCHECK(!weak_observer_); 154 DCHECK(!weak_observer_);
157 } 155 }
158 156
159 bool AddToHomescreenDataFetcher::OnMessageReceived( 157 bool AddToHomescreenDataFetcher::OnMessageReceived(
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 206
209 if (!data.manifest.IsEmpty()) { 207 if (!data.manifest.IsEmpty()) {
210 content::RecordAction( 208 content::RecordAction(
211 base::UserMetricsAction("webapps.AddShortcut.Manifest")); 209 base::UserMetricsAction("webapps.AddShortcut.Manifest"));
212 shortcut_info_.UpdateFromManifest(data.manifest); 210 shortcut_info_.UpdateFromManifest(data.manifest);
213 shortcut_info_.manifest_url = data.manifest_url; 211 shortcut_info_.manifest_url = data.manifest_url;
214 } 212 }
215 213
216 // Save the splash screen URL for the later download. 214 // Save the splash screen URL for the later download.
217 splash_screen_url_ = ManifestIconSelector::FindBestMatchingIcon( 215 splash_screen_url_ = ManifestIconSelector::FindBestMatchingIcon(
218 data.manifest.icons, ideal_splash_image_size_in_dp_, 216 data.manifest.icons, ideal_splash_image_size_in_px_,
219 minimum_splash_image_size_in_dp_); 217 minimum_splash_image_size_in_px_);
220 218
221 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); 219 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title);
222 220
223 if (data.icon) { 221 if (data.icon) {
224 shortcut_info_.best_icon_url = data.icon_url; 222 shortcut_info_.best_icon_url = data.icon_url;
225 223
226 CreateLauncherIcon(*(data.icon)); 224 CreateLauncherIcon(*(data.icon));
227 return; 225 return;
228 } 226 }
229 227
(...skipping 11 matching lines...) Expand all
241 favicon_base::FAVICON, 239 favicon_base::FAVICON,
242 favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON}; 240 favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON};
243 241
244 favicon::FaviconService* favicon_service = 242 favicon::FaviconService* favicon_service =
245 FaviconServiceFactory::GetForProfile( 243 FaviconServiceFactory::GetForProfile(
246 Profile::FromBrowserContext(web_contents()->GetBrowserContext()), 244 Profile::FromBrowserContext(web_contents()->GetBrowserContext()),
247 ServiceAccessType::EXPLICIT_ACCESS); 245 ServiceAccessType::EXPLICIT_ACCESS);
248 246
249 // Using favicon if its size is not smaller than platform required size, 247 // Using favicon if its size is not smaller than platform required size,
250 // otherwise using the largest icon among all avaliable icons. 248 // otherwise using the largest icon among all avaliable icons.
251 int ideal_icon_size_in_px = 249 int threshold_to_get_any_largest_icon = ideal_icon_size_in_px_ - 1;
252 ideal_icon_size_in_dp_ *
253 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor();
254 int threshold_to_get_any_largest_icon = ideal_icon_size_in_px - 1;
255 favicon_service->GetLargestRawFaviconForPageURL( 250 favicon_service->GetLargestRawFaviconForPageURL(
256 shortcut_info_.url, icon_types, threshold_to_get_any_largest_icon, 251 shortcut_info_.url, icon_types, threshold_to_get_any_largest_icon,
257 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), 252 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this),
258 &favicon_task_tracker_); 253 &favicon_task_tracker_);
259 } 254 }
260 255
261 void AddToHomescreenDataFetcher::OnFaviconFetched( 256 void AddToHomescreenDataFetcher::OnFaviconFetched(
262 const favicon_base::FaviconRawBitmapResult& bitmap_result) { 257 const favicon_base::FaviconRawBitmapResult& bitmap_result) {
263 if (!web_contents() || !weak_observer_ || is_icon_saved_) 258 if (!web_contents() || !weak_observer_ || is_icon_saved_)
264 return; 259 return;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& icon) { 312 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& icon) {
318 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 313 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
319 if (!web_contents() || !weak_observer_ || is_icon_saved_) 314 if (!web_contents() || !weak_observer_ || is_icon_saved_)
320 return; 315 return;
321 316
322 is_icon_saved_ = true; 317 is_icon_saved_ = true;
323 shortcut_icon_ = icon; 318 shortcut_icon_ = icon;
324 is_ready_ = true; 319 is_ready_ = true;
325 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); 320 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_);
326 } 321 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698