Add an UMA stat to be able to see if the User pods are show on start screen,
[chromium-blink-merge.git] / content / child / notifications / notification_image_loader.h
blob33a38f065a97cedc753a03209f8d0da17103367b
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.
5 #ifndef CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_
6 #define CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_
8 #include <vector>
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/sequenced_task_runner_helpers.h"
14 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
16 class GURL;
17 class SkBitmap;
19 namespace base {
20 class SingleThreadTaskRunner;
23 namespace blink {
24 class WebURL;
25 struct WebURLError;
26 class WebURLLoader;
29 namespace content {
31 struct NotificationImageLoaderDeleter;
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.
35 // Image downloaders must not be re-used for multiple notifications.
37 // All methods, except for the constructor, are expected to be used on the
38 // renderer main thread.
39 class NotificationImageLoader
40 : public blink::WebURLLoaderClient,
41 public base::RefCountedThreadSafe<NotificationImageLoader,
42 NotificationImageLoaderDeleter> {
43 using ImageLoadCompletedCallback = base::Callback<void(int, const SkBitmap&)>;
45 public:
46 NotificationImageLoader(
47 const ImageLoadCompletedCallback& callback,
48 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner);
50 // Asynchronously starts loading |image_url| using a Blink WebURLLoader. Must
51 // only be called on the main thread.
52 void StartOnMainThread(int notification_id, const GURL& image_url);
54 // blink::WebURLLoaderClient implementation.
55 virtual void didReceiveData(blink::WebURLLoader* loader,
56 const char* data,
57 int data_length,
58 int encoded_data_length);
59 virtual void didFinishLoading(blink::WebURLLoader* loader,
60 double finish_time,
61 int64_t total_encoded_data_length);
62 virtual void didFail(blink::WebURLLoader* loader,
63 const blink::WebURLError& error);
65 private:
66 friend class base::DeleteHelper<NotificationImageLoader>;
67 friend class base::RefCountedThreadSafe<NotificationImageLoader,
68 NotificationImageLoaderDeleter>;
69 friend struct NotificationImageLoaderDeleter;
71 virtual ~NotificationImageLoader();
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.
75 // For all other threads a task will be posted to the appropriate task runner.
76 void RunCallbackOnWorkerThread();
78 // Returns a Skia bitmap, empty if buffer_ was empty or could not be decoded
79 // as an image, or a valid bitmap otherwise.
80 SkBitmap GetDecodedImage() const;
82 // Ensures that we delete the image loader on the main thread.
83 void DeleteOnCorrectThread() const;
85 ImageLoadCompletedCallback callback_;
87 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_;
88 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
90 int notification_id_;
91 bool completed_;
93 scoped_ptr<blink::WebURLLoader> url_loader_;
95 std::vector<uint8_t> buffer_;
97 DISALLOW_COPY_AND_ASSIGN(NotificationImageLoader);
100 struct NotificationImageLoaderDeleter {
101 static void Destruct(const NotificationImageLoader* context) {
102 context->DeleteOnCorrectThread();
106 } // namespace content
108 #endif // CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_IMAGE_LOADER_H_