OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 "content/public/browser/browser_thread.h" | |
8 #include "net/url_request/url_fetcher.h" | |
9 | |
10 namespace notifier { | |
11 | |
12 NotificationBitmapFetcher::NotificationBitmapFetcher( | |
13 const GURL& url, | |
14 NotificationBitmapFetcherDelegate* delegate) | |
15 : url_(url), delegate_(delegate) {} | |
16 | |
17 NotificationBitmapFetcher::~NotificationBitmapFetcher() {} | |
18 | |
19 void NotificationBitmapFetcher::StartImageFetch() { | |
20 if (url_fetcher_ == NULL) | |
21 url_fetcher_.reset( | |
22 net::URLFetcher::Create(url_, net::URLFetcher::GET, this)); | |
23 url_fetcher_->Start(); | |
24 } | |
25 | |
26 void NotificationBitmapFetcher::SetURLFetcherForTest( | |
27 scoped_ptr<net::URLFetcher>& url_fetcher) { | |
28 url_fetcher_ = url_fetcher.Pass(); | |
29 } | |
30 | |
31 // Methods inherited from URLFetcherDelegate. | |
32 | |
33 void NotificationBitmapFetcher::OnURLFetchComplete( | |
34 const net::URLFetcher* source) { | |
35 std::string image_data; | |
36 | |
37 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
38 | |
39 // Copy the data into the string. Keep in mind it may have embedded nulls. | |
40 source->GetResponseAsString(&image_data); | |
41 | |
42 // Handle fetch failure. If it failed, set failed to true, and fire | |
43 // notification to listeners. | |
44 if (image_data.length() == 0) { | |
45 OnDecodeImageFailed(NULL); | |
dcheng
2013/05/29 20:40:29
Return?
Pete Williamson
2013/05/30 00:13:18
Good catch! Done.
| |
46 } | |
47 | |
48 // Create an ImageDecoder with the data and assign it to the refptr. | |
49 image_decoder_ = new ImageDecoder(this, image_data, | |
50 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 NotificationBitmapFetcher::OnURLFetchDownloadProgress( | |
61 const net::URLFetcher* source, int64 current, int64 total) { | |
62 // Do nothing here. | |
63 } | |
64 | |
65 // Methods inherited from ImageDecoder::Delegate. | |
66 | |
67 // This comes in on another thread, so we send the bitmap back to the UI | |
dcheng
2013/05/29 20:40:29
No longer accurate.
Pete Williamson
2013/05/30 00:13:18
Done.
| |
68 // thread so that the object stays thread safe. | |
69 void NotificationBitmapFetcher::OnImageDecoded( | |
70 const ImageDecoder* decoder, const SkBitmap& decoded_image) { | |
71 // Make a copy of the bitmap which we pass back to the UI thread. | |
72 bitmap_.reset(new SkBitmap()); | |
73 decoded_image.deepCopyTo(bitmap_.get(), decoded_image.getConfig()); | |
74 | |
75 // Report success. | |
76 delegate_->OnFetchComplete(url_, bitmap_.get()); | |
77 } | |
78 | |
79 void NotificationBitmapFetcher::OnDecodeImageFailed( | |
80 const ImageDecoder* decoder) { | |
81 | |
82 // Report failure. | |
83 delegate_->OnFetchComplete(url_, NULL); | |
84 } | |
85 | |
86 } // namespace notifier | |
OLD | NEW |