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, |
| 54 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner); |
52 | 55 |
53 // Asynchronously starts loading |image_url| using a Blink WebURLLoader. Must | 56 // Asynchronously starts loading |image_url| using a Blink WebURLLoader. Must |
54 // only be called on the main thread. | 57 // only be called on the main thread. |
55 void StartOnMainThread(int notification_id, const GURL& image_url); | 58 void StartOnMainThread(const GURL& image_url); |
56 | 59 |
57 // blink::WebURLLoaderClient implementation. | 60 // blink::WebURLLoaderClient implementation. |
58 void didReceiveData(blink::WebURLLoader* loader, | 61 void didReceiveData(blink::WebURLLoader* loader, |
59 const char* data, | 62 const char* data, |
60 int data_length, | 63 int data_length, |
61 int encoded_data_length) override; | 64 int encoded_data_length) override; |
62 void didFinishLoading(blink::WebURLLoader* loader, | 65 void didFinishLoading(blink::WebURLLoader* loader, |
63 double finish_time, | 66 double finish_time, |
64 int64_t total_encoded_data_length) override; | 67 int64_t total_encoded_data_length) override; |
65 void didFail(blink::WebURLLoader* loader, | 68 void didFail(blink::WebURLLoader* loader, |
(...skipping 15 matching lines...) Expand all Loading... |
81 // Returns a Skia bitmap, empty if buffer_ was empty or could not be decoded | 84 // Returns a Skia bitmap, empty if buffer_ was empty or could not be decoded |
82 // as an image, or a valid bitmap otherwise. | 85 // as an image, or a valid bitmap otherwise. |
83 SkBitmap GetDecodedImage() const; | 86 SkBitmap GetDecodedImage() const; |
84 | 87 |
85 // Ensures that we delete the image loader on the main thread. | 88 // Ensures that we delete the image loader on the main thread. |
86 void DeleteOnCorrectThread() const; | 89 void DeleteOnCorrectThread() const; |
87 | 90 |
88 ImageLoadCompletedCallback callback_; | 91 ImageLoadCompletedCallback callback_; |
89 | 92 |
90 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; | 93 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; |
91 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 94 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
92 | 95 |
93 int notification_id_; | |
94 bool completed_; | 96 bool completed_; |
95 | 97 |
96 scoped_ptr<blink::WebURLLoader> url_loader_; | 98 scoped_ptr<blink::WebURLLoader> url_loader_; |
97 | 99 |
98 std::vector<uint8_t> buffer_; | 100 std::vector<uint8_t> buffer_; |
99 | 101 |
100 DISALLOW_COPY_AND_ASSIGN(NotificationImageLoader); | 102 DISALLOW_COPY_AND_ASSIGN(NotificationImageLoader); |
101 }; | 103 }; |
102 | 104 |
103 struct NotificationImageLoaderDeleter { | 105 struct NotificationImageLoaderDeleter { |
104 static void Destruct(const NotificationImageLoader* context) { | 106 static void Destruct(const NotificationImageLoader* context) { |
105 context->DeleteOnCorrectThread(); | 107 context->DeleteOnCorrectThread(); |
106 } | 108 } |
107 }; | 109 }; |
108 | 110 |
109 } // namespace content | 111 } // namespace content |
110 | 112 |
111 #endif // CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ | 113 #endif // CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ |
OLD | NEW |