Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / bitmap_fetcher / bitmap_fetcher.cc
blobae8ab659f47e23c8dbdbb67db33933429a2e5948
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 : url_(url), delegate_(delegate) {
18 BitmapFetcher::~BitmapFetcher() {
21 void BitmapFetcher::Start(net::URLRequestContextGetter* request_context,
22 const std::string& referrer,
23 net::URLRequest::ReferrerPolicy referrer_policy,
24 int load_flags) {
25 if (url_fetcher_ != NULL)
26 return;
28 url_fetcher_.reset(net::URLFetcher::Create(url_, net::URLFetcher::GET, this));
29 url_fetcher_->SetRequestContext(request_context);
30 url_fetcher_->SetReferrer(referrer);
31 url_fetcher_->SetReferrerPolicy(referrer_policy);
32 url_fetcher_->SetLoadFlags(load_flags);
33 url_fetcher_->Start();
36 // Methods inherited from URLFetcherDelegate.
38 void BitmapFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
39 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
41 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS) {
42 ReportFailure();
43 return;
46 std::string image_data;
47 source->GetResponseAsString(&image_data);
48 image_decoder_ =
49 new ImageDecoder(this, image_data, ImageDecoder::DEFAULT_CODEC);
51 // Call start to begin decoding. The ImageDecoder will call OnImageDecoded
52 // with the data when it is done.
53 scoped_refptr<base::MessageLoopProxy> task_runner =
54 content::BrowserThread::GetMessageLoopProxyForThread(
55 content::BrowserThread::UI);
56 image_decoder_->Start(task_runner);
59 void BitmapFetcher::OnURLFetchDownloadProgress(const net::URLFetcher* source,
60 int64 current,
61 int64 total) {
62 // Do nothing here.
65 // Methods inherited from ImageDecoder::Delegate.
67 void BitmapFetcher::OnImageDecoded(const ImageDecoder* decoder,
68 const SkBitmap& decoded_image) {
69 // Report success.
70 delegate_->OnFetchComplete(url_, &decoded_image);
73 void BitmapFetcher::OnDecodeImageFailed(const ImageDecoder* decoder) {
74 ReportFailure();
77 void BitmapFetcher::ReportFailure() {
78 delegate_->OnFetchComplete(url_, NULL);
81 } // namespace chrome