Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / bitmap_fetcher.cc
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.
4
5 #include "chrome/browser/bitmap_fetcher.h"
6
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"
11
12 namespace chrome {
13
14 BitmapFetcher::BitmapFetcher(const GURL& url,
15                              BitmapFetcherDelegate* delegate)
16     : url_(url),
17       delegate_(delegate) {
18 }
19
20 BitmapFetcher::~BitmapFetcher() {}
21
22 void BitmapFetcher::Start(net::URLRequestContextGetter* request_context,
23                           const std::string& referrer,
24                           net::URLRequest::ReferrerPolicy referrer_policy,
25                           int load_flags) {
26   if (url_fetcher_ != NULL)
27     return;
28
29   url_fetcher_.reset(net::URLFetcher::Create(url_, net::URLFetcher::GET, this));
30   url_fetcher_->SetRequestContext(request_context);
31   url_fetcher_->SetReferrer(referrer);
32   url_fetcher_->SetReferrerPolicy(referrer_policy);
33   url_fetcher_->SetLoadFlags(load_flags);
34   url_fetcher_->Start();
35 }
36
37 // Methods inherited from URLFetcherDelegate.
38
39 void BitmapFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
40   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
41
42   if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS) {
43     ReportFailure();
44     return;
45   }
46
47   std::string image_data;
48   source->GetResponseAsString(&image_data);
49   image_decoder_ =
50       new ImageDecoder(this, image_data, ImageDecoder::DEFAULT_CODEC);
51
52   // Call start to begin decoding.  The ImageDecoder will call OnImageDecoded
53   // with the data when it is done.
54   scoped_refptr<base::MessageLoopProxy> task_runner =
55       content::BrowserThread::GetMessageLoopProxyForThread(
56           content::BrowserThread::UI);
57   image_decoder_->Start(task_runner);
58 }
59
60 void BitmapFetcher::OnURLFetchDownloadProgress(const net::URLFetcher* source,
61                                                int64 current,
62                                                int64 total) {
63   // Do nothing here.
64 }
65
66 // Methods inherited from ImageDecoder::Delegate.
67
68 void BitmapFetcher::OnImageDecoded(const ImageDecoder* decoder,
69                                    const SkBitmap& decoded_image) {
70   // Make a copy of the bitmap which we pass back to the UI thread.
71   scoped_ptr<SkBitmap> bitmap(new SkBitmap());
72   decoded_image.deepCopyTo(bitmap.get());
73
74   // Report success.
75   delegate_->OnFetchComplete(url_, bitmap.get());
76 }
77
78 void BitmapFetcher::OnDecodeImageFailed(const ImageDecoder* decoder) {
79   ReportFailure();
80 }
81
82 void BitmapFetcher::ReportFailure() {
83   delegate_->OnFetchComplete(url_, NULL);
84 }
85
86 }  // namespace chrome