Index: chrome/browser/android/banners/app_banner_icon_fetcher.cc |
diff --git a/chrome/browser/android/banners/app_banner_icon_fetcher.cc b/chrome/browser/android/banners/app_banner_icon_fetcher.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f5e298e5338c5b0f7a4be2efeabcfac89c103889 |
--- /dev/null |
+++ b/chrome/browser/android/banners/app_banner_icon_fetcher.cc |
@@ -0,0 +1,71 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/android/banners/app_banner_icon_fetcher.h" |
+ |
+#include "chrome/browser/profiles/profile.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/web_contents.h" |
+#include "net/url_request/url_fetcher.h" |
+#include "net/url_request/url_request_status.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+ |
+AppBannerIconFetcher::AppBannerIconFetcher(Delegate* delegate, |
+ content::WebContents* web_contents, |
+ const GURL& image_url) |
+ : delegate_(delegate), |
+ image_url_(image_url) { |
+ DCHECK(delegate_); |
+ Profile* profile = |
+ Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
+ fetcher_.reset(net::URLFetcher::Create(image_url, |
+ net::URLFetcher::GET, |
+ this)); |
+ fetcher_.get()->SetRequestContext(profile->GetRequestContext()); |
+ fetcher_.get()->Start(); |
Nico
2014/02/05 05:53:17
http://google-styleguide.googlecode.com/svn/trunk/
|
+} |
+ |
+AppBannerIconFetcher::~AppBannerIconFetcher() { |
+ delegate_ = NULL; |
+ fetcher_.reset(); |
+} |
+ |
+void AppBannerIconFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
+ std::string image_data; |
+ if (source->GetURL() != image_url_ || |
+ !source->GetStatus().is_success() || |
+ !source->GetResponseAsString(&image_data)) { |
+ AlertDelegateAboutFailure(); |
+ return; |
+ } |
+ |
+ // Begin converting the raw image data into a usable SkBitmap. |
+ scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder( |
+ this, |
+ image_data, |
+ ImageDecoder::DEFAULT_CODEC); |
Nico
2014/02/05 05:53:17
(`git cl format` formats this as
scoped_refptr<
|
+ scoped_refptr<base::MessageLoopProxy> task_runner = |
+ content::BrowserThread::GetMessageLoopProxyForThread( |
+ content::BrowserThread::UI); |
+ image_decoder->Start(task_runner); |
+} |
+ |
+void AppBannerIconFetcher::OnImageDecoded(const ImageDecoder* decoder, |
+ const SkBitmap& decoded_image) { |
+ if (!delegate_) return; |
+ |
+ if (decoded_image.getSize()) |
+ delegate_->OnIconFetchSuccessful(image_url_, decoded_image); |
+ else |
+ AlertDelegateAboutFailure(); |
+} |
+ |
+void AppBannerIconFetcher::OnDecodeImageFailed(const ImageDecoder* decoder) { |
+ AlertDelegateAboutFailure(); |
+} |
+ |
+void AppBannerIconFetcher::AlertDelegateAboutFailure() { |
+ if (delegate_) |
+ delegate_->OnIconFetchFailed(image_url_); |
+} |