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