OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/client/session/blimp_client_session.h" | |
6 | |
7 #include <utility> | |
8 #include <vector> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/command_line.h" | |
12 #include "base/location.h" | |
13 #include "base/logging.h" | |
14 #include "base/memory/ptr_util.h" | |
15 #include "base/sequenced_task_runner.h" | |
16 #include "base/task_runner_util.h" | |
17 #include "base/threading/sequenced_task_runner_handle.h" | |
18 #include "base/threading/thread.h" | |
19 #include "base/threading/thread_task_runner_handle.h" | |
20 #include "blimp/client/core/contents/ime_feature.h" | |
21 #include "blimp/client/core/contents/navigation_feature.h" | |
22 #include "blimp/client/core/contents/tab_control_feature.h" | |
23 #include "blimp/client/core/geolocation/geolocation_feature.h" | |
24 #include "blimp/client/core/render_widget/render_widget_feature.h" | |
25 #include "blimp/client/core/session/client_network_components.h" | |
26 #include "blimp/client/core/session/cross_thread_network_event_observer.h" | |
27 #include "blimp/client/core/settings/settings_feature.h" | |
28 #include "blimp/client/core/switches/blimp_client_switches.h" | |
29 #include "blimp/common/blob_cache/in_memory_blob_cache.h" | |
30 #include "blimp/net/blimp_message_thread_pipe.h" | |
31 #include "blimp/net/blimp_stats.h" | |
32 #include "blimp/net/blob_channel/blob_channel_receiver.h" | |
33 #include "blimp/net/blob_channel/helium_blob_receiver_delegate.h" | |
34 #include "blimp/net/thread_pipe_manager.h" | |
35 #include "device/geolocation/geolocation_delegate.h" | |
36 #include "device/geolocation/location_arbitrator.h" | |
37 #include "url/gurl.h" | |
38 | |
39 namespace blimp { | |
40 namespace client { | |
41 namespace { | |
42 | |
43 void DropConnectionOnIOThread(ClientNetworkComponents* net_components) { | |
44 net_components->GetBrowserConnectionHandler()->DropCurrentConnection(); | |
45 } | |
46 | |
47 } // namespace | |
48 | |
49 BlimpClientSession::BlimpClientSession(const GURL& assigner_endpoint) | |
50 : io_thread_("BlimpIOThread"), | |
51 geolocation_feature_(base::MakeUnique<GeolocationFeature>( | |
52 base::MakeUnique<device::LocationArbitrator>( | |
53 base::MakeUnique<device::GeolocationDelegate>()))), | |
54 tab_control_feature_(new TabControlFeature), | |
55 navigation_feature_(new NavigationFeature), | |
56 ime_feature_(new ImeFeature), | |
57 render_widget_feature_(new RenderWidgetFeature), | |
58 settings_feature_(new SettingsFeature), | |
59 weak_factory_(this) { | |
60 base::Thread::Options options; | |
61 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
62 io_thread_.StartWithOptions(options); | |
63 net_components_.reset(new ClientNetworkComponents( | |
64 base::MakeUnique<CrossThreadNetworkEventObserver>( | |
65 weak_factory_.GetWeakPtr(), base::SequencedTaskRunnerHandle::Get()))); | |
66 | |
67 assignment_source_.reset(new AssignmentSource( | |
68 assigner_endpoint, io_thread_.task_runner(), io_thread_.task_runner())); | |
69 | |
70 std::unique_ptr<HeliumBlobReceiverDelegate> blob_delegate( | |
71 new HeliumBlobReceiverDelegate); | |
72 blob_delegate_ = blob_delegate.get(); | |
73 blob_receiver_ = BlobChannelReceiver::Create( | |
74 base::WrapUnique(new InMemoryBlobCache), std::move(blob_delegate)); | |
75 | |
76 RegisterFeatures(); | |
77 | |
78 blob_image_processor_.set_blob_receiver(blob_receiver_.get()); | |
79 blob_image_processor_.set_error_delegate(this); | |
80 | |
81 // Initialize must only be posted after the RegisterFeature calls have | |
82 // completed. | |
83 io_thread_.task_runner()->PostTask( | |
84 FROM_HERE, base::Bind(&ClientNetworkComponents::Initialize, | |
85 base::Unretained(net_components_.get()))); | |
86 } | |
87 | |
88 BlimpClientSession::~BlimpClientSession() { | |
89 io_thread_.task_runner()->DeleteSoon(FROM_HERE, net_components_.release()); | |
90 } | |
91 | |
92 void BlimpClientSession::Connect(const std::string& client_auth_token) { | |
93 VLOG(1) << "Trying to get assignment."; | |
94 assignment_source_->GetAssignment( | |
95 client_auth_token, base::Bind(&BlimpClientSession::ConnectWithAssignment, | |
96 weak_factory_.GetWeakPtr())); | |
97 } | |
98 | |
99 void BlimpClientSession::ConnectWithAssignment(AssignmentRequestResult result, | |
100 const Assignment& assignment) { | |
101 OnAssignmentConnectionAttempted(result, assignment); | |
102 | |
103 if (result != ASSIGNMENT_REQUEST_RESULT_OK) { | |
104 LOG(ERROR) << "Assignment failed, reason: " << result; | |
105 return; | |
106 } | |
107 | |
108 VLOG(1) << "Assignment succeeded"; | |
109 | |
110 io_thread_.task_runner()->PostTask( | |
111 FROM_HERE, | |
112 base::Bind(&ClientNetworkComponents::ConnectWithAssignment, | |
113 base::Unretained(net_components_.get()), assignment)); | |
114 } | |
115 | |
116 void BlimpClientSession::OnAssignmentConnectionAttempted( | |
117 AssignmentRequestResult result, | |
118 const Assignment& assignment) {} | |
119 | |
120 void BlimpClientSession::RegisterFeatures() { | |
121 thread_pipe_manager_ = base::MakeUnique<ThreadPipeManager>( | |
122 io_thread_.task_runner(), net_components_->GetBrowserConnectionHandler()); | |
123 | |
124 // Register features' message senders and receivers. | |
125 geolocation_feature_->set_outgoing_message_processor( | |
126 thread_pipe_manager_->RegisterFeature(BlimpMessage::kGeolocation, | |
127 geolocation_feature_.get())); | |
128 tab_control_feature_->set_outgoing_message_processor( | |
129 thread_pipe_manager_->RegisterFeature(BlimpMessage::kTabControl, | |
130 tab_control_feature_.get())); | |
131 navigation_feature_->set_outgoing_message_processor( | |
132 thread_pipe_manager_->RegisterFeature(BlimpMessage::kNavigation, | |
133 navigation_feature_.get())); | |
134 render_widget_feature_->set_outgoing_input_message_processor( | |
135 thread_pipe_manager_->RegisterFeature(BlimpMessage::kInput, | |
136 render_widget_feature_.get())); | |
137 render_widget_feature_->set_outgoing_compositor_message_processor( | |
138 thread_pipe_manager_->RegisterFeature(BlimpMessage::kCompositor, | |
139 render_widget_feature_.get())); | |
140 settings_feature_->set_outgoing_message_processor( | |
141 thread_pipe_manager_->RegisterFeature(BlimpMessage::kSettings, | |
142 settings_feature_.get())); | |
143 thread_pipe_manager_->RegisterFeature(BlimpMessage::kBlobChannel, | |
144 blob_delegate_); | |
145 | |
146 // Client will not send send any RenderWidget messages, so don't save the | |
147 // outgoing BlimpMessageProcessor in the RenderWidgetFeature. | |
148 thread_pipe_manager_->RegisterFeature(BlimpMessage::kRenderWidget, | |
149 render_widget_feature_.get()); | |
150 | |
151 ime_feature_->set_outgoing_message_processor( | |
152 thread_pipe_manager_->RegisterFeature(BlimpMessage::kIme, | |
153 ime_feature_.get())); | |
154 | |
155 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
156 switches::kDownloadWholeDocument)) | |
157 settings_feature_->SetRecordWholeDocument(true); | |
158 } | |
159 | |
160 void BlimpClientSession::DropConnection() { | |
161 io_thread_.task_runner()->PostTask( | |
162 FROM_HERE, base::Bind(&DropConnectionOnIOThread, net_components_.get())); | |
163 } | |
164 | |
165 void BlimpClientSession::OnConnected() {} | |
166 | |
167 void BlimpClientSession::OnDisconnected(int result) {} | |
168 | |
169 void BlimpClientSession::OnImageDecodeError() { | |
170 DropConnection(); | |
171 } | |
172 | |
173 TabControlFeature* BlimpClientSession::GetTabControlFeature() const { | |
174 return tab_control_feature_.get(); | |
175 } | |
176 | |
177 NavigationFeature* BlimpClientSession::GetNavigationFeature() const { | |
178 return navigation_feature_.get(); | |
179 } | |
180 | |
181 ImeFeature* BlimpClientSession::GetImeFeature() const { | |
182 return ime_feature_.get(); | |
183 } | |
184 | |
185 RenderWidgetFeature* BlimpClientSession::GetRenderWidgetFeature() const { | |
186 return render_widget_feature_.get(); | |
187 } | |
188 | |
189 SettingsFeature* BlimpClientSession::GetSettingsFeature() const { | |
190 return settings_feature_.get(); | |
191 } | |
192 | |
193 } // namespace client | |
194 } // namespace blimp | |
OLD | NEW |