| 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 if (main_thread_task_runner_) |
| 30 // created on, being the main thread. | 34 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 31 if (!main_thread_task_runner_->RunsTasksOnCurrentThread()) | |
| 32 main_thread_task_runner_->DeleteSoon(FROM_HERE, url_loader_.release()); | |
| 33 } | 35 } |
| 34 | 36 |
| 35 void NotificationImageLoader::StartOnMainThread( | 37 void NotificationImageLoader::StartOnMainThread(int notification_id, |
| 36 const WebURL& image_url, | 38 const GURL& image_url) { |
| 37 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner) { | |
| 38 DCHECK(ChildThreadImpl::current()); | 39 DCHECK(ChildThreadImpl::current()); |
| 39 DCHECK(!url_loader_); | 40 DCHECK(!url_loader_); |
| 40 DCHECK(worker_task_runner); | |
| 41 | 41 |
| 42 worker_task_runner_ = worker_task_runner; | |
| 43 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 42 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 43 notification_id_ = notification_id; |
| 44 | 44 |
| 45 WebURLRequest request(image_url); | 45 WebURL image_web_url(image_url); |
| 46 WebURLRequest request(image_web_url); |
| 46 request.setRequestContext(WebURLRequest::RequestContextImage); | 47 request.setRequestContext(WebURLRequest::RequestContextImage); |
| 47 | 48 |
| 48 url_loader_.reset(blink::Platform::current()->createURLLoader()); | 49 url_loader_.reset(blink::Platform::current()->createURLLoader()); |
| 49 url_loader_->loadAsynchronously(request, this); | 50 url_loader_->loadAsynchronously(request, this); |
| 50 } | 51 } |
| 51 | 52 |
| 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( | 53 void NotificationImageLoader::didReceiveData( |
| 61 WebURLLoader* loader, | 54 WebURLLoader* loader, |
| 62 const char* data, | 55 const char* data, |
| 63 int data_length, | 56 int data_length, |
| 64 int encoded_data_length) { | 57 int encoded_data_length) { |
| 65 DCHECK(!completed_); | 58 DCHECK(!completed_); |
| 66 DCHECK_GT(data_length, 0); | 59 DCHECK_GT(data_length, 0); |
| 67 | 60 |
| 68 buffer_.insert(buffer_.end(), data, data + data_length); | 61 buffer_.insert(buffer_.end(), data, data + data_length); |
| 69 } | 62 } |
| 70 | 63 |
| 71 void NotificationImageLoader::didFinishLoading( | 64 void NotificationImageLoader::didFinishLoading( |
| 72 WebURLLoader* loader, | 65 WebURLLoader* loader, |
| 73 double finish_time, | 66 double finish_time, |
| 74 int64_t total_encoded_data_length) { | 67 int64_t total_encoded_data_length) { |
| 75 DCHECK(!completed_); | 68 DCHECK(!completed_); |
| 76 | 69 |
| 77 RunCallbackOnWorkerThread(); | 70 RunCallbackOnWorkerThread(); |
| 78 } | 71 } |
| 79 | 72 |
| 80 void NotificationImageLoader::didFail(WebURLLoader* loader, | 73 void NotificationImageLoader::didFail(WebURLLoader* loader, |
| 81 const WebURLError& error) { | 74 const WebURLError& error) { |
| 82 if (completed_) | 75 if (completed_) |
| 83 return; | 76 return; |
| 84 | 77 |
| 85 RunCallbackOnWorkerThread(); | 78 RunCallbackOnWorkerThread(); |
| 86 } | 79 } |
| 87 | 80 |
| 88 void NotificationImageLoader::RunCallbackOnWorkerThread() { | 81 void NotificationImageLoader::RunCallbackOnWorkerThread() { |
| 89 scoped_refptr<NotificationImageLoader> loader = make_scoped_refptr(this); | 82 url_loader_.reset(); |
| 90 if (worker_task_runner_->BelongsToCurrentThread()) | 83 |
| 91 callback_.Run(loader); | 84 completed_ = true; |
| 92 else | 85 SkBitmap icon = GetDecodedImage(); |
| 93 worker_task_runner_->PostTask(FROM_HERE, base::Bind(callback_, loader)); | 86 |
| 87 if (worker_task_runner_->BelongsToCurrentThread()) { |
| 88 callback_.Run(notification_id_, icon); |
| 89 } else { |
| 90 worker_task_runner_->PostTask( |
| 91 FROM_HERE, base::Bind(callback_, notification_id_, icon)); |
| 92 } |
| 93 } |
| 94 |
| 95 SkBitmap NotificationImageLoader::GetDecodedImage() const { |
| 96 DCHECK(completed_); |
| 97 if (buffer_.empty()) |
| 98 return SkBitmap(); |
| 99 |
| 100 ImageDecoder decoder; |
| 101 return decoder.Decode(&buffer_[0], buffer_.size()); |
| 102 } |
| 103 |
| 104 void NotificationImageLoader::DeleteOnCorrectThread() const { |
| 105 if (!ChildThreadImpl::current()) { |
| 106 main_thread_task_runner_->DeleteSoon(FROM_HERE, this); |
| 107 return; |
| 108 } |
| 109 |
| 110 delete this; |
| 94 } | 111 } |
| 95 | 112 |
| 96 } // namespace content | 113 } // namespace content |
| OLD | NEW |