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/threaded_data_provider.h" | 5 #include "content/child/threaded_data_provider.h" |
6 | 6 |
| 7 #include "base/location.h" |
| 8 #include "base/single_thread_task_runner.h" |
7 #include "components/scheduler/child/webthread_impl_for_worker_scheduler.h" | 9 #include "components/scheduler/child/webthread_impl_for_worker_scheduler.h" |
8 #include "content/child/child_process.h" | 10 #include "content/child/child_process.h" |
9 #include "content/child/child_thread_impl.h" | 11 #include "content/child/child_thread_impl.h" |
10 #include "content/child/resource_dispatcher.h" | 12 #include "content/child/resource_dispatcher.h" |
11 #include "content/child/thread_safe_sender.h" | 13 #include "content/child/thread_safe_sender.h" |
12 #include "content/common/resource_messages.h" | 14 #include "content/common/resource_messages.h" |
13 #include "ipc/ipc_sync_channel.h" | 15 #include "ipc/ipc_sync_channel.h" |
14 #include "third_party/WebKit/public/platform/WebThread.h" | 16 #include "third_party/WebKit/public/platform/WebThread.h" |
15 #include "third_party/WebKit/public/platform/WebThreadedDataReceiver.h" | 17 #include "third_party/WebKit/public/platform/WebThreadedDataReceiver.h" |
16 | 18 |
17 namespace content { | 19 namespace content { |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 class DataProviderMessageFilter : public IPC::MessageFilter { | 23 class DataProviderMessageFilter : public IPC::MessageFilter { |
22 public: | 24 public: |
23 DataProviderMessageFilter( | 25 DataProviderMessageFilter( |
24 const scoped_refptr<base::MessageLoopProxy>& io_message_loop, | 26 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
25 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, | 27 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, |
26 const scheduler::WebThreadImplForWorkerScheduler& background_thread, | 28 const scheduler::WebThreadImplForWorkerScheduler& background_thread, |
27 const base::WeakPtr<ThreadedDataProvider>& | 29 const base::WeakPtr<ThreadedDataProvider>& |
28 background_thread_resource_provider, | 30 background_thread_resource_provider, |
29 const base::WeakPtr<ThreadedDataProvider>& main_thread_resource_provider, | 31 const base::WeakPtr<ThreadedDataProvider>& main_thread_resource_provider, |
30 int request_id); | 32 int request_id); |
31 | 33 |
32 // IPC::ChannelProxy::MessageFilter | 34 // IPC::ChannelProxy::MessageFilter |
33 void OnFilterAdded(IPC::Sender* sender) final; | 35 void OnFilterAdded(IPC::Sender* sender) final; |
34 bool OnMessageReceived(const IPC::Message& message) final; | 36 bool OnMessageReceived(const IPC::Message& message) final; |
35 | 37 |
36 private: | 38 private: |
37 ~DataProviderMessageFilter() override {} | 39 ~DataProviderMessageFilter() override {} |
38 | 40 |
39 void OnReceivedData(int request_id, int data_offset, int data_length, | 41 void OnReceivedData(int request_id, int data_offset, int data_length, |
40 int encoded_data_length); | 42 int encoded_data_length); |
41 | 43 |
42 const scoped_refptr<base::MessageLoopProxy> io_message_loop_; | 44 const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
43 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 45 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
44 const scheduler::WebThreadImplForWorkerScheduler& background_thread_; | 46 const scheduler::WebThreadImplForWorkerScheduler& background_thread_; |
45 // This weakptr can only be dereferenced on the background thread. | 47 // This weakptr can only be dereferenced on the background thread. |
46 base::WeakPtr<ThreadedDataProvider> | 48 base::WeakPtr<ThreadedDataProvider> |
47 background_thread_resource_provider_; | 49 background_thread_resource_provider_; |
48 // This weakptr can only be dereferenced on the main thread. | 50 // This weakptr can only be dereferenced on the main thread. |
49 base::WeakPtr<ThreadedDataProvider> | 51 base::WeakPtr<ThreadedDataProvider> |
50 main_thread_resource_provider_; | 52 main_thread_resource_provider_; |
51 int request_id_; | 53 int request_id_; |
52 }; | 54 }; |
53 | 55 |
54 DataProviderMessageFilter::DataProviderMessageFilter( | 56 DataProviderMessageFilter::DataProviderMessageFilter( |
55 const scoped_refptr<base::MessageLoopProxy>& io_message_loop, | 57 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
56 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, | 58 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, |
57 const scheduler::WebThreadImplForWorkerScheduler& background_thread, | 59 const scheduler::WebThreadImplForWorkerScheduler& background_thread, |
58 const base::WeakPtr<ThreadedDataProvider>& | 60 const base::WeakPtr<ThreadedDataProvider>& |
59 background_thread_resource_provider, | 61 background_thread_resource_provider, |
60 const base::WeakPtr<ThreadedDataProvider>& main_thread_resource_provider, | 62 const base::WeakPtr<ThreadedDataProvider>& main_thread_resource_provider, |
61 int request_id) | 63 int request_id) |
62 : io_message_loop_(io_message_loop), | 64 : io_task_runner_(io_task_runner), |
63 main_thread_task_runner_(main_thread_task_runner), | 65 main_thread_task_runner_(main_thread_task_runner), |
64 background_thread_(background_thread), | 66 background_thread_(background_thread), |
65 background_thread_resource_provider_(background_thread_resource_provider), | 67 background_thread_resource_provider_(background_thread_resource_provider), |
66 main_thread_resource_provider_(main_thread_resource_provider), | 68 main_thread_resource_provider_(main_thread_resource_provider), |
67 request_id_(request_id) { | 69 request_id_(request_id) { |
68 DCHECK(main_thread_task_runner_.get()); | 70 DCHECK(main_thread_task_runner_.get()); |
69 } | 71 } |
70 | 72 |
71 void DataProviderMessageFilter::OnFilterAdded(IPC::Sender* sender) { | 73 void DataProviderMessageFilter::OnFilterAdded(IPC::Sender* sender) { |
72 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 74 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
73 | 75 |
74 main_thread_task_runner_->PostTask( | 76 main_thread_task_runner_->PostTask( |
75 FROM_HERE, | 77 FROM_HERE, |
76 base::Bind(&ThreadedDataProvider::OnResourceMessageFilterAddedMainThread, | 78 base::Bind(&ThreadedDataProvider::OnResourceMessageFilterAddedMainThread, |
77 main_thread_resource_provider_)); | 79 main_thread_resource_provider_)); |
78 } | 80 } |
79 | 81 |
80 bool DataProviderMessageFilter::OnMessageReceived( | 82 bool DataProviderMessageFilter::OnMessageReceived( |
81 const IPC::Message& message) { | 83 const IPC::Message& message) { |
82 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 84 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
83 | 85 |
84 if (message.type() != ResourceMsg_DataReceived::ID) | 86 if (message.type() != ResourceMsg_DataReceived::ID) |
85 return false; | 87 return false; |
86 | 88 |
87 int request_id; | 89 int request_id; |
88 | 90 |
89 PickleIterator iter(message); | 91 PickleIterator iter(message); |
90 if (!iter.ReadInt(&request_id)) { | 92 if (!iter.ReadInt(&request_id)) { |
91 NOTREACHED() << "malformed resource message"; | 93 NOTREACHED() << "malformed resource message"; |
92 return true; | 94 return true; |
93 } | 95 } |
94 | 96 |
95 if (request_id == request_id_) { | 97 if (request_id == request_id_) { |
96 ResourceMsg_DataReceived::Schema::Param arg; | 98 ResourceMsg_DataReceived::Schema::Param arg; |
97 if (ResourceMsg_DataReceived::Read(&message, &arg)) { | 99 if (ResourceMsg_DataReceived::Read(&message, &arg)) { |
98 OnReceivedData(get<0>(arg), get<1>(arg), get<2>(arg), get<3>(arg)); | 100 OnReceivedData(get<0>(arg), get<1>(arg), get<2>(arg), get<3>(arg)); |
99 return true; | 101 return true; |
100 } | 102 } |
101 } | 103 } |
102 | 104 |
103 return false; | 105 return false; |
104 } | 106 } |
105 | 107 |
106 void DataProviderMessageFilter::OnReceivedData(int request_id, | 108 void DataProviderMessageFilter::OnReceivedData(int request_id, |
107 int data_offset, | 109 int data_offset, |
108 int data_length, | 110 int data_length, |
109 int encoded_data_length) { | 111 int encoded_data_length) { |
110 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 112 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
111 background_thread_.TaskRunner()->PostTask( | 113 background_thread_.TaskRunner()->PostTask( |
112 FROM_HERE, | 114 FROM_HERE, |
113 base::Bind(&ThreadedDataProvider::OnReceivedDataOnBackgroundThread, | 115 base::Bind(&ThreadedDataProvider::OnReceivedDataOnBackgroundThread, |
114 background_thread_resource_provider_, data_offset, data_length, | 116 background_thread_resource_provider_, data_offset, data_length, |
115 encoded_data_length)); | 117 encoded_data_length)); |
116 } | 118 } |
117 | 119 |
118 } // anonymous namespace | 120 } // anonymous namespace |
119 | 121 |
120 ThreadedDataProvider::ThreadedDataProvider( | 122 ThreadedDataProvider::ThreadedDataProvider( |
(...skipping 15 matching lines...) Expand all Loading... |
136 main_thread_weak_factory_(this) { | 138 main_thread_weak_factory_(this) { |
137 DCHECK(ChildThreadImpl::current()); | 139 DCHECK(ChildThreadImpl::current()); |
138 DCHECK(ipc_channel_); | 140 DCHECK(ipc_channel_); |
139 DCHECK(threaded_data_receiver_); | 141 DCHECK(threaded_data_receiver_); |
140 DCHECK(main_thread_task_runner_.get()); | 142 DCHECK(main_thread_task_runner_.get()); |
141 | 143 |
142 background_thread_weak_factory_.reset( | 144 background_thread_weak_factory_.reset( |
143 new base::WeakPtrFactory<ThreadedDataProvider>(this)); | 145 new base::WeakPtrFactory<ThreadedDataProvider>(this)); |
144 | 146 |
145 filter_ = new DataProviderMessageFilter( | 147 filter_ = new DataProviderMessageFilter( |
146 ChildProcess::current()->io_message_loop_proxy(), | 148 ChildProcess::current()->io_task_runner(), main_thread_task_runner_, |
147 main_thread_task_runner_, background_thread_, | 149 background_thread_, background_thread_weak_factory_->GetWeakPtr(), |
148 background_thread_weak_factory_->GetWeakPtr(), | |
149 main_thread_weak_factory_.GetWeakPtr(), request_id); | 150 main_thread_weak_factory_.GetWeakPtr(), request_id); |
150 | 151 |
151 ChildThreadImpl::current()->channel()->AddFilter(filter_.get()); | 152 ChildThreadImpl::current()->channel()->AddFilter(filter_.get()); |
152 } | 153 } |
153 | 154 |
154 ThreadedDataProvider::~ThreadedDataProvider() { | 155 ThreadedDataProvider::~ThreadedDataProvider() { |
155 DCHECK(ChildThreadImpl::current()); | 156 DCHECK(ChildThreadImpl::current()); |
156 | 157 |
157 ChildThreadImpl::current()->channel()->RemoveFilter(filter_.get()); | 158 ChildThreadImpl::current()->channel()->RemoveFilter(filter_.get()); |
158 | 159 |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 DCHECK(threaded_data_receiver_->needsMainthreadDataCopy()); | 341 DCHECK(threaded_data_receiver_->needsMainthreadDataCopy()); |
341 DCHECK_EQ((size_t)data_length, data_copy->size()); | 342 DCHECK_EQ((size_t)data_length, data_copy->size()); |
342 } | 343 } |
343 | 344 |
344 threaded_data_receiver_->acceptMainthreadDataNotification( | 345 threaded_data_receiver_->acceptMainthreadDataNotification( |
345 (data_copy && !data_copy->empty()) ? &data_copy->front() : NULL, | 346 (data_copy && !data_copy->empty()) ? &data_copy->front() : NULL, |
346 data_length, encoded_data_length); | 347 data_length, encoded_data_length); |
347 } | 348 } |
348 | 349 |
349 } // namespace content | 350 } // namespace content |
OLD | NEW |