| 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" | |
| 14 | 12 |
| 15 namespace content { | 13 namespace content { |
| 16 | 14 |
| 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 | |
| 38 ResourceSchedulingFilter::ResourceSchedulingFilter( | 15 ResourceSchedulingFilter::ResourceSchedulingFilter( |
| 39 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, | 16 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| 40 ResourceDispatcher* resource_dispatcher) | 17 ResourceDispatcher* resource_dispatcher) |
| 41 : main_thread_task_runner_(main_thread_task_runner), | 18 : main_thread_task_runner_(main_thread_task_runner), |
| 42 resource_dispatcher_(resource_dispatcher), | 19 resource_dispatcher_(resource_dispatcher), |
| 43 weak_ptr_factory_(this) { | 20 weak_ptr_factory_(this) { |
| 44 DCHECK(main_thread_task_runner_.get()); | 21 DCHECK(main_thread_task_runner_.get()); |
| 45 DCHECK(resource_dispatcher_); | 22 DCHECK(resource_dispatcher_); |
| 46 } | 23 } |
| 47 | 24 |
| 48 ResourceSchedulingFilter::~ResourceSchedulingFilter() { | 25 ResourceSchedulingFilter::~ResourceSchedulingFilter() { |
| 49 } | 26 } |
| 50 | 27 |
| 51 bool ResourceSchedulingFilter::OnMessageReceived(const IPC::Message& message) { | 28 bool ResourceSchedulingFilter::OnMessageReceived(const IPC::Message& message) { |
| 52 base::AutoLock lock(request_id_to_task_runner_map_lock_); | 29 main_thread_task_runner_->PostTask( |
| 53 int request_id; | 30 FROM_HERE, base::Bind(&ResourceSchedulingFilter::DispatchMessage, |
| 54 | 31 weak_ptr_factory_.GetWeakPtr(), message)); |
| 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 // TODO(alexclarke): Find a way to let blink and chromium FROM_HERE coexist. | |
| 66 iter->second->postTask( | |
| 67 blink::WebTraceLocation(__FUNCTION__, __FILE__), | |
| 68 new DispatchMessageTask(weak_ptr_factory_.GetWeakPtr(), message)); | |
| 69 } else { | |
| 70 main_thread_task_runner_->PostTask( | |
| 71 FROM_HERE, base::Bind(&ResourceSchedulingFilter::DispatchMessage, | |
| 72 weak_ptr_factory_.GetWeakPtr(), message)); | |
| 73 } | |
| 74 return true; | 32 return true; |
| 75 } | 33 } |
| 76 | 34 |
| 77 void ResourceSchedulingFilter::SetRequestIdTaskRunner( | |
| 78 int id, scoped_ptr<blink::WebTaskRunner> web_task_runner) { | |
| 79 base::AutoLock lock(request_id_to_task_runner_map_lock_); | |
| 80 request_id_to_task_runner_map_.insert(id, web_task_runner.Pass()); | |
| 81 } | |
| 82 | |
| 83 void ResourceSchedulingFilter::ClearRequestIdTaskRunner(int id) { | |
| 84 base::AutoLock lock(request_id_to_task_runner_map_lock_); | |
| 85 request_id_to_task_runner_map_.erase(id); | |
| 86 } | |
| 87 | |
| 88 bool ResourceSchedulingFilter::GetSupportedMessageClasses( | 35 bool ResourceSchedulingFilter::GetSupportedMessageClasses( |
| 89 std::vector<uint32>* supported_message_classes) const { | 36 std::vector<uint32>* supported_message_classes) const { |
| 90 supported_message_classes->push_back(ResourceMsgStart); | 37 supported_message_classes->push_back(ResourceMsgStart); |
| 91 return true; | 38 return true; |
| 92 } | 39 } |
| 93 | 40 |
| 94 void ResourceSchedulingFilter::DispatchMessage(const IPC::Message& message) { | 41 void ResourceSchedulingFilter::DispatchMessage(const IPC::Message& message) { |
| 95 resource_dispatcher_->OnMessageReceived(message); | 42 resource_dispatcher_->OnMessageReceived(message); |
| 96 } | 43 } |
| 97 | 44 |
| 98 } // namespace content | 45 } // namespace content |
| OLD | NEW |