Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / bitmap_fetcher / bitmap_fetcher.cc
blobe413fa1a8e35de6643623c4434156ea5de9f9941
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
7 #include "content/public/browser/browser_thread.h"
8 #include "net/url_request/url_fetcher.h"
9 #include "net/url_request/url_request_context_getter.h"
10 #include "net/url_request/url_request_status.h"
12 namespace chrome {
14 BitmapFetcher::BitmapFetcher(const GURL& url, BitmapFetcherDelegate* delegate)
15 : ImageRequest(content::BrowserThread::GetMessageLoopProxyForThread(
16 content::BrowserThread::UI)),
17 url_(url),
18 delegate_(delegate) {
21 BitmapFetcher::~BitmapFetcher() {
24 void BitmapFetcher::Start(net::URLRequestContextGetter* request_context,
25 const std::string& referrer,
26 net::URLRequest::ReferrerPolicy referrer_policy,
27 int load_flags) {
28 if (url_fetcher_ != NULL)
29 return;
31 url_fetcher_.reset(net::URLFetcher::Create(url_, net::URLFetcher::GET, this));
32 url_fetcher_->SetRequestContext(request_context);
33 url_fetcher_->SetReferrer(referrer);
34 url_fetcher_->SetReferrerPolicy(referrer_policy);
35 url_fetcher_->SetLoadFlags(load_flags);
36 url_fetcher_->Start();
39 // Methods inherited from URLFetcherDelegate.
41 void BitmapFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
42 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
44 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS) {
45 ReportFailure();
46 return;
49 std::string image_data;
50 source->GetResponseAsString(&image_data);
52 // Call start to begin decoding. The ImageDecoder will call OnImageDecoded
53 // with the data when it is done.
54 ImageDecoder::Start(this, image_data);
57 void BitmapFetcher::OnURLFetchDownloadProgress(const net::URLFetcher* source,
58 int64 current,
59 int64 total) {
60 // Do nothing here.
63 // Methods inherited from ImageDecoder::ImageRequest.
65 void BitmapFetcher::OnImageDecoded(const SkBitmap& decoded_image) {
66 // Report success.
67 delegate_->OnFetchComplete(url_, &decoded_image);
70 void BitmapFetcher::OnDecodeImageFailed() {
71 ReportFailure();
74 void BitmapFetcher::ReportFailure() {
75 delegate_->OnFetchComplete(url_, NULL);
78 } // namespace chrome