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 "chrome/common/chrome_notification_types.h" | |
8 #include "content/public/browser/browser_thread.h" | |
9 #include "content/public/browser/notification_details.h" | |
10 #include "content/public/browser/notification_service.h" | |
11 #include "net/url_request/url_fetcher.h" | |
12 | |
13 namespace notifier { | |
14 | |
15 NotificationBitmapFetcher::NotificationBitmapFetcher(GURL& url) | |
16 : url_(url), image_ready_(false), image_failed_(false) {} | |
17 | |
18 NotificationBitmapFetcher::~NotificationBitmapFetcher() {} | |
19 | |
20 bool NotificationBitmapFetcher::image_ready() { | |
21 return image_ready_; | |
22 } | |
23 | |
24 bool NotificationBitmapFetcher::image_failed() { | |
25 return image_failed_; | |
26 } | |
27 | |
28 SkBitmap* NotificationBitmapFetcher::bitmap() { | |
29 return bitmap_.get(); | |
30 } | |
31 | |
32 void NotificationBitmapFetcher::StartImageFetch() { | |
33 if (url_fetcher_ == NULL) | |
dcheng
2013/05/23 19:32:25
Why check for NULL here? Is it permissible to call
Pete Williamson
2013/05/24 22:18:06
This is to work with the SetURLFetcherForTest() fu
| |
34 url_fetcher_.reset( | |
35 net::URLFetcher::Create(url_, net::URLFetcher::GET, this)); | |
36 url_fetcher_->Start(); | |
37 } | |
38 | |
39 void NotificationBitmapFetcher::SetURLFetcherForTest( | |
40 scoped_ptr<net::URLFetcher>& url_fetcher) { | |
41 url_fetcher_ = url_fetcher.Pass(); | |
42 } | |
43 | |
44 void NotificationBitmapFetcher::HandleImageDecoded( | |
45 scoped_ptr<SkBitmap> bitmap) { | |
46 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
47 | |
48 bitmap_ = bitmap.Pass(); | |
49 image_ready_ = true; | |
50 | |
51 // Notify observers that the fetch is done. | |
52 content::NotificationService::current()->Notify( | |
53 chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, | |
54 content::NotificationService::AllSources(), | |
55 content::NotificationDetails()); | |
56 } | |
57 | |
58 void NotificationBitmapFetcher::HandleImageFailed() { | |
59 // Mark the image as failed so we don't keep waiting for it. | |
60 image_failed_ = true; | |
61 content::NotificationService::current()->Notify( | |
62 chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, | |
63 content::NotificationService::AllSources(), | |
64 content::NotificationDetails()); | |
65 } | |
66 | |
67 // Methods inherited from URLFetcherDelegate. | |
68 | |
69 void NotificationBitmapFetcher::OnURLFetchComplete( | |
70 const net::URLFetcher* source) { | |
71 std::string image_data; | |
72 | |
73 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
74 | |
75 // Copy the data into the string. Keep in mind it may have embedded nulls. | |
76 source->GetResponseAsString(&image_data); | |
77 | |
78 // Handle fetch failure. If it failed, set failed to true, and fire | |
79 // notification to listeners. | |
80 if (image_data.length() == 0) { | |
81 image_failed_ = true; | |
82 content::NotificationService::current()->Notify( | |
83 chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, | |
84 content::NotificationService::AllSources(), | |
85 content::NotificationDetails()); | |
86 } | |
87 | |
88 // Create an ImageDecoder with the data and assign it to the refptr. | |
89 image_decoder_ = new ImageDecoder(this, image_data, | |
90 ImageDecoder::DEFAULT_CODEC); | |
91 | |
92 // Call start to begin decoding. The ImageDecoder will call OnImageDecoded | |
93 // with the data when it is done. | |
94 // TODO(reviewers): what is the proper thread to use? I picked IO arbitrarily. | |
95 scoped_refptr<base::MessageLoopProxy> task_runner = | |
96 content::BrowserThread::GetMessageLoopProxyForThread( | |
97 content::BrowserThread::IO); | |
98 image_decoder_->Start(task_runner); | |
99 } | |
100 | |
101 void NotificationBitmapFetcher::OnURLFetchDownloadProgress( | |
102 const net::URLFetcher* source, int64 current, int64 total) { | |
103 // Do nothing here other than save the values for debugging. | |
104 DCHECK(source == url_fetcher_.get()); | |
dcheng
2013/05/23 19:32:25
DCHECK_EQ
Pete Williamson
2013/05/24 22:18:06
Removed the check
| |
105 progress_total_ = total; | |
106 progress_current_ = current; | |
dcheng
2013/05/23 19:32:25
It doesn't seem like anything uses these values ou
Pete Williamson
2013/05/24 22:18:06
Done.
| |
107 } | |
108 | |
109 // Methods inherited from ImageDecoder::Delegate. | |
110 | |
111 // This comes in on another thread, so we send the bitmap back to the UI | |
112 // thread so that the object stays thread safe. | |
113 void NotificationBitmapFetcher::OnImageDecoded( | |
114 const ImageDecoder* decoder, const SkBitmap& decoded_image) { | |
115 // Make a copy of the bitmap which we pass back to the UI thread. | |
116 scoped_ptr<SkBitmap> bitmap(new SkBitmap()); | |
117 decoded_image.deepCopyTo(bitmap.get(), decoded_image.getConfig()); | |
118 | |
119 // Post to the UI thread. | |
120 content::BrowserThread::PostTask( | |
121 content::BrowserThread::UI, FROM_HERE, | |
122 base::Bind(&NotificationBitmapFetcher::HandleImageDecoded, this, | |
123 base::Passed(&bitmap))); | |
124 } | |
125 | |
126 void NotificationBitmapFetcher::OnDecodeImageFailed( | |
127 const ImageDecoder* decoder) { | |
128 | |
129 // Post to the UI thread. | |
130 content::BrowserThread::PostTask( | |
131 content::BrowserThread::UI, FROM_HERE, | |
132 base::Bind(&NotificationBitmapFetcher::HandleImageFailed, this)); | |
133 } | |
134 | |
135 } // namespace notifier | |
OLD | NEW |