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 <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/sequenced_task_runner_helpers.h" |
13 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" | 14 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" |
14 | 15 |
| 16 class GURL; |
15 class SkBitmap; | 17 class SkBitmap; |
16 | 18 |
17 namespace base { | 19 namespace base { |
18 class SingleThreadTaskRunner; | 20 class SingleThreadTaskRunner; |
19 } | 21 } |
20 | 22 |
21 namespace blink { | 23 namespace blink { |
22 class WebURL; | 24 class WebURL; |
23 struct WebURLError; | 25 struct WebURLError; |
24 class WebURLLoader; | 26 class WebURLLoader; |
25 } | 27 } |
26 | 28 |
27 namespace content { | 29 namespace content { |
28 | 30 |
29 class NotificationImageLoader; | 31 struct NotificationImageLoaderDeleter; |
30 | |
31 // Callback to be invoked when an image load has been completed. | |
32 using NotificationImageLoadedCallback = | |
33 base::Callback<void(scoped_refptr<NotificationImageLoader>)>; | |
34 | 32 |
35 // Downloads the image associated with a notification and decodes the received | 33 // Downloads the image associated with a notification and decodes the received |
36 // image. This must be completed before notifications are shown to the user. | 34 // image. This must be completed before notifications are shown to the user. |
37 // Image downloaders must not be re-used for multiple notifications. The image | 35 // Image downloaders must not be re-used for multiple notifications. |
38 // loader must be started from the main thread, but will invoke the callback on | 36 // |
39 // the thread identified in the StartOnMainThread call. | 37 // All methods, except for the constructor, are expected to be used on the |
| 38 // renderer main thread. |
40 class NotificationImageLoader | 39 class NotificationImageLoader |
41 : public blink::WebURLLoaderClient, | 40 : public blink::WebURLLoaderClient, |
42 public base::RefCountedThreadSafe<NotificationImageLoader> { | 41 public base::RefCountedThreadSafe<NotificationImageLoader, |
| 42 NotificationImageLoaderDeleter> { |
| 43 using ImageLoadCompletedCallback = base::Callback<void(int, const SkBitmap&)>; |
| 44 |
43 public: | 45 public: |
44 explicit NotificationImageLoader( | 46 NotificationImageLoader( |
45 const NotificationImageLoadedCallback& callback); | 47 const ImageLoadCompletedCallback& callback, |
46 | |
47 // Asynchronously starts loading |image_url|. | |
48 // Must be called on the main thread. The callback should be executed by | |
49 // |worker_task_runner|. | |
50 void StartOnMainThread( | |
51 const blink::WebURL& image_url, | |
52 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner); | 48 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner); |
53 | 49 |
54 // Returns the SkBitmap resulting from decoding the loaded buffer. | 50 // Asynchronously starts loading |image_url| using a Blink WebURLLoader. Must |
55 SkBitmap GetDecodedImage() const; | 51 // only be called on the main thread. |
| 52 void StartOnMainThread(int notification_id, const GURL& image_url); |
56 | 53 |
57 // blink::WebURLLoaderClient implementation. | 54 // blink::WebURLLoaderClient implementation. |
58 virtual void didReceiveData(blink::WebURLLoader* loader, | 55 virtual void didReceiveData(blink::WebURLLoader* loader, |
59 const char* data, | 56 const char* data, |
60 int data_length, | 57 int data_length, |
61 int encoded_data_length); | 58 int encoded_data_length); |
62 virtual void didFinishLoading(blink::WebURLLoader* loader, | 59 virtual void didFinishLoading(blink::WebURLLoader* loader, |
63 double finish_time, | 60 double finish_time, |
64 int64_t total_encoded_data_length); | 61 int64_t total_encoded_data_length); |
65 virtual void didFail(blink::WebURLLoader* loader, | 62 virtual void didFail(blink::WebURLLoader* loader, |
66 const blink::WebURLError& error); | 63 const blink::WebURLError& error); |
67 | 64 |
68 private: | 65 private: |
69 friend class base::RefCountedThreadSafe<NotificationImageLoader>; | 66 friend class base::DeleteHelper<NotificationImageLoader>; |
| 67 friend class base::RefCountedThreadSafe<NotificationImageLoader, |
| 68 NotificationImageLoaderDeleter>; |
| 69 friend struct NotificationImageLoaderDeleter; |
| 70 |
70 virtual ~NotificationImageLoader(); | 71 virtual ~NotificationImageLoader(); |
71 | 72 |
72 // Invokes the callback on the thread this image loader was started for. When | 73 // Invokes the callback on the thread this image loader was started for. When |
73 // the thread id is zero (the main document), it will be executed immediately. | 74 // the thread id is zero (the main document), it will be executed immediately. |
74 // For all other threads a task will be posted to the appropriate task runner. | 75 // For all other threads a task will be posted to the appropriate task runner. |
75 void RunCallbackOnWorkerThread(); | 76 void RunCallbackOnWorkerThread(); |
76 | 77 |
77 NotificationImageLoadedCallback callback_; | 78 // Returns a Skia bitmap, empty if buffer_ was empty or could not be decoded |
| 79 // as an image, or a valid bitmap otherwise. |
| 80 SkBitmap GetDecodedImage() const; |
| 81 |
| 82 // Ensures that we delete the image loader on the main thread. |
| 83 void DeleteOnCorrectThread() const; |
| 84 |
| 85 ImageLoadCompletedCallback callback_; |
| 86 |
| 87 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; |
| 88 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
| 89 |
| 90 int notification_id_; |
| 91 bool completed_; |
78 | 92 |
79 scoped_ptr<blink::WebURLLoader> url_loader_; | 93 scoped_ptr<blink::WebURLLoader> url_loader_; |
80 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; | |
81 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | |
82 bool completed_; | |
83 | 94 |
84 std::vector<uint8_t> buffer_; | 95 std::vector<uint8_t> buffer_; |
85 | 96 |
86 DISALLOW_COPY_AND_ASSIGN(NotificationImageLoader); | 97 DISALLOW_COPY_AND_ASSIGN(NotificationImageLoader); |
87 }; | 98 }; |
88 | 99 |
| 100 struct NotificationImageLoaderDeleter { |
| 101 static void Destruct(const NotificationImageLoader* context) { |
| 102 context->DeleteOnCorrectThread(); |
| 103 } |
| 104 }; |
| 105 |
89 } // namespace content | 106 } // namespace content |
90 | 107 |
91 #endif // CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ | 108 #endif // CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ |
OLD | NEW |