OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_sound_loader.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/thread_task_runner_handle.h" | 8 #include "base/thread_task_runner_handle.h" |
9 #include "content/child/child_thread_impl.h" | 9 #include "content/child/child_thread_impl.h" |
10 #include "content/child/image_decoder.h" | 10 #include "content/child/image_decoder.h" |
11 #include "third_party/WebKit/public/platform/Platform.h" | 11 #include "third_party/WebKit/public/platform/Platform.h" |
12 #include "third_party/WebKit/public/platform/WebURL.h" | 12 #include "third_party/WebKit/public/platform/WebURL.h" |
13 #include "third_party/WebKit/public/platform/WebURLLoader.h" | 13 #include "third_party/WebKit/public/platform/WebURLLoader.h" |
14 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 14 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
16 | 16 |
17 using blink::WebURL; | 17 using blink::WebURL; |
18 using blink::WebURLError; | 18 using blink::WebURLError; |
19 using blink::WebURLLoader; | 19 using blink::WebURLLoader; |
20 using blink::WebURLRequest; | 20 using blink::WebURLRequest; |
21 | 21 |
22 namespace content { | 22 namespace content { |
23 | 23 |
24 NotificationImageLoader::NotificationImageLoader( | 24 NotificationSoundLoader::NotificationSoundLoader( |
25 const ImageLoadCompletedCallback& callback, | 25 const SoundLoadCompletedCallback& callback, |
26 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner) | 26 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner) |
27 : callback_(callback), | 27 : callback_(callback), |
28 worker_task_runner_(worker_task_runner), | 28 worker_task_runner_(worker_task_runner), |
29 notification_id_(0), | 29 notification_id_(0), |
30 completed_(false) {} | 30 completed_(false) {} |
31 | 31 |
32 NotificationImageLoader::~NotificationImageLoader() { | 32 NotificationSoundLoader::~NotificationSoundLoader() { |
33 if (main_thread_task_runner_) | 33 if (main_thread_task_runner_) |
34 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | 34 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
35 } | 35 } |
36 | 36 |
37 void NotificationImageLoader::StartOnMainThread(int notification_id, | 37 void NotificationSoundLoader::StartOnMainThread(int notification_id, |
38 const GURL& image_url) { | 38 const GURL& image_url) { |
39 DCHECK(ChildThreadImpl::current()); | 39 DCHECK(ChildThreadImpl::current()); |
40 DCHECK(!url_loader_); | 40 DCHECK(!url_loader_); |
41 | 41 |
42 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 42 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
43 notification_id_ = notification_id; | 43 notification_id_ = notification_id; |
44 | 44 |
45 WebURL image_web_url(image_url); | 45 WebURL image_web_url(image_url); |
46 WebURLRequest request(image_web_url); | 46 WebURLRequest request(image_web_url); |
47 request.setRequestContext(WebURLRequest::RequestContextImage); | 47 request.setRequestContext(WebURLRequest::RequestContextImage); |
48 | 48 |
49 url_loader_.reset(blink::Platform::current()->createURLLoader()); | 49 url_loader_.reset(blink::Platform::current()->createURLLoader()); |
50 url_loader_->loadAsynchronously(request, this); | 50 url_loader_->loadAsynchronously(request, this); |
51 } | 51 } |
52 | 52 |
53 void NotificationImageLoader::didReceiveData( | 53 void NotificationSoundLoader::didReceiveData( |
54 WebURLLoader* loader, | 54 WebURLLoader* loader, |
55 const char* data, | 55 const char* data, |
56 int data_length, | 56 int data_length, |
57 int encoded_data_length) { | 57 int encoded_data_length) { |
58 DCHECK(!completed_); | 58 DCHECK(!completed_); |
59 DCHECK_GT(data_length, 0); | 59 DCHECK_GT(data_length, 0); |
60 | 60 |
61 buffer_.insert(buffer_.end(), data, data + data_length); | 61 buffer_.insert(buffer_.end(), data, data + data_length); |
62 } | 62 } |
63 | 63 |
64 void NotificationImageLoader::didFinishLoading( | 64 void NotificationSoundLoader::didFinishLoading( |
65 WebURLLoader* loader, | 65 WebURLLoader* loader, |
66 double finish_time, | 66 double finish_time, |
67 int64_t total_encoded_data_length) { | 67 int64_t total_encoded_data_length) { |
68 DCHECK(!completed_); | 68 DCHECK(!completed_); |
69 | 69 |
70 RunCallbackOnWorkerThread(); | 70 RunCallbackOnWorkerThread(); |
71 } | 71 } |
72 | 72 |
73 void NotificationImageLoader::didFail(WebURLLoader* loader, | 73 void NotificationSoundLoader::didFail(WebURLLoader* loader, |
74 const WebURLError& error) { | 74 const WebURLError& error) { |
75 if (completed_) | 75 if (completed_) |
76 return; | 76 return; |
77 | 77 |
78 RunCallbackOnWorkerThread(); | 78 RunCallbackOnWorkerThread(); |
79 } | 79 } |
80 | 80 |
81 void NotificationImageLoader::RunCallbackOnWorkerThread() { | 81 void NotificationSoundLoader::RunCallbackOnWorkerThread() { |
82 url_loader_.reset(); | 82 url_loader_.reset(); |
83 | 83 |
84 completed_ = true; | 84 completed_ = true; |
85 SkBitmap icon = GetDecodedImage(); | 85 SkBitmap icon = GetDecodedImage(); |
86 | 86 |
87 if (worker_task_runner_->BelongsToCurrentThread()) { | 87 if (worker_task_runner_->BelongsToCurrentThread()) { |
88 callback_.Run(notification_id_, icon); | 88 callback_.Run(notification_id_, icon); |
89 } else { | 89 } else { |
90 worker_task_runner_->PostTask( | 90 worker_task_runner_->PostTask( |
91 FROM_HERE, base::Bind(callback_, notification_id_, icon)); | 91 FROM_HERE, base::Bind(callback_, notification_id_, icon)); |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
95 SkBitmap NotificationImageLoader::GetDecodedImage() const { | 95 SkBitmap NotificationSoundLoader::GetDecodedImage() const { |
96 DCHECK(completed_); | 96 DCHECK(completed_); |
97 if (buffer_.empty()) | 97 if (buffer_.empty()) |
98 return SkBitmap(); | 98 return SkBitmap(); |
99 | 99 |
100 ImageDecoder decoder; | 100 ImageDecoder decoder; |
101 return decoder.Decode(&buffer_[0], buffer_.size()); | 101 return decoder.Decode(&buffer_[0], buffer_.size()); |
102 } | 102 } |
103 | 103 |
104 void NotificationImageLoader::DeleteOnCorrectThread() const { | 104 void NotificationSoundLoader::DeleteOnCorrectThread() const { |
105 if (!ChildThreadImpl::current()) { | 105 if (!ChildThreadImpl::current()) { |
106 main_thread_task_runner_->DeleteSoon(FROM_HERE, this); | 106 main_thread_task_runner_->DeleteSoon(FROM_HERE, this); |
107 return; | 107 return; |
108 } | 108 } |
109 | 109 |
110 delete this; | 110 delete this; |
111 } | 111 } |
112 | 112 |
113 } // namespace content | 113 } // namespace content |
OLD | NEW |