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