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