| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 NotificationImageLoader_h | 5 #ifndef NotificationImageLoader_h |
| 6 #define NotificationImageLoader_h | 6 #define NotificationImageLoader_h |
| 7 | 7 |
| 8 #include "core/loader/ThreadableLoader.h" | 8 #include "core/loader/ThreadableLoader.h" |
| 9 #include "core/loader/ThreadableLoaderClient.h" | 9 #include "core/loader/ThreadableLoaderClient.h" |
| 10 #include "modules/ModulesExport.h" | 10 #include "modules/ModulesExport.h" |
| 11 #include "platform/SharedBuffer.h" | 11 #include "platform/SharedBuffer.h" |
| 12 #include "platform/heap/Handle.h" | 12 #include "platform/heap/Handle.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 #include "wtf/Functional.h" | 14 #include "wtf/Functional.h" |
| 14 #include "wtf/RefPtr.h" | 15 #include "wtf/RefPtr.h" |
| 15 #include <memory> | 16 #include <memory> |
| 16 | 17 |
| 17 class SkBitmap; | |
| 18 | |
| 19 namespace blink { | 18 namespace blink { |
| 20 | 19 |
| 21 class ExecutionContext; | 20 class ExecutionContext; |
| 22 class KURL; | 21 class KURL; |
| 23 class ResourceError; | 22 class ResourceError; |
| 24 | 23 |
| 25 // Asynchronously downloads an image when given a url, decodes the loaded data, | 24 // Asynchronously downloads an image when given a url, decodes the loaded data, |
| 26 // and passes the bitmap to the given callback. | 25 // and passes the bitmap to the given callback. |
| 27 class MODULES_EXPORT NotificationImageLoader final | 26 class MODULES_EXPORT NotificationImageLoader final |
| 28 : public GarbageCollectedFinalized<NotificationImageLoader>, | 27 : public GarbageCollectedFinalized<NotificationImageLoader>, |
| 29 public ThreadableLoaderClient { | 28 public ThreadableLoaderClient { |
| 30 public: | 29 public: |
| 30 // Type names are used in UMAs, so do not rename. |
| 31 enum class Type { Image, Icon, Badge, ActionIcon }; |
| 32 |
| 31 // The bitmap may be empty if the request failed or the image data could not | 33 // The bitmap may be empty if the request failed or the image data could not |
| 32 // be decoded. | 34 // be decoded. |
| 33 using ImageCallback = Function<void(const SkBitmap&)>; | 35 using ImageCallback = Function<void(const SkBitmap&)>; |
| 34 | 36 |
| 35 NotificationImageLoader(); | 37 explicit NotificationImageLoader(Type); |
| 36 ~NotificationImageLoader() override; | 38 ~NotificationImageLoader() override; |
| 37 | 39 |
| 40 // Scales down |image| according to its type and returns result. If it is |
| 41 // already small enough, |image| is returned unchanged. |
| 42 static SkBitmap scaleDownIfNeeded(const SkBitmap& image, Type); |
| 43 |
| 38 // Asynchronously downloads an image from the given url, decodes the loaded | 44 // Asynchronously downloads an image from the given url, decodes the loaded |
| 39 // data, and passes the bitmap to the callback. Times out if the load takes | 45 // data, and passes the bitmap to the callback. Times out if the load takes |
| 40 // too long and ImageCallback is invoked with an empty bitmap. | 46 // too long and ImageCallback is invoked with an empty bitmap. |
| 41 void start(ExecutionContext*, const KURL&, std::unique_ptr<ImageCallback>); | 47 void start(ExecutionContext*, const KURL&, std::unique_ptr<ImageCallback>); |
| 42 | 48 |
| 43 // Cancels the pending load, if there is one. The |m_imageCallback| will not | 49 // Cancels the pending load, if there is one. The |m_imageCallback| will not |
| 44 // be run. | 50 // be run. |
| 45 void stop(); | 51 void stop(); |
| 46 | 52 |
| 47 // ThreadableLoaderClient interface. | 53 // ThreadableLoaderClient interface. |
| 48 void didReceiveData(const char* data, unsigned length) override; | 54 void didReceiveData(const char* data, unsigned length) override; |
| 49 void didFinishLoading(unsigned long resourceIdentifier, | 55 void didFinishLoading(unsigned long resourceIdentifier, |
| 50 double finishTime) override; | 56 double finishTime) override; |
| 51 void didFail(const ResourceError&) override; | 57 void didFail(const ResourceError&) override; |
| 52 void didFailRedirectCheck() override; | 58 void didFailRedirectCheck() override; |
| 53 | 59 |
| 54 DEFINE_INLINE_TRACE() { visitor->trace(m_threadableLoader); } | 60 DEFINE_INLINE_TRACE() { visitor->trace(m_threadableLoader); } |
| 55 | 61 |
| 56 private: | 62 private: |
| 57 void runCallbackWithEmptyBitmap(); | 63 void runCallbackWithEmptyBitmap(); |
| 58 | 64 |
| 65 Type m_type; |
| 59 bool m_stopped; | 66 bool m_stopped; |
| 60 double m_startTime; | 67 double m_startTime; |
| 61 RefPtr<SharedBuffer> m_data; | 68 RefPtr<SharedBuffer> m_data; |
| 62 std::unique_ptr<ImageCallback> m_imageCallback; | 69 std::unique_ptr<ImageCallback> m_imageCallback; |
| 63 Member<ThreadableLoader> m_threadableLoader; | 70 Member<ThreadableLoader> m_threadableLoader; |
| 64 }; | 71 }; |
| 65 | 72 |
| 66 } // namespace blink | 73 } // namespace blink |
| 67 | 74 |
| 68 #endif // NotificationImageLoader_h | 75 #endif // NotificationImageLoader_h |
| OLD | NEW |