OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "content/child/notifications/notification_image_loader.h" | 5 #include "content/child/notifications/notification_image_loader.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/single_thread_task_runner.h" |
8 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
9 #include "content/child/child_thread_impl.h" | |
10 #include "content/child/image_decoder.h" | 12 #include "content/child/image_decoder.h" |
11 #include "third_party/WebKit/public/platform/Platform.h" | 13 #include "third_party/WebKit/public/platform/Platform.h" |
12 #include "third_party/WebKit/public/platform/WebURL.h" | 14 #include "third_party/WebKit/public/platform/WebURL.h" |
13 #include "third_party/WebKit/public/platform/WebURLLoader.h" | 15 #include "third_party/WebKit/public/platform/WebURLLoader.h" |
14 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 16 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
16 | 18 |
17 using blink::WebURL; | 19 using blink::WebURL; |
18 using blink::WebURLError; | 20 using blink::WebURLError; |
19 using blink::WebURLLoader; | 21 using blink::WebURLLoader; |
20 using blink::WebURLRequest; | 22 using blink::WebURLRequest; |
21 | 23 |
22 namespace content { | 24 namespace content { |
23 | 25 |
24 NotificationImageLoader::NotificationImageLoader( | 26 NotificationImageLoader::NotificationImageLoader( |
25 const ImageLoadCompletedCallback& callback, | 27 const ImageLoadCompletedCallback& callback, |
26 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner) | 28 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner) |
27 : callback_(callback), | 29 : callback_(callback), |
28 worker_task_runner_(worker_task_runner), | 30 worker_task_runner_(worker_task_runner), |
29 notification_id_(0), | |
30 completed_(false) {} | 31 completed_(false) {} |
31 | 32 |
32 NotificationImageLoader::~NotificationImageLoader() { | 33 NotificationImageLoader::~NotificationImageLoader() { |
33 if (main_thread_task_runner_) | 34 if (main_thread_task_runner_) |
34 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | 35 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
35 } | 36 } |
36 | 37 |
37 void NotificationImageLoader::StartOnMainThread(int notification_id, | 38 void NotificationImageLoader::StartOnMainThread(const GURL& image_url) { |
38 const GURL& image_url) { | |
39 DCHECK(ChildThreadImpl::current()); | |
40 DCHECK(!url_loader_); | 39 DCHECK(!url_loader_); |
41 | 40 |
42 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 41 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
43 notification_id_ = notification_id; | |
44 | 42 |
45 WebURL image_web_url(image_url); | 43 WebURL image_web_url(image_url); |
46 WebURLRequest request(image_web_url); | 44 WebURLRequest request(image_web_url); |
47 request.setRequestContext(WebURLRequest::RequestContextImage); | 45 request.setRequestContext(WebURLRequest::RequestContextImage); |
48 | 46 |
49 url_loader_.reset(blink::Platform::current()->createURLLoader()); | 47 url_loader_.reset(blink::Platform::current()->createURLLoader()); |
50 url_loader_->loadAsynchronously(request, this); | 48 url_loader_->loadAsynchronously(request, this); |
51 } | 49 } |
52 | 50 |
53 void NotificationImageLoader::didReceiveData(WebURLLoader* loader, | 51 void NotificationImageLoader::didReceiveData(WebURLLoader* loader, |
(...skipping 23 matching lines...) Expand all Loading... |
77 RunCallbackOnWorkerThread(); | 75 RunCallbackOnWorkerThread(); |
78 } | 76 } |
79 | 77 |
80 void NotificationImageLoader::RunCallbackOnWorkerThread() { | 78 void NotificationImageLoader::RunCallbackOnWorkerThread() { |
81 url_loader_.reset(); | 79 url_loader_.reset(); |
82 | 80 |
83 completed_ = true; | 81 completed_ = true; |
84 SkBitmap icon = GetDecodedImage(); | 82 SkBitmap icon = GetDecodedImage(); |
85 | 83 |
86 if (worker_task_runner_->BelongsToCurrentThread()) { | 84 if (worker_task_runner_->BelongsToCurrentThread()) { |
87 callback_.Run(notification_id_, icon); | 85 callback_.Run(icon); |
88 } else { | 86 } else { |
89 worker_task_runner_->PostTask( | 87 worker_task_runner_->PostTask(FROM_HERE, base::Bind(callback_, icon)); |
90 FROM_HERE, base::Bind(callback_, notification_id_, icon)); | |
91 } | 88 } |
92 } | 89 } |
93 | 90 |
94 SkBitmap NotificationImageLoader::GetDecodedImage() const { | 91 SkBitmap NotificationImageLoader::GetDecodedImage() const { |
95 DCHECK(completed_); | 92 DCHECK(completed_); |
96 if (buffer_.empty()) | 93 if (buffer_.empty()) |
97 return SkBitmap(); | 94 return SkBitmap(); |
98 | 95 |
99 ImageDecoder decoder; | 96 ImageDecoder decoder; |
100 return decoder.Decode(&buffer_[0], buffer_.size()); | 97 return decoder.Decode(&buffer_[0], buffer_.size()); |
101 } | 98 } |
102 | 99 |
103 void NotificationImageLoader::DeleteOnCorrectThread() const { | 100 void NotificationImageLoader::DeleteOnCorrectThread() const { |
104 if (!ChildThreadImpl::current()) { | 101 if (main_thread_task_runner_ && |
| 102 !main_thread_task_runner_->BelongsToCurrentThread()) { |
105 main_thread_task_runner_->DeleteSoon(FROM_HERE, this); | 103 main_thread_task_runner_->DeleteSoon(FROM_HERE, this); |
106 return; | 104 return; |
107 } | 105 } |
108 | 106 |
109 delete this; | 107 delete this; |
110 } | 108 } |
111 | 109 |
112 } // namespace content | 110 } // namespace content |
OLD | NEW |