| 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/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 NotificationImageLoader::NotificationImageLoader( |
| 25 const ImageLoadCompletedCallback& callback, | 25 const ImageLoadCompletedCallback& 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), | |
| 30 completed_(false) {} | 29 completed_(false) {} |
| 31 | 30 |
| 32 NotificationImageLoader::~NotificationImageLoader() { | 31 NotificationImageLoader::~NotificationImageLoader() { |
| 33 if (main_thread_task_runner_) | 32 if (main_thread_task_runner_) |
| 34 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | 33 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 35 } | 34 } |
| 36 | 35 |
| 37 void NotificationImageLoader::StartOnMainThread(int notification_id, | 36 void NotificationImageLoader::StartOnMainThread(const GURL& image_url) { |
| 38 const GURL& image_url) { | |
| 39 DCHECK(ChildThreadImpl::current()); | 37 DCHECK(ChildThreadImpl::current()); |
| 40 DCHECK(!url_loader_); | 38 DCHECK(!url_loader_); |
| 41 | 39 |
| 42 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 40 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 43 notification_id_ = notification_id; | |
| 44 | 41 |
| 45 WebURL image_web_url(image_url); | 42 WebURL image_web_url(image_url); |
| 46 WebURLRequest request(image_web_url); | 43 WebURLRequest request(image_web_url); |
| 47 request.setRequestContext(WebURLRequest::RequestContextImage); | 44 request.setRequestContext(WebURLRequest::RequestContextImage); |
| 48 | 45 |
| 49 url_loader_.reset(blink::Platform::current()->createURLLoader()); | 46 url_loader_.reset(blink::Platform::current()->createURLLoader()); |
| 50 url_loader_->loadAsynchronously(request, this); | 47 url_loader_->loadAsynchronously(request, this); |
| 51 } | 48 } |
| 52 | 49 |
| 53 void NotificationImageLoader::didReceiveData(WebURLLoader* loader, | 50 void NotificationImageLoader::didReceiveData(WebURLLoader* loader, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 77 RunCallbackOnWorkerThread(); | 74 RunCallbackOnWorkerThread(); |
| 78 } | 75 } |
| 79 | 76 |
| 80 void NotificationImageLoader::RunCallbackOnWorkerThread() { | 77 void NotificationImageLoader::RunCallbackOnWorkerThread() { |
| 81 url_loader_.reset(); | 78 url_loader_.reset(); |
| 82 | 79 |
| 83 completed_ = true; | 80 completed_ = true; |
| 84 SkBitmap icon = GetDecodedImage(); | 81 SkBitmap icon = GetDecodedImage(); |
| 85 | 82 |
| 86 if (worker_task_runner_->BelongsToCurrentThread()) { | 83 if (worker_task_runner_->BelongsToCurrentThread()) { |
| 87 callback_.Run(notification_id_, icon); | 84 callback_.Run(icon); |
| 88 } else { | 85 } else { |
| 89 worker_task_runner_->PostTask( | 86 worker_task_runner_->PostTask(FROM_HERE, base::Bind(callback_, icon)); |
| 90 FROM_HERE, base::Bind(callback_, notification_id_, icon)); | |
| 91 } | 87 } |
| 92 } | 88 } |
| 93 | 89 |
| 94 SkBitmap NotificationImageLoader::GetDecodedImage() const { | 90 SkBitmap NotificationImageLoader::GetDecodedImage() const { |
| 95 DCHECK(completed_); | 91 DCHECK(completed_); |
| 96 if (buffer_.empty()) | 92 if (buffer_.empty()) |
| 97 return SkBitmap(); | 93 return SkBitmap(); |
| 98 | 94 |
| 99 ImageDecoder decoder; | 95 ImageDecoder decoder; |
| 100 return decoder.Decode(&buffer_[0], buffer_.size()); | 96 return decoder.Decode(&buffer_[0], buffer_.size()); |
| 101 } | 97 } |
| 102 | 98 |
| 103 void NotificationImageLoader::DeleteOnCorrectThread() const { | 99 void NotificationImageLoader::DeleteOnCorrectThread() const { |
| 104 if (!ChildThreadImpl::current()) { | 100 if (!ChildThreadImpl::current()) { |
| 105 main_thread_task_runner_->DeleteSoon(FROM_HERE, this); | 101 main_thread_task_runner_->DeleteSoon(FROM_HERE, this); |
| 106 return; | 102 return; |
| 107 } | 103 } |
| 108 | 104 |
| 109 delete this; | 105 delete this; |
| 110 } | 106 } |
| 111 | 107 |
| 112 } // namespace content | 108 } // namespace content |
| OLD | NEW |