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