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 // 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 } |
32 return true; | 74 return true; |
33 } | 75 } |
34 | 76 |
| 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 |
35 bool ResourceSchedulingFilter::GetSupportedMessageClasses( | 88 bool ResourceSchedulingFilter::GetSupportedMessageClasses( |
36 std::vector<uint32>* supported_message_classes) const { | 89 std::vector<uint32>* supported_message_classes) const { |
37 supported_message_classes->push_back(ResourceMsgStart); | 90 supported_message_classes->push_back(ResourceMsgStart); |
38 return true; | 91 return true; |
39 } | 92 } |
40 | 93 |
41 void ResourceSchedulingFilter::DispatchMessage(const IPC::Message& message) { | 94 void ResourceSchedulingFilter::DispatchMessage(const IPC::Message& message) { |
42 resource_dispatcher_->OnMessageReceived(message); | 95 resource_dispatcher_->OnMessageReceived(message); |
43 } | 96 } |
44 | 97 |
45 } // namespace content | 98 } // namespace content |
OLD | NEW |