- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / sync_notifier / notification_bitmap_fetcher.cc
1 // Copyright 2013 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/notifications/sync_notifier/notification_bitmap_fetcher.h"
6
7 #include "chrome/browser/profiles/profile.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "net/url_request/url_fetcher.h"
10 #include "net/url_request/url_request_status.h"
11
12 namespace notifier {
13
14 NotificationBitmapFetcher::NotificationBitmapFetcher(
15     const GURL& url,
16     NotificationBitmapFetcherDelegate* delegate)
17     : url_(url), delegate_(delegate) {}
18
19 NotificationBitmapFetcher::~NotificationBitmapFetcher() {}
20
21 void NotificationBitmapFetcher::Start(Profile* profile) {
22   url_fetcher_.reset(
23       net::URLFetcher::Create(url_, net::URLFetcher::GET, this));
24   // The RequestContext is coming from the current profile.
25   // TODO(petewil): Make sure this is the right profile to use.
26   // It seems to work, but we might prefer to use a blank profile with
27   // no cookies.
28   url_fetcher_->SetRequestContext(profile->GetRequestContext());
29   url_fetcher_->Start();
30 }
31
32 // Methods inherited from URLFetcherDelegate.
33
34 void NotificationBitmapFetcher::OnURLFetchComplete(
35     const net::URLFetcher* source) {
36   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
37
38   if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS) {
39     OnDecodeImageFailed(NULL);
40     return;
41   }
42
43   std::string image_data;
44   source->GetResponseAsString(&image_data);
45   // Create an ImageDecoder with the data and assign it to the refptr.
46   image_decoder_ = new ImageDecoder(this, image_data,
47                                     ImageDecoder::DEFAULT_CODEC);
48
49   // Call start to begin decoding.  The ImageDecoder will call OnImageDecoded
50   // with the data when it is done.
51   scoped_refptr<base::MessageLoopProxy> task_runner =
52       content::BrowserThread::GetMessageLoopProxyForThread(
53           content::BrowserThread::UI);
54   image_decoder_->Start(task_runner);
55 }
56
57 void NotificationBitmapFetcher::OnURLFetchDownloadProgress(
58     const net::URLFetcher* source, int64 current, int64 total) {
59   // Do nothing here.
60 }
61
62 // Methods inherited from ImageDecoder::Delegate.
63
64 void NotificationBitmapFetcher::OnImageDecoded(
65     const ImageDecoder* decoder, const SkBitmap& decoded_image) {
66   // Make a copy of the bitmap which we pass back to the UI thread.
67   bitmap_.reset(new SkBitmap());
68   decoded_image.deepCopyTo(bitmap_.get(), decoded_image.getConfig());
69
70   // Report success.
71   delegate_->OnFetchComplete(url_, bitmap_.get());
72 }
73
74 void NotificationBitmapFetcher::OnDecodeImageFailed(
75     const ImageDecoder* decoder) {
76
77   // Report failure.
78   delegate_->OnFetchComplete(url_, NULL);
79 }
80
81 }  // namespace notifier