Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/resource_scheduling_filter.h" | 5 #include "content/child/resource_scheduling_filter.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "content/child/resource_dispatcher.h" | 9 #include "content/child/resource_dispatcher.h" |
| 10 #include "ipc/ipc_message.h" | 10 #include "ipc/ipc_message.h" |
| 11 #include "ipc/ipc_message_start.h" | 11 #include "ipc/ipc_message_start.h" |
| 12 #include "third_party/WebKit/public/platform/WebTaskRunner.h" | |
| 13 #include "third_party/WebKit/public/platform/WebTraceLocation.h" | |
| 12 | 14 |
| 13 namespace content { | 15 namespace content { |
| 14 | 16 |
| 17 namespace { | |
| 18 class DispatchMessageTask : public blink::WebTaskRunner::Task { | |
| 19 public: | |
| 20 DispatchMessageTask( | |
| 21 base::WeakPtr<ResourceSchedulingFilter> resource_scheduling_filter, | |
| 22 const IPC::Message& message) | |
| 23 : resource_scheduling_filter_(resource_scheduling_filter), | |
| 24 message_(message) {} | |
| 25 | |
| 26 void run() override { | |
| 27 if (!resource_scheduling_filter_.get()) | |
| 28 return; | |
| 29 resource_scheduling_filter_->DispatchMessage(message_); | |
| 30 } | |
| 31 | |
| 32 private: | |
| 33 base::WeakPtr<ResourceSchedulingFilter> resource_scheduling_filter_; | |
| 34 const IPC::Message message_; | |
| 35 }; | |
| 36 } // namespace | |
| 37 | |
| 15 ResourceSchedulingFilter::ResourceSchedulingFilter( | 38 ResourceSchedulingFilter::ResourceSchedulingFilter( |
| 16 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, | 39 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| 17 ResourceDispatcher* resource_dispatcher) | 40 ResourceDispatcher* resource_dispatcher) |
| 18 : main_thread_task_runner_(main_thread_task_runner), | 41 : main_thread_task_runner_(main_thread_task_runner), |
| 19 resource_dispatcher_(resource_dispatcher), | 42 resource_dispatcher_(resource_dispatcher), |
| 20 weak_ptr_factory_(this) { | 43 weak_ptr_factory_(this) { |
| 21 DCHECK(main_thread_task_runner_.get()); | 44 DCHECK(main_thread_task_runner_.get()); |
| 22 DCHECK(resource_dispatcher_); | 45 DCHECK(resource_dispatcher_); |
| 23 } | 46 } |
| 24 | 47 |
| 25 ResourceSchedulingFilter::~ResourceSchedulingFilter() { | 48 ResourceSchedulingFilter::~ResourceSchedulingFilter() { |
| 26 } | 49 } |
| 27 | 50 |
| 28 bool ResourceSchedulingFilter::OnMessageReceived(const IPC::Message& message) { | 51 bool ResourceSchedulingFilter::OnMessageReceived(const IPC::Message& message) { |
| 29 main_thread_task_runner_->PostTask( | 52 base::AutoLock lock(request_id_to_task_runner_map_lock_); |
| 30 FROM_HERE, base::Bind(&ResourceSchedulingFilter::DispatchMessage, | 53 int request_id; |
| 31 weak_ptr_factory_.GetWeakPtr(), message)); | 54 |
| 55 base::PickleIterator pickle_iterator(message); | |
| 56 if (!pickle_iterator.ReadInt(&request_id)) { | |
| 57 NOTREACHED() << "malformed resource message"; | |
| 58 return true; | |
| 59 } | |
| 60 // Dispatch the message on the request id specific task runner, if there is | |
| 61 // one, or on the general main_thread_task_runner if there isn't. | |
| 62 RequestIdToTaskRunnerMap::const_iterator iter = | |
| 63 request_id_to_task_runner_map_.find(request_id); | |
| 64 if (iter != request_id_to_task_runner_map_.end()) { | |
| 65 iter->second->postTask( | |
| 66 blink::WebTraceLocation(__FUNCTION__, __FILE__), | |
|
Sami
2015/09/29 11:22:46
Is this because we can't use the FROM_HERE from Bl
alex clarke (OOO till 29th)
2015/09/29 16:10:36
Done.
| |
| 67 new DispatchMessageTask(weak_ptr_factory_.GetWeakPtr(), message)); | |
| 68 } else { | |
| 69 main_thread_task_runner_->PostTask( | |
| 70 FROM_HERE, base::Bind(&ResourceSchedulingFilter::DispatchMessage, | |
| 71 weak_ptr_factory_.GetWeakPtr(), message)); | |
| 72 } | |
| 32 return true; | 73 return true; |
| 33 } | 74 } |
| 34 | 75 |
| 76 void ResourceSchedulingFilter::SetRequestIdTaskRunner( | |
| 77 int id, blink::WebTaskRunner* web_task_runner) { | |
| 78 base::AutoLock lock(request_id_to_task_runner_map_lock_); | |
| 79 request_id_to_task_runner_map_[id].reset(web_task_runner); | |
| 80 } | |
| 81 | |
| 82 void ResourceSchedulingFilter::ClearRequestIdTaskRunner(int id) { | |
| 83 base::AutoLock lock(request_id_to_task_runner_map_lock_); | |
| 84 request_id_to_task_runner_map_.erase(id); | |
| 85 } | |
| 86 | |
| 35 bool ResourceSchedulingFilter::GetSupportedMessageClasses( | 87 bool ResourceSchedulingFilter::GetSupportedMessageClasses( |
| 36 std::vector<uint32>* supported_message_classes) const { | 88 std::vector<uint32>* supported_message_classes) const { |
| 37 supported_message_classes->push_back(ResourceMsgStart); | 89 supported_message_classes->push_back(ResourceMsgStart); |
| 38 return true; | 90 return true; |
| 39 } | 91 } |
| 40 | 92 |
| 41 void ResourceSchedulingFilter::DispatchMessage(const IPC::Message& message) { | 93 void ResourceSchedulingFilter::DispatchMessage(const IPC::Message& message) { |
| 42 resource_dispatcher_->OnMessageReceived(message); | 94 resource_dispatcher_->OnMessageReceived(message); |
| 43 } | 95 } |
| 44 | 96 |
| 45 } // namespace content | 97 } // namespace content |
| OLD | NEW |