| OLD | NEW |
| 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 "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" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree
nOrientationLockType.h" | 27 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree
nOrientationLockType.h" |
| 28 #include "ui/gfx/codec/png_codec.h" | 28 #include "ui/gfx/codec/png_codec.h" |
| 29 #include "ui/gfx/favicon_size.h" | 29 #include "ui/gfx/favicon_size.h" |
| 30 #include "ui/gfx/screen.h" | 30 #include "ui/gfx/screen.h" |
| 31 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 32 | 32 |
| 33 using content::Manifest; | 33 using content::Manifest; |
| 34 | 34 |
| 35 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( | 35 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
| 36 content::WebContents* web_contents, | 36 content::WebContents* web_contents, |
| 37 int ideal_icon_size_in_dp, |
| 38 int minimum_icon_size_in_dp, |
| 37 int ideal_splash_image_size_in_dp, | 39 int ideal_splash_image_size_in_dp, |
| 38 int ideal_icon_size_in_dp, | 40 int minimum_splash_image_size_in_dp, |
| 39 Observer* observer) | 41 Observer* observer) |
| 40 : WebContentsObserver(web_contents), | 42 : WebContentsObserver(web_contents), |
| 41 weak_observer_(observer), | 43 weak_observer_(observer), |
| 42 is_waiting_for_web_application_info_(false), | 44 is_waiting_for_web_application_info_(false), |
| 43 is_icon_saved_(false), | 45 is_icon_saved_(false), |
| 44 is_ready_(false), | 46 is_ready_(false), |
| 45 icon_timeout_timer_(false, false), | 47 icon_timeout_timer_(false, false), |
| 46 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( | 48 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( |
| 47 web_contents->GetURL())), | 49 web_contents->GetURL())), |
| 50 ideal_icon_size_in_dp_(ideal_icon_size_in_dp), |
| 51 minimum_icon_size_in_dp_(minimum_icon_size_in_dp), |
| 48 ideal_splash_image_size_in_dp_(ideal_splash_image_size_in_dp), | 52 ideal_splash_image_size_in_dp_(ideal_splash_image_size_in_dp), |
| 49 ideal_icon_size_in_dp_(ideal_icon_size_in_dp) { | 53 minimum_splash_image_size_in_dp_(minimum_splash_image_size_in_dp) { |
| 54 DCHECK(minimum_icon_size_in_dp <= ideal_icon_size_in_dp); |
| 55 DCHECK(minimum_splash_image_size_in_dp <= ideal_splash_image_size_in_dp); |
| 56 |
| 50 // Send a message to the renderer to retrieve information about the page. | 57 // Send a message to the renderer to retrieve information about the page. |
| 51 is_waiting_for_web_application_info_ = true; | 58 is_waiting_for_web_application_info_ = true; |
| 52 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); | 59 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); |
| 53 } | 60 } |
| 54 | 61 |
| 55 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( | 62 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( |
| 56 const WebApplicationInfo& received_web_app_info) { | 63 const WebApplicationInfo& received_web_app_info) { |
| 57 is_waiting_for_web_application_info_ = false; | 64 is_waiting_for_web_application_info_ = false; |
| 58 if (!web_contents() || !weak_observer_) return; | 65 if (!web_contents() || !weak_observer_) return; |
| 59 | 66 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 109 |
| 103 if (!manifest.IsEmpty()) { | 110 if (!manifest.IsEmpty()) { |
| 104 content::RecordAction( | 111 content::RecordAction( |
| 105 base::UserMetricsAction("webapps.AddShortcut.Manifest")); | 112 base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
| 106 shortcut_info_.UpdateFromManifest(manifest); | 113 shortcut_info_.UpdateFromManifest(manifest); |
| 107 } | 114 } |
| 108 | 115 |
| 109 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | 116 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( |
| 110 manifest.icons, | 117 manifest.icons, |
| 111 ideal_icon_size_in_dp_, | 118 ideal_icon_size_in_dp_, |
| 119 minimum_icon_size_in_dp_, |
| 112 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | 120 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); |
| 113 | 121 |
| 114 // If fetching the Manifest icon fails, fallback to the best favicon | 122 // If fetching the Manifest icon fails, fallback to the best favicon |
| 115 // for the page. | 123 // for the page. |
| 116 if (!ManifestIconDownloader::Download( | 124 if (!ManifestIconDownloader::Download( |
| 117 web_contents(), | 125 web_contents(), |
| 118 icon_src, | 126 icon_src, |
| 119 ideal_icon_size_in_dp_, | 127 ideal_icon_size_in_dp_, |
| 128 minimum_icon_size_in_dp_, |
| 120 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, | 129 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, |
| 121 this))) { | 130 this))) { |
| 122 FetchFavicon(); | 131 FetchFavicon(); |
| 123 } | 132 } |
| 124 | 133 |
| 125 // Save the splash screen URL for the later download. | 134 // Save the splash screen URL for the later download. |
| 126 splash_screen_url_ = ManifestIconSelector::FindBestMatchingIcon( | 135 splash_screen_url_ = ManifestIconSelector::FindBestMatchingIcon( |
| 127 manifest.icons, | 136 manifest.icons, |
| 128 ideal_splash_image_size_in_dp_, | 137 ideal_splash_image_size_in_dp_, |
| 138 minimum_splash_image_size_in_dp_, |
| 129 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | 139 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); |
| 130 | 140 |
| 131 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); | 141 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
| 132 | 142 |
| 133 // Kick off a timeout for downloading the icon. If an icon isn't set within | 143 // 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. | 144 // the timeout, fall back to using a dynamically-generated launcher icon. |
| 135 icon_timeout_timer_.Start(FROM_HERE, | 145 icon_timeout_timer_.Start(FROM_HERE, |
| 136 base::TimeDelta::FromMilliseconds(3000), | 146 base::TimeDelta::FromMilliseconds(3000), |
| 137 base::Bind( | 147 base::Bind( |
| 138 &AddToHomescreenDataFetcher::OnFaviconFetched, | 148 &AddToHomescreenDataFetcher::OnFaviconFetched, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 158 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { | 168 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { |
| 159 DCHECK(!weak_observer_); | 169 DCHECK(!weak_observer_); |
| 160 } | 170 } |
| 161 | 171 |
| 162 void AddToHomescreenDataFetcher::FetchSplashScreenImage( | 172 void AddToHomescreenDataFetcher::FetchSplashScreenImage( |
| 163 const std::string& webapp_id) { | 173 const std::string& webapp_id) { |
| 164 ShortcutHelper::FetchSplashScreenImage( | 174 ShortcutHelper::FetchSplashScreenImage( |
| 165 web_contents(), | 175 web_contents(), |
| 166 splash_screen_url_, | 176 splash_screen_url_, |
| 167 ideal_splash_image_size_in_dp_, | 177 ideal_splash_image_size_in_dp_, |
| 178 minimum_splash_image_size_in_dp_, |
| 168 webapp_id); | 179 webapp_id); |
| 169 } | 180 } |
| 170 | 181 |
| 171 void AddToHomescreenDataFetcher::FetchFavicon() { | 182 void AddToHomescreenDataFetcher::FetchFavicon() { |
| 172 if (!web_contents() || !weak_observer_) return; | 183 if (!web_contents() || !weak_observer_) return; |
| 173 | 184 |
| 174 Profile* profile = | 185 Profile* profile = |
| 175 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 186 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 176 | 187 |
| 177 // Grab the best, largest icon we can find to represent this bookmark. | 188 // Grab the best, largest icon we can find to represent this bookmark. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { | 259 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { |
| 249 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 260 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 250 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 261 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 251 return; | 262 return; |
| 252 | 263 |
| 253 is_icon_saved_ = true; | 264 is_icon_saved_ = true; |
| 254 shortcut_icon_ = bitmap; | 265 shortcut_icon_ = bitmap; |
| 255 is_ready_ = true; | 266 is_ready_ = true; |
| 256 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | 267 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); |
| 257 } | 268 } |
| OLD | NEW |