| 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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 threshold_to_get_any_largest_icon, | 204 threshold_to_get_any_largest_icon, |
| 205 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), | 205 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), |
| 206 &favicon_task_tracker_); | 206 &favicon_task_tracker_); |
| 207 } | 207 } |
| 208 | 208 |
| 209 void AddToHomescreenDataFetcher::OnFaviconFetched( | 209 void AddToHomescreenDataFetcher::OnFaviconFetched( |
| 210 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 210 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 211 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 211 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 212 return; | 212 return; |
| 213 | 213 |
| 214 content::BrowserThread::PostTask( | 214 content::BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior( |
| 215 content::BrowserThread::IO, | |
| 216 FROM_HERE, | 215 FROM_HERE, |
| 217 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIcon, | 216 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIconInBackground, |
| 218 this, | 217 this, shortcut_info_.url, bitmap_result), |
| 219 bitmap_result)); | 218 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 220 } | 219 } |
| 221 | 220 |
| 222 void AddToHomescreenDataFetcher::CreateLauncherIcon( | 221 void AddToHomescreenDataFetcher::CreateLauncherIconInBackground( |
| 222 const GURL& page_url, |
| 223 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 223 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 224 if (!web_contents() || !weak_observer_) return; | 224 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 225 | 225 |
| 226 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 227 SkBitmap icon_bitmap; | 226 SkBitmap icon_bitmap; |
| 228 if (bitmap_result.is_valid()) { | 227 if (bitmap_result.is_valid()) { |
| 229 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), | 228 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), |
| 230 bitmap_result.bitmap_data->size(), | 229 bitmap_result.bitmap_data->size(), |
| 231 &icon_bitmap); | 230 &icon_bitmap); |
| 232 } | 231 } |
| 233 | 232 |
| 234 bool is_generated = false; | 233 bool is_generated = false; |
| 235 if (weak_observer_) { | 234 if (weak_observer_) { |
| 236 icon_bitmap = weak_observer_->FinalizeLauncherIcon(icon_bitmap, | 235 icon_bitmap = weak_observer_->FinalizeLauncherIconInBackground( |
| 237 shortcut_info_.url, | 236 icon_bitmap, page_url, &is_generated); |
| 238 &is_generated); | |
| 239 } | 237 } |
| 240 | 238 |
| 241 shortcut_info_.icon_url = is_generated ? GURL() : bitmap_result.icon_url; | 239 GURL icon_url = is_generated ? GURL() : bitmap_result.icon_url; |
| 242 content::BrowserThread::PostTask( | 240 content::BrowserThread::PostTask( |
| 243 content::BrowserThread::UI, FROM_HERE, | 241 content::BrowserThread::UI, FROM_HERE, |
| 244 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, this, | 242 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, this, icon_url, |
| 245 icon_bitmap)); | 243 icon_bitmap)); |
| 246 } | 244 } |
| 247 | 245 |
| 248 void AddToHomescreenDataFetcher::OnManifestIconFetched(const GURL& icon_url, | 246 void AddToHomescreenDataFetcher::OnManifestIconFetched(const GURL& icon_url, |
| 249 const SkBitmap& icon) { | 247 const SkBitmap& icon) { |
| 250 if (icon.drawsNothing()) { | 248 if (icon.drawsNothing()) { |
| 251 FetchFavicon(); | 249 FetchFavicon(); |
| 252 return; | 250 return; |
| 253 } | 251 } |
| 254 shortcut_info_.icon_url = icon_url; | 252 NotifyObserver(icon_url, icon); |
| 255 NotifyObserver(icon); | |
| 256 } | 253 } |
| 257 | 254 |
| 258 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { | 255 void AddToHomescreenDataFetcher::NotifyObserver(const GURL& icon_url, |
| 256 const SkBitmap& bitmap) { |
| 259 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 257 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 260 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 258 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 261 return; | 259 return; |
| 262 | 260 |
| 263 is_icon_saved_ = true; | 261 is_icon_saved_ = true; |
| 262 shortcut_info_.icon_url = icon_url; |
| 264 shortcut_icon_ = bitmap; | 263 shortcut_icon_ = bitmap; |
| 265 is_ready_ = true; | 264 is_ready_ = true; |
| 266 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | 265 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); |
| 267 } | 266 } |
| 268 | 267 |
| 269 GURL AddToHomescreenDataFetcher::GetShortcutUrl(const GURL& actual_url) { | 268 GURL AddToHomescreenDataFetcher::GetShortcutUrl(const GURL& actual_url) { |
| 270 GURL original_url = | 269 GURL original_url = |
| 271 dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url); | 270 dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url); |
| 272 | 271 |
| 273 // If URL points to an offline content, get original URL. | 272 // If URL points to an offline content, get original URL. |
| 274 GURL online_url = | 273 GURL online_url = |
| 275 offline_pages::OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | 274 offline_pages::OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( |
| 276 web_contents()->GetBrowserContext(), original_url); | 275 web_contents()->GetBrowserContext(), original_url); |
| 277 if (online_url.is_valid()) | 276 if (online_url.is_valid()) |
| 278 return online_url; | 277 return online_url; |
| 279 | 278 |
| 280 return original_url; | 279 return original_url; |
| 281 } | 280 } |
| OLD | NEW |