[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / chrome / browser / bitmap_fetcher.cc
blobc31b62aca2268e4b4f6b1cb212937588a2c6b5cb
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.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,
15 BitmapFetcherDelegate* delegate)
16 : url_(url),
17 delegate_(delegate) {
20 BitmapFetcher::~BitmapFetcher() {}
22 void BitmapFetcher::Start(net::URLRequestContextGetter* request_context) {
23 if (url_fetcher_ != NULL)
24 return;
26 url_fetcher_.reset(net::URLFetcher::Create(url_, net::URLFetcher::GET, this));
27 url_fetcher_->SetRequestContext(request_context);
28 url_fetcher_->Start();
31 // Methods inherited from URLFetcherDelegate.
33 void BitmapFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
36 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS) {
37 ReportFailure();
38 return;
41 std::string image_data;
42 source->GetResponseAsString(&image_data);
43 image_decoder_ =
44 new ImageDecoder(this, image_data, ImageDecoder::DEFAULT_CODEC);
46 // Call start to begin decoding. The ImageDecoder will call OnImageDecoded
47 // with the data when it is done.
48 scoped_refptr<base::MessageLoopProxy> task_runner =
49 content::BrowserThread::GetMessageLoopProxyForThread(
50 content::BrowserThread::UI);
51 image_decoder_->Start(task_runner);
54 void BitmapFetcher::OnURLFetchDownloadProgress(const net::URLFetcher* source,
55 int64 current,
56 int64 total) {
57 // Do nothing here.
60 // Methods inherited from ImageDecoder::Delegate.
62 void BitmapFetcher::OnImageDecoded(const ImageDecoder* decoder,
63 const SkBitmap& decoded_image) {
64 // Make a copy of the bitmap which we pass back to the UI thread.
65 scoped_ptr<SkBitmap> bitmap(new SkBitmap());
66 decoded_image.deepCopyTo(bitmap.get());
68 // Report success.
69 delegate_->OnFetchComplete(url_, bitmap.get());
72 void BitmapFetcher::OnDecodeImageFailed(const ImageDecoder* decoder) {
73 ReportFailure();
76 void BitmapFetcher::ReportFailure() {
77 delegate_->OnFetchComplete(url_, NULL);
80 } // namespace chrome