| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_SOUND_LOADER_H_ |
| 6 #define CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ | 6 #define CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_SOUND_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 "base/sequenced_task_runner_helpers.h" |
| 14 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" | 14 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" |
| 15 | 15 |
| 16 class GURL; | 16 class GURL; |
| 17 class SkBitmap; | 17 class SkBitmap; |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 class SingleThreadTaskRunner; | 20 class SingleThreadTaskRunner; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace blink { | 23 namespace blink { |
| 24 class WebURL; | 24 class WebURL; |
| 25 struct WebURLError; | 25 struct WebURLError; |
| 26 class WebURLLoader; | 26 class WebURLLoader; |
| 27 } | 27 } |
| 28 | 28 |
| 29 namespace content { | 29 namespace content { |
| 30 | 30 |
| 31 struct NotificationImageLoaderDeleter; | 31 struct NotificationSoundLoaderDeleter; |
| 32 | 32 |
| 33 // Downloads the image associated with a notification and decodes the received | 33 // Downloads the image associated with a notification and decodes the received |
| 34 // 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. |
| 35 // Image downloaders must not be re-used for multiple notifications. | 35 // Image downloaders must not be re-used for multiple notifications. |
| 36 // | 36 // |
| 37 // All methods, except for the constructor, are expected to be used on the | 37 // All methods, except for the constructor, are expected to be used on the |
| 38 // renderer main thread. | 38 // renderer main thread. |
| 39 class NotificationImageLoader | 39 class NotificationSoundLoader |
| 40 : public blink::WebURLLoaderClient, | 40 : public blink::WebURLLoaderClient, |
| 41 public base::RefCountedThreadSafe<NotificationImageLoader, | 41 public base::RefCountedThreadSafe<NotificationSoundLoader, |
| 42 NotificationImageLoaderDeleter> { | 42 NotificationSoundLoaderDeleter> { |
| 43 using ImageLoadCompletedCallback = base::Callback<void(int, const SkBitmap&)>; | 43 using SoundLoadCompletedCallback = base::Callback<void(int, const SkBitmap&)>; |
| 44 | 44 |
| 45 public: | 45 public: |
| 46 NotificationImageLoader( | 46 NotificationSoundLoader( |
| 47 const ImageLoadCompletedCallback& callback, | 47 const SoundLoadCompletedCallback& callback, |
| 48 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner); | 48 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner); |
| 49 | 49 |
| 50 // Asynchronously starts loading |image_url| using a Blink WebURLLoader. Must | 50 // Asynchronously starts loading |image_url| using a Blink WebURLLoader. Must |
| 51 // only be called on the main thread. | 51 // only be called on the main thread. |
| 52 void StartOnMainThread(int notification_id, const GURL& image_url); | 52 void StartOnMainThread(int notification_id, const GURL& image_url); |
| 53 | 53 |
| 54 // blink::WebURLLoaderClient implementation. | 54 // blink::WebURLLoaderClient implementation. |
| 55 virtual void didReceiveData(blink::WebURLLoader* loader, | 55 virtual void didReceiveData(blink::WebURLLoader* loader, |
| 56 const char* data, | 56 const char* data, |
| 57 int data_length, | 57 int data_length, |
| 58 int encoded_data_length); | 58 int encoded_data_length); |
| 59 virtual void didFinishLoading(blink::WebURLLoader* loader, | 59 virtual void didFinishLoading(blink::WebURLLoader* loader, |
| 60 double finish_time, | 60 double finish_time, |
| 61 int64_t total_encoded_data_length); | 61 int64_t total_encoded_data_length); |
| 62 virtual void didFail(blink::WebURLLoader* loader, | 62 virtual void didFail(blink::WebURLLoader* loader, |
| 63 const blink::WebURLError& error); | 63 const blink::WebURLError& error); |
| 64 | 64 |
| 65 private: | 65 private: |
| 66 friend class base::DeleteHelper<NotificationImageLoader>; | 66 friend class base::DeleteHelper<NotificationSoundLoader>; |
| 67 friend class base::RefCountedThreadSafe<NotificationImageLoader, | 67 friend class base::RefCountedThreadSafe<NotificationSoundLoader, |
| 68 NotificationImageLoaderDeleter>; | 68 NotificationSoundLoaderDeleter>; |
| 69 friend struct NotificationImageLoaderDeleter; | 69 friend struct NotificationSoundLoaderDeleter; |
| 70 | 70 |
| 71 virtual ~NotificationImageLoader(); | 71 virtual ~NotificationSoundLoader(); |
| 72 | 72 |
| 73 // 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 |
| 74 // 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. |
| 75 // 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. |
| 76 void RunCallbackOnWorkerThread(); | 76 void RunCallbackOnWorkerThread(); |
| 77 | 77 |
| 78 // Returns a Skia bitmap, empty if buffer_ was empty or could not be decoded | 78 // Returns a Skia bitmap, empty if buffer_ was empty or could not be decoded |
| 79 // as an image, or a valid bitmap otherwise. | 79 // as an image, or a valid bitmap otherwise. |
| 80 SkBitmap GetDecodedImage() const; | 80 SkBitmap GetDecodedImage() const; |
| 81 | 81 |
| 82 // Ensures that we delete the image loader on the main thread. | 82 // Ensures that we delete the image loader on the main thread. |
| 83 void DeleteOnCorrectThread() const; | 83 void DeleteOnCorrectThread() const; |
| 84 | 84 |
| 85 ImageLoadCompletedCallback callback_; | 85 SoundLoadCompletedCallback callback_; |
| 86 | 86 |
| 87 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; | 87 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; |
| 88 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 88 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
| 89 | 89 |
| 90 int notification_id_; | 90 int notification_id_; |
| 91 bool completed_; | 91 bool completed_; |
| 92 | 92 |
| 93 scoped_ptr<blink::WebURLLoader> url_loader_; | 93 scoped_ptr<blink::WebURLLoader> url_loader_; |
| 94 | 94 |
| 95 std::vector<uint8_t> buffer_; | 95 std::vector<uint8_t> buffer_; |
| 96 | 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(NotificationImageLoader); | 97 DISALLOW_COPY_AND_ASSIGN(NotificationSoundLoader); |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 struct NotificationImageLoaderDeleter { | 100 struct NotificationSoundLoaderDeleter { |
| 101 static void Destruct(const NotificationImageLoader* context) { | 101 static void Destruct(const NotificationSoundLoader* context) { |
| 102 context->DeleteOnCorrectThread(); | 102 context->DeleteOnCorrectThread(); |
| 103 } | 103 } |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 } // namespace content | 106 } // namespace content |
| 107 | 107 |
| 108 #endif // CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_ | 108 #endif // CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_SOUND_LOADER_H_ |
| OLD | NEW |