1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "blimp/client/session/blimp_client_session.h"
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "blimp/client/app/blimp_client_switches.h"
#include "blimp/client/feature/navigation_feature.h"
#include "blimp/client/feature/render_widget_feature.h"
#include "blimp/client/feature/tab_control_feature.h"
#include "blimp/net/blimp_message_processor.h"
#include "blimp/net/blimp_message_thread_pipe.h"
#include "blimp/net/browser_connection_handler.h"
#include "blimp/net/client_connection_manager.h"
#include "blimp/net/common.h"
#include "blimp/net/null_blimp_message_processor.h"
#include "blimp/net/tcp_client_transport.h"
#include "net/base/address_list.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
namespace blimp {
namespace client {
// This class's functions and destruction are all invoked on the IO thread by
// the BlimpClientSession.
// TODO(haibinlu): crbug/574884
class ClientNetworkComponents {
public:
// Can be created on any thread.
ClientNetworkComponents() {}
~ClientNetworkComponents() {}
// Sets up network components.
void Initialize();
// Starts the connection to the engine using the given |assignment|.
// It is required to first call Initialize.
void ConnectWithAssignment(const Assignment& assignment);
// Invoked by BlimpEngineSession to finish feature registration on IO thread:
// using |incoming_proxy| as the incoming message processor, and connecting
// |outgoing_pipe| to the actual message sender.
void RegisterFeature(BlimpMessage::Type type,
scoped_ptr<BlimpMessageThreadPipe> outgoing_pipe,
scoped_ptr<BlimpMessageProcessor> incoming_proxy);
private:
scoped_ptr<BrowserConnectionHandler> browser_connection_handler_;
scoped_ptr<ClientConnectionManager> connection_manager_;
// Container for the feature-specific MessageProcessors.
std::vector<scoped_ptr<BlimpMessageProcessor>> incoming_proxies_;
// Containers for the MessageProcessors used to write feature-specific
// messages to the network, and the thread-pipe endpoints through which
// they are used from the UI thread.
std::vector<scoped_ptr<BlimpMessageThreadPipe>> outgoing_pipes_;
std::vector<scoped_ptr<BlimpMessageProcessor>> outgoing_message_processors_;
DISALLOW_COPY_AND_ASSIGN(ClientNetworkComponents);
};
void ClientNetworkComponents::Initialize() {
DCHECK(!connection_manager_);
connection_manager_ = make_scoped_ptr(
new ClientConnectionManager(browser_connection_handler_.get()));
}
void ClientNetworkComponents::ConnectWithAssignment(
const Assignment& assignment) {
DCHECK(connection_manager_);
connection_manager_->set_client_token(assignment.client_token);
connection_manager_->AddTransport(make_scoped_ptr(new TCPClientTransport(
net::AddressList(assignment.ip_endpoint), nullptr)));
connection_manager_->Connect();
}
void ClientNetworkComponents::RegisterFeature(
BlimpMessage::Type type,
scoped_ptr<BlimpMessageThreadPipe> outgoing_pipe,
scoped_ptr<BlimpMessageProcessor> incoming_proxy) {
if (!browser_connection_handler_) {
browser_connection_handler_ = make_scoped_ptr(new BrowserConnectionHandler);
}
// Registers |incoming_proxy| as the message processor for incoming
// messages with |type|. Sets the returned outgoing message processor as the
// actual sender of the |outgoing_pipe|.
scoped_ptr<BlimpMessageProcessor> outgoing_message_processor =
browser_connection_handler_->RegisterFeature(type, incoming_proxy.get());
outgoing_pipe->set_target_processor(outgoing_message_processor.get());
incoming_proxies_.push_back(std::move(incoming_proxy));
outgoing_pipes_.push_back(std::move(outgoing_pipe));
outgoing_message_processors_.push_back(std::move(outgoing_message_processor));
}
BlimpClientSession::BlimpClientSession(
scoped_ptr<AssignmentSource> assignment_source)
: assignment_source_(std::move(assignment_source)),
io_thread_("BlimpIOThread"),
tab_control_feature_(new TabControlFeature),
navigation_feature_(new NavigationFeature),
render_widget_feature_(new RenderWidgetFeature),
net_components_(new ClientNetworkComponents),
weak_factory_(this) {
base::Thread::Options options;
options.message_loop_type = base::MessageLoop::TYPE_IO;
io_thread_.StartWithOptions(options);
// Register features' message senders and receivers.
tab_control_feature_->set_outgoing_message_processor(
RegisterFeature(BlimpMessage::TAB_CONTROL, tab_control_feature_.get()));
navigation_feature_->set_outgoing_message_processor(
RegisterFeature(BlimpMessage::NAVIGATION, navigation_feature_.get()));
render_widget_feature_->set_outgoing_input_message_processor(
RegisterFeature(BlimpMessage::INPUT, render_widget_feature_.get()));
render_widget_feature_->set_outgoing_compositor_message_processor(
RegisterFeature(BlimpMessage::COMPOSITOR, render_widget_feature_.get()));
// We don't expect to send any RenderWidget messages, so don't save the
// outgoing BlimpMessageProcessor in the RenderWidgetFeature.
RegisterFeature(BlimpMessage::RENDER_WIDGET, render_widget_feature_.get());
// Initialize must only be posted after the RegisterFeature calls have
// completed.
io_thread_.task_runner()->PostTask(
FROM_HERE, base::Bind(&ClientNetworkComponents::Initialize,
base::Unretained(net_components_.get())));
}
BlimpClientSession::~BlimpClientSession() {
io_thread_.task_runner()->DeleteSoon(FROM_HERE, net_components_.release());
}
void BlimpClientSession::Connect() {
assignment_source_->GetAssignment(base::Bind(
&BlimpClientSession::ConnectWithAssignment, weak_factory_.GetWeakPtr()));
}
void BlimpClientSession::ConnectWithAssignment(const Assignment& assignment) {
io_thread_.task_runner()->PostTask(
FROM_HERE,
base::Bind(&ClientNetworkComponents::ConnectWithAssignment,
base::Unretained(net_components_.get()), assignment));
}
scoped_ptr<BlimpMessageProcessor> BlimpClientSession::RegisterFeature(
BlimpMessage::Type type,
BlimpMessageProcessor* incoming_processor) {
// Creates an outgoing pipe and a proxy for forwarding messages
// from features on the UI thread to network components on the IO thread.
scoped_ptr<BlimpMessageThreadPipe> outgoing_pipe(
new BlimpMessageThreadPipe(io_thread_.task_runner()));
scoped_ptr<BlimpMessageProcessor> outgoing_message_proxy =
outgoing_pipe->CreateProxy();
// Creates an incoming pipe and a proxy for receiving messages
// from network components on the IO thread.
scoped_ptr<BlimpMessageThreadPipe> incoming_pipe(
new BlimpMessageThreadPipe(base::SequencedTaskRunnerHandle::Get()));
incoming_pipe->set_target_processor(incoming_processor);
scoped_ptr<BlimpMessageProcessor> incoming_proxy =
incoming_pipe->CreateProxy();
// Finishes registration on IO thread.
io_thread_.task_runner()->PostTask(
FROM_HERE, base::Bind(&ClientNetworkComponents::RegisterFeature,
base::Unretained(net_components_.get()), type,
base::Passed(std::move(outgoing_pipe)),
base::Passed(std::move(incoming_proxy))));
incoming_pipes_.push_back(std::move(incoming_pipe));
return outgoing_message_proxy;
}
TabControlFeature* BlimpClientSession::GetTabControlFeature() const {
return tab_control_feature_.get();
}
NavigationFeature* BlimpClientSession::GetNavigationFeature() const {
return navigation_feature_.get();
}
RenderWidgetFeature* BlimpClientSession::GetRenderWidgetFeature() const {
return render_widget_feature_.get();
}
} // namespace client
} // namespace blimp
|