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