Chromium Code Reviews| 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/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/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 | 16 |
| 16 using blink::WebURL; | 17 using blink::WebURL; |
| 17 using blink::WebURLError; | 18 using blink::WebURLError; |
| 18 using blink::WebURLLoader; | 19 using blink::WebURLLoader; |
| 19 using blink::WebURLRequest; | 20 using blink::WebURLRequest; |
| 20 | 21 |
| 21 namespace content { | 22 namespace content { |
| 22 | 23 |
| 23 NotificationImageLoader::NotificationImageLoader( | 24 NotificationImageLoader::NotificationImageLoader( |
| 24 const NotificationImageLoadedCallback& callback) | 25 const ImageLoadCompletedCallback& callback, |
| 26 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner) | |
| 25 : callback_(callback), | 27 : callback_(callback), |
| 28 worker_task_runner_(worker_task_runner), | |
| 29 notification_id_(0), | |
| 26 completed_(false) {} | 30 completed_(false) {} |
| 27 | 31 |
| 28 NotificationImageLoader::~NotificationImageLoader() { | 32 NotificationImageLoader::~NotificationImageLoader() { |
| 29 // The WebURLLoader instance must be destroyed on the same thread it was | 33 // The WebURLLoader instance must be destroyed on the same thread it was |
| 30 // created on, being the main thread. | 34 // created on, being the main thread. |
| 31 if (!main_thread_task_runner_->RunsTasksOnCurrentThread()) | 35 if (!main_thread_task_runner_->RunsTasksOnCurrentThread()) |
| 32 main_thread_task_runner_->DeleteSoon(FROM_HERE, url_loader_.release()); | 36 main_thread_task_runner_->DeleteSoon(FROM_HERE, url_loader_.release()); |
| 33 } | 37 } |
| 34 | 38 |
| 35 void NotificationImageLoader::StartOnMainThread( | 39 void NotificationImageLoader::StartOnMainThread(int notification_id, |
| 36 const WebURL& image_url, | 40 const GURL& image_url) { |
| 37 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner) { | |
| 38 DCHECK(ChildThreadImpl::current()); | 41 DCHECK(ChildThreadImpl::current()); |
| 39 DCHECK(!url_loader_); | 42 DCHECK(!url_loader_); |
| 40 DCHECK(worker_task_runner); | |
|
mlamouri (slow - plz ping)
2015/02/18 14:53:58
Would it make sense to have:
DCHECK(!worker_task_r
Peter Beverloo
2015/02/18 15:11:50
No, because it's possible for the worker_task_runn
| |
| 41 | 43 |
| 42 worker_task_runner_ = worker_task_runner; | |
| 43 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 44 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 45 notification_id_ = notification_id; | |
| 44 | 46 |
| 45 WebURLRequest request(image_url); | 47 WebURL imageUrl(image_url); |
|
mlamouri (slow - plz ping)
2015/02/18 14:53:58
nit: coding style.
Peter Beverloo
2015/02/18 15:11:50
Done.
| |
| 48 WebURLRequest request(imageUrl); | |
| 46 request.setRequestContext(WebURLRequest::RequestContextImage); | 49 request.setRequestContext(WebURLRequest::RequestContextImage); |
| 47 | 50 |
| 48 url_loader_.reset(blink::Platform::current()->createURLLoader()); | 51 url_loader_.reset(blink::Platform::current()->createURLLoader()); |
| 49 url_loader_->loadAsynchronously(request, this); | 52 url_loader_->loadAsynchronously(request, this); |
| 50 } | 53 } |
| 51 | 54 |
| 52 SkBitmap NotificationImageLoader::GetDecodedImage() const { | |
| 53 if (buffer_.empty()) | |
| 54 return SkBitmap(); | |
| 55 | |
| 56 ImageDecoder decoder; | |
| 57 return decoder.Decode(&buffer_[0], buffer_.size()); | |
| 58 } | |
| 59 | |
| 60 void NotificationImageLoader::didReceiveData( | 55 void NotificationImageLoader::didReceiveData( |
| 61 WebURLLoader* loader, | 56 WebURLLoader* loader, |
| 62 const char* data, | 57 const char* data, |
| 63 int data_length, | 58 int data_length, |
| 64 int encoded_data_length) { | 59 int encoded_data_length) { |
| 65 DCHECK(!completed_); | 60 DCHECK(!completed_); |
| 66 DCHECK_GT(data_length, 0); | 61 DCHECK_GT(data_length, 0); |
| 67 | 62 |
| 68 buffer_.insert(buffer_.end(), data, data + data_length); | 63 buffer_.insert(buffer_.end(), data, data + data_length); |
| 69 } | 64 } |
| 70 | 65 |
| 71 void NotificationImageLoader::didFinishLoading( | 66 void NotificationImageLoader::didFinishLoading( |
| 72 WebURLLoader* loader, | 67 WebURLLoader* loader, |
| 73 double finish_time, | 68 double finish_time, |
| 74 int64_t total_encoded_data_length) { | 69 int64_t total_encoded_data_length) { |
| 75 DCHECK(!completed_); | 70 DCHECK(!completed_); |
| 76 | 71 |
| 77 RunCallbackOnWorkerThread(); | 72 RunCallbackOnWorkerThread(); |
| 78 } | 73 } |
| 79 | 74 |
| 80 void NotificationImageLoader::didFail(WebURLLoader* loader, | 75 void NotificationImageLoader::didFail(WebURLLoader* loader, |
| 81 const WebURLError& error) { | 76 const WebURLError& error) { |
| 82 if (completed_) | 77 if (completed_) |
| 83 return; | 78 return; |
| 84 | 79 |
| 85 RunCallbackOnWorkerThread(); | 80 RunCallbackOnWorkerThread(); |
| 86 } | 81 } |
| 87 | 82 |
| 88 void NotificationImageLoader::RunCallbackOnWorkerThread() { | 83 void NotificationImageLoader::RunCallbackOnWorkerThread() { |
| 89 scoped_refptr<NotificationImageLoader> loader = make_scoped_refptr(this); | 84 SkBitmap icon; |
| 90 if (worker_task_runner_->BelongsToCurrentThread()) | 85 if (!buffer_.empty()) { |
| 91 callback_.Run(loader); | 86 ImageDecoder decoder; |
| 92 else | 87 icon = decoder.Decode(&buffer_[0], buffer_.size()); |
| 93 worker_task_runner_->PostTask(FROM_HERE, base::Bind(callback_, loader)); | 88 } |
|
mlamouri (slow - plz ping)
2015/02/18 14:53:58
What was wrong with GetDecodedImage? Maybe you cou
mlamouri (slow - plz ping)
2015/02/18 14:53:58
What was wrong with GetDecodedImage? Maybe you cou
Peter Beverloo
2015/02/18 15:11:50
Nothing. Restored.
| |
| 89 | |
| 90 url_loader_.reset(); | |
| 91 completed_ = true; | |
| 92 | |
| 93 if (worker_task_runner_->BelongsToCurrentThread()) { | |
| 94 callback_.Run(notification_id_, icon); | |
| 95 } else { | |
| 96 worker_task_runner_->PostTask( | |
| 97 FROM_HERE, base::Bind(callback_, notification_id_, icon)); | |
| 98 } | |
| 94 } | 99 } |
| 95 | 100 |
| 96 } // namespace content | 101 } // namespace content |
| OLD | NEW |