OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ | 5 #ifndef CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ |
6 #define CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ | 6 #define CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
(...skipping 15 matching lines...) Expand all Loading... |
26 namespace blink { | 26 namespace blink { |
27 class WebURL; | 27 class WebURL; |
28 struct WebURLError; | 28 struct WebURLError; |
29 class WebURLLoader; | 29 class WebURLLoader; |
30 } | 30 } |
31 | 31 |
32 namespace content { | 32 namespace content { |
33 | 33 |
34 struct NotificationImageLoaderDeleter; | 34 struct NotificationImageLoaderDeleter; |
35 | 35 |
| 36 // The |image| may be empty if the request failed or the image data could not be |
| 37 // decoded. |
| 38 using ImageLoadCompletedCallback = base::Callback<void(const SkBitmap& image)>; |
| 39 |
36 // Downloads the image associated with a notification and decodes the received | 40 // Downloads the image associated with a notification and decodes the received |
37 // image. This must be completed before notifications are shown to the user. | 41 // image. This must be completed before notifications are shown to the user. |
38 // Image downloaders must not be re-used for multiple notifications. | 42 // Image downloaders must not be re-used for multiple notifications. |
39 // | 43 // |
40 // All methods, except for the constructor, are expected to be used on the | 44 // All methods, except for the constructor, are expected to be used on the |
41 // renderer main thread. | 45 // renderer main thread. |
42 class NotificationImageLoader | 46 class NotificationImageLoader |
43 : public blink::WebURLLoaderClient, | 47 : public blink::WebURLLoaderClient, |
44 public base::RefCountedThreadSafe<NotificationImageLoader, | 48 public base::RefCountedThreadSafe<NotificationImageLoader, |
45 NotificationImageLoaderDeleter> { | 49 NotificationImageLoaderDeleter> { |
46 using ImageLoadCompletedCallback = base::Callback<void(int, const SkBitmap&)>; | |
47 | |
48 public: | 50 public: |
49 NotificationImageLoader( | 51 NotificationImageLoader( |
50 const ImageLoadCompletedCallback& callback, | 52 const ImageLoadCompletedCallback& callback, |
51 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner); | 53 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner); |
52 | 54 |
53 // Asynchronously starts loading |image_url| using a Blink WebURLLoader. Must | 55 // Asynchronously starts loading |image_url| using a Blink WebURLLoader. Must |
54 // only be called on the main thread. | 56 // only be called on the main thread. |
55 void StartOnMainThread(int notification_id, const GURL& image_url); | 57 void StartOnMainThread(const GURL& image_url); |
56 | 58 |
57 // blink::WebURLLoaderClient implementation. | 59 // blink::WebURLLoaderClient implementation. |
58 void didReceiveData(blink::WebURLLoader* loader, | 60 void didReceiveData(blink::WebURLLoader* loader, |
59 const char* data, | 61 const char* data, |
60 int data_length, | 62 int data_length, |
61 int encoded_data_length) override; | 63 int encoded_data_length) override; |
62 void didFinishLoading(blink::WebURLLoader* loader, | 64 void didFinishLoading(blink::WebURLLoader* loader, |
63 double finish_time, | 65 double finish_time, |
64 int64_t total_encoded_data_length) override; | 66 int64_t total_encoded_data_length) override; |
65 void didFail(blink::WebURLLoader* loader, | 67 void didFail(blink::WebURLLoader* loader, |
(...skipping 17 matching lines...) Expand all Loading... |
83 SkBitmap GetDecodedImage() const; | 85 SkBitmap GetDecodedImage() const; |
84 | 86 |
85 // Ensures that we delete the image loader on the main thread. | 87 // Ensures that we delete the image loader on the main thread. |
86 void DeleteOnCorrectThread() const; | 88 void DeleteOnCorrectThread() const; |
87 | 89 |
88 ImageLoadCompletedCallback callback_; | 90 ImageLoadCompletedCallback callback_; |
89 | 91 |
90 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; | 92 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; |
91 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 93 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
92 | 94 |
93 int notification_id_; | |
94 bool completed_; | 95 bool completed_; |
95 | 96 |
96 scoped_ptr<blink::WebURLLoader> url_loader_; | 97 scoped_ptr<blink::WebURLLoader> url_loader_; |
97 | 98 |
98 std::vector<uint8_t> buffer_; | 99 std::vector<uint8_t> buffer_; |
99 | 100 |
100 DISALLOW_COPY_AND_ASSIGN(NotificationImageLoader); | 101 DISALLOW_COPY_AND_ASSIGN(NotificationImageLoader); |
101 }; | 102 }; |
102 | 103 |
103 struct NotificationImageLoaderDeleter { | 104 struct NotificationImageLoaderDeleter { |
104 static void Destruct(const NotificationImageLoader* context) { | 105 static void Destruct(const NotificationImageLoader* context) { |
105 context->DeleteOnCorrectThread(); | 106 context->DeleteOnCorrectThread(); |
106 } | 107 } |
107 }; | 108 }; |
108 | 109 |
109 } // namespace content | 110 } // namespace content |
110 | 111 |
111 #endif // CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ | 112 #endif // CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ |
OLD | NEW |