| 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 35 | 35 |
| 36 using content::Manifest; | 36 using content::Manifest; |
| 37 | 37 |
| 38 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( | 38 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
| 39 content::WebContents* web_contents, | 39 content::WebContents* web_contents, |
| 40 int ideal_icon_size_in_dp, | 40 int ideal_icon_size_in_dp, |
| 41 int minimum_icon_size_in_dp, | 41 int minimum_icon_size_in_dp, |
| 42 int ideal_splash_image_size_in_dp, | 42 int ideal_splash_image_size_in_dp, |
| 43 int minimum_splash_image_size_in_dp, | 43 int minimum_splash_image_size_in_dp, |
| 44 bool stop_fetch_when_no_manifest, |
| 44 Observer* observer) | 45 Observer* observer) |
| 45 : WebContentsObserver(web_contents), | 46 : WebContentsObserver(web_contents), |
| 46 weak_observer_(observer), | 47 weak_observer_(observer), |
| 47 is_waiting_for_web_application_info_(false), | 48 is_waiting_for_web_application_info_(false), |
| 48 is_icon_saved_(false), | 49 is_icon_saved_(false), |
| 49 is_ready_(false), | 50 is_ready_(false), |
| 50 icon_timeout_timer_(false, false), | 51 icon_timeout_timer_(false, false), |
| 51 shortcut_info_(GetShortcutUrl(web_contents->GetURL())), | 52 shortcut_info_(GetShortcutUrl(web_contents->GetURL())), |
| 52 ideal_icon_size_in_dp_(ideal_icon_size_in_dp), | 53 ideal_icon_size_in_dp_(ideal_icon_size_in_dp), |
| 53 minimum_icon_size_in_dp_(minimum_icon_size_in_dp), | 54 minimum_icon_size_in_dp_(minimum_icon_size_in_dp), |
| 54 ideal_splash_image_size_in_dp_(ideal_splash_image_size_in_dp), | 55 ideal_splash_image_size_in_dp_(ideal_splash_image_size_in_dp), |
| 55 minimum_splash_image_size_in_dp_(minimum_splash_image_size_in_dp) { | 56 minimum_splash_image_size_in_dp_(minimum_splash_image_size_in_dp), |
| 57 stop_fetch_when_no_manifest_(stop_fetch_when_no_manifest) { |
| 56 DCHECK(minimum_icon_size_in_dp <= ideal_icon_size_in_dp); | 58 DCHECK(minimum_icon_size_in_dp <= ideal_icon_size_in_dp); |
| 57 DCHECK(minimum_splash_image_size_in_dp <= ideal_splash_image_size_in_dp); | 59 DCHECK(minimum_splash_image_size_in_dp <= ideal_splash_image_size_in_dp); |
| 58 | 60 |
| 59 // Send a message to the renderer to retrieve information about the page. | 61 // Send a message to the renderer to retrieve information about the page. |
| 60 is_waiting_for_web_application_info_ = true; | 62 is_waiting_for_web_application_info_ = true; |
| 61 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); | 63 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); |
| 62 } | 64 } |
| 63 | 65 |
| 64 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( | 66 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( |
| 65 const WebApplicationInfo& received_web_app_info) { | 67 const WebApplicationInfo& received_web_app_info) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 94 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: | 96 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: |
| 95 content::RecordAction( | 97 content::RecordAction( |
| 96 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); | 98 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); |
| 97 break; | 99 break; |
| 98 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: | 100 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: |
| 99 content::RecordAction( | 101 content::RecordAction( |
| 100 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); | 102 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); |
| 101 break; | 103 break; |
| 102 } | 104 } |
| 103 | 105 |
| 106 if (stop_fetch_when_no_manifest_) { |
| 107 web_contents()->HasManifest( |
| 108 base::Bind(&AddToHomescreenDataFetcher::OnDidHasManifest, this)); |
| 109 } else { |
| 110 web_contents()->GetManifest( |
| 111 base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest, this)); |
| 112 } |
| 113 } |
| 114 |
| 115 void AddToHomescreenDataFetcher::OnDidHasManifest(bool has_manifest) { |
| 116 if (!has_manifest) |
| 117 return; |
| 118 |
| 104 web_contents()->GetManifest( | 119 web_contents()->GetManifest( |
| 105 base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest, this)); | 120 base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest, this)); |
| 106 } | 121 } |
| 107 | 122 |
| 108 void AddToHomescreenDataFetcher::OnDidGetManifest( | 123 void AddToHomescreenDataFetcher::OnDidGetManifest( |
| 109 const GURL& manifest_url, | 124 const GURL& manifest_url, |
| 110 const content::Manifest& manifest) { | 125 const content::Manifest& manifest) { |
| 111 if (!web_contents() || !weak_observer_) return; | 126 if (!web_contents() || !weak_observer_) return; |
| 112 | 127 |
| 128 if (stop_fetch_when_no_manifest_ && manifest.IsEmpty()) return; |
| 129 |
| 130 weak_observer_->OnDidHasManifest(true); |
| 113 if (!manifest.IsEmpty()) { | 131 if (!manifest.IsEmpty()) { |
| 114 content::RecordAction( | 132 content::RecordAction( |
| 115 base::UserMetricsAction("webapps.AddShortcut.Manifest")); | 133 base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
| 116 shortcut_info_.UpdateFromManifest(manifest); | 134 shortcut_info_.UpdateFromManifest(manifest); |
| 117 shortcut_info_.manifest_url = manifest_url; | 135 shortcut_info_.manifest_url = manifest_url; |
| 136 icon_urls_.clear(); |
| 137 for (const auto& icon : manifest.icons) |
| 138 icon_urls_.push_back(icon.src.spec()); |
| 118 } | 139 } |
| 119 | 140 |
| 120 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | 141 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( |
| 121 manifest.icons, ideal_icon_size_in_dp_, minimum_icon_size_in_dp_); | 142 manifest.icons, ideal_icon_size_in_dp_, minimum_icon_size_in_dp_); |
| 122 | 143 |
| 123 // If fetching the Manifest icon fails, fallback to the best favicon | 144 // If fetching the Manifest icon fails, fallback to the best favicon |
| 124 // for the page. | 145 // for the page. |
| 125 if (!ManifestIconDownloader::Download( | 146 if (!ManifestIconDownloader::Download( |
| 126 web_contents(), | 147 web_contents(), |
| 127 icon_src, | 148 icon_src, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 157 | 178 |
| 158 IPC_BEGIN_MESSAGE_MAP(AddToHomescreenDataFetcher, message) | 179 IPC_BEGIN_MESSAGE_MAP(AddToHomescreenDataFetcher, message) |
| 159 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, | 180 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, |
| 160 OnDidGetWebApplicationInfo) | 181 OnDidGetWebApplicationInfo) |
| 161 IPC_MESSAGE_UNHANDLED(handled = false) | 182 IPC_MESSAGE_UNHANDLED(handled = false) |
| 162 IPC_END_MESSAGE_MAP() | 183 IPC_END_MESSAGE_MAP() |
| 163 | 184 |
| 164 return handled; | 185 return handled; |
| 165 } | 186 } |
| 166 | 187 |
| 188 void AddToHomescreenDataFetcher::ReplaceWebContents( |
| 189 content::WebContents* web_contents) { |
| 190 Observe(web_contents); |
| 191 } |
| 192 |
| 167 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { | 193 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { |
| 168 DCHECK(!weak_observer_); | 194 DCHECK(!weak_observer_); |
| 169 } | 195 } |
| 170 | 196 |
| 171 base::Closure AddToHomescreenDataFetcher::FetchSplashScreenImageCallback( | 197 base::Closure AddToHomescreenDataFetcher::FetchSplashScreenImageCallback( |
| 172 const std::string& webapp_id) { | 198 const std::string& webapp_id) { |
| 173 return base::Bind(&ShortcutHelper::FetchSplashScreenImage, web_contents(), | 199 return base::Bind(&ShortcutHelper::FetchSplashScreenImage, web_contents(), |
| 174 splash_screen_url_, ideal_splash_image_size_in_dp_, | 200 splash_screen_url_, ideal_splash_image_size_in_dp_, |
| 175 minimum_splash_image_size_in_dp_, webapp_id); | 201 minimum_splash_image_size_in_dp_, webapp_id); |
| 176 } | 202 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 } | 282 } |
| 257 | 283 |
| 258 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { | 284 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { |
| 259 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 285 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 260 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 286 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 261 return; | 287 return; |
| 262 | 288 |
| 263 is_icon_saved_ = true; | 289 is_icon_saved_ = true; |
| 264 shortcut_icon_ = bitmap; | 290 shortcut_icon_ = bitmap; |
| 265 is_ready_ = true; | 291 is_ready_ = true; |
| 266 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | 292 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_, icon_urls_); |
| 267 } | 293 } |
| 268 | 294 |
| 269 GURL AddToHomescreenDataFetcher::GetShortcutUrl(const GURL& actual_url) { | 295 GURL AddToHomescreenDataFetcher::GetShortcutUrl(const GURL& actual_url) { |
| 270 GURL original_url = | 296 GURL original_url = |
| 271 dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url); | 297 dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url); |
| 272 | 298 |
| 273 // If URL points to an offline content, get original URL. | 299 // If URL points to an offline content, get original URL. |
| 274 GURL online_url = | 300 GURL online_url = |
| 275 offline_pages::OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | 301 offline_pages::OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( |
| 276 web_contents()->GetBrowserContext(), original_url); | 302 web_contents()->GetBrowserContext(), original_url); |
| 277 if (online_url.is_valid()) | 303 if (online_url.is_valid()) |
| 278 return online_url; | 304 return online_url; |
| 279 | 305 |
| 280 return original_url; | 306 return original_url; |
| 281 } | 307 } |
| OLD | NEW |