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