summaryrefslogtreecommitdiffstats
path: root/chrome/browser/bitmap_fetcher.cc
blob: 7894e607806e8450981df79afb0d3fdb6ab9f840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 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/bitmap_fetcher.h"

#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_status.h"

namespace chrome {

BitmapFetcher::BitmapFetcher(const GURL& url,
                             BitmapFetcherDelegate* delegate)
    : url_(url),
      delegate_(delegate) {
}

BitmapFetcher::~BitmapFetcher() {}

void BitmapFetcher::Start(Profile* profile) {
  DCHECK(url_fetcher_ == NULL);
  url_fetcher_.reset(net::URLFetcher::Create(url_, net::URLFetcher::GET, this));
  url_fetcher_->SetRequestContext(profile->GetRequestContext());
  url_fetcher_->Start();
}

// Methods inherited from URLFetcherDelegate.

void BitmapFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS) {
    ReportFailure();
    return;
  }

  std::string image_data;
  source->GetResponseAsString(&image_data);
  image_decoder_ =
      new ImageDecoder(this, image_data, ImageDecoder::DEFAULT_CODEC);

  // Call start to begin decoding.  The ImageDecoder will call OnImageDecoded
  // with the data when it is done.
  scoped_refptr<base::MessageLoopProxy> task_runner =
      content::BrowserThread::GetMessageLoopProxyForThread(
          content::BrowserThread::UI);
  image_decoder_->Start(task_runner);
}

void BitmapFetcher::OnURLFetchDownloadProgress(const net::URLFetcher* source,
                                               int64 current,
                                               int64 total) {
  // Do nothing here.
}

// Methods inherited from ImageDecoder::Delegate.

void BitmapFetcher::OnImageDecoded(const ImageDecoder* decoder,
                                   const SkBitmap& decoded_image) {
  // Make a copy of the bitmap which we pass back to the UI thread.
  scoped_ptr<SkBitmap> bitmap(new SkBitmap());
  decoded_image.deepCopyTo(bitmap.get());

  // Report success.
  delegate_->OnFetchComplete(url_, bitmap.get());
}

void BitmapFetcher::OnDecodeImageFailed(const ImageDecoder* decoder) {
  ReportFailure();
}

void BitmapFetcher::ReportFailure() {
  delegate_->OnFetchComplete(url_, NULL);
}

}  // namespace chrome