| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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/core/context/blimp_client_context_impl.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/metrics/histogram_macros.h" | |
| 14 #include "base/threading/sequenced_task_runner_handle.h" | |
| 15 #include "blimp/client/core/compositor/blimp_compositor_dependencies.h" | |
| 16 #include "blimp/client/core/compositor/blob_channel_feature.h" | |
| 17 #include "blimp/client/core/contents/blimp_contents_impl.h" | |
| 18 #include "blimp/client/core/contents/blimp_contents_manager.h" | |
| 19 #include "blimp/client/core/contents/ime_feature.h" | |
| 20 #include "blimp/client/core/contents/navigation_feature.h" | |
| 21 #include "blimp/client/core/contents/tab_control_feature.h" | |
| 22 #include "blimp/client/core/feedback/blimp_feedback_data.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/cross_thread_network_event_observer.h" | |
| 26 #include "blimp/client/core/settings/settings.h" | |
| 27 #include "blimp/client/core/settings/settings_feature.h" | |
| 28 #include "blimp/client/core/switches/blimp_client_switches.h" | |
| 29 #include "blimp/client/public/blimp_client_context_delegate.h" | |
| 30 #include "blimp/client/public/compositor/compositor_dependencies.h" | |
| 31 #include "blimp/net/thread_pipe_manager.h" | |
| 32 #include "components/prefs/pref_service.h" | |
| 33 #include "device/geolocation/geolocation_delegate.h" | |
| 34 #include "device/geolocation/location_arbitrator.h" | |
| 35 #include "ui/gfx/native_widget_types.h" | |
| 36 | |
| 37 #if defined(OS_ANDROID) | |
| 38 #include "blimp/client/core/context/android/blimp_client_context_impl_android.h" | |
| 39 #include "blimp/client/core/settings/android/settings_android.h" | |
| 40 #endif // OS_ANDROID | |
| 41 | |
| 42 namespace blimp { | |
| 43 namespace client { | |
| 44 | |
| 45 namespace { | |
| 46 | |
| 47 const char kDefaultAssignerUrl[] = | |
| 48 "https://blimp-pa.googleapis.com/v1/assignment"; | |
| 49 | |
| 50 void DropConnectionOnIOThread(ClientNetworkComponents* net_components) { | |
| 51 net_components->GetBrowserConnectionHandler()->DropCurrentConnection(); | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 // This function is declared in //blimp/client/public/blimp_client_context.h, | |
| 57 // and either this function or the one in | |
| 58 // //blimp/client/core/dummy_blimp_client_context.cc should be linked in to | |
| 59 // any binary using BlimpClientContext::Create. | |
| 60 // static | |
| 61 BlimpClientContext* BlimpClientContext::Create( | |
| 62 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner, | |
| 63 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | |
| 64 std::unique_ptr<CompositorDependencies> compositor_dependencies, | |
| 65 PrefService* local_state) { | |
| 66 #if defined(OS_ANDROID) | |
| 67 auto settings = base::MakeUnique<SettingsAndroid>(local_state); | |
| 68 return new BlimpClientContextImplAndroid( | |
| 69 io_thread_task_runner, file_thread_task_runner, | |
| 70 std::move(compositor_dependencies), std::move(settings)); | |
| 71 #else | |
| 72 auto settings = base::MakeUnique<Settings>(local_state); | |
| 73 return new BlimpClientContextImpl( | |
| 74 io_thread_task_runner, file_thread_task_runner, | |
| 75 std::move(compositor_dependencies), std::move(settings), nullptr); | |
| 76 #endif // defined(OS_ANDROID) | |
| 77 } | |
| 78 | |
| 79 // This function is declared in //blimp/client/public/blimp_client_context.h | |
| 80 // and either this function or the one in | |
| 81 // //blimp/client/core/dummy_blimp_client_context.cc should be linked in to | |
| 82 // any binary using BlimpClientContext::RegisterPrefs. | |
| 83 // static | |
| 84 void BlimpClientContext::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 85 Settings::RegisterPrefs(registry); | |
| 86 } | |
| 87 | |
| 88 // This function is declared in //blimp/client/public/blimp_client_context.h | |
| 89 // and either this function or the one in | |
| 90 // //blimp/client/core/dummy_blimp_client_context.cc should be linked in to | |
| 91 // any binary using BlimpClientContext::ApplyBlimpSwitches. | |
| 92 // static | |
| 93 void BlimpClientContext::ApplyBlimpSwitches(CommandLinePrefStore* store) { | |
| 94 Settings::ApplyBlimpSwitches(store); | |
| 95 } | |
| 96 | |
| 97 BlimpClientContextImpl::BlimpClientContextImpl( | |
| 98 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner, | |
| 99 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | |
| 100 std::unique_ptr<CompositorDependencies> compositor_dependencies, | |
| 101 std::unique_ptr<Settings> settings, | |
| 102 std::unique_ptr<PipeManager> pipe_manager) | |
| 103 : BlimpClientContext(), | |
| 104 io_thread_task_runner_(io_thread_task_runner), | |
| 105 file_thread_task_runner_(file_thread_task_runner), | |
| 106 blimp_compositor_dependencies_( | |
| 107 base::MakeUnique<BlimpCompositorDependencies>( | |
| 108 std::move(compositor_dependencies))), | |
| 109 settings_(std::move(settings)), | |
| 110 blob_channel_feature_(new BlobChannelFeature(this)), | |
| 111 geolocation_feature_(base::MakeUnique<GeolocationFeature>( | |
| 112 base::MakeUnique<device::LocationArbitrator>( | |
| 113 base::MakeUnique<device::GeolocationDelegate>()))), | |
| 114 ime_feature_(new ImeFeature), | |
| 115 navigation_feature_(new NavigationFeature), | |
| 116 render_widget_feature_(new RenderWidgetFeature), | |
| 117 settings_feature_(base::MakeUnique<SettingsFeature>(settings_.get())), | |
| 118 tab_control_feature_(new TabControlFeature), | |
| 119 blimp_contents_manager_( | |
| 120 new BlimpContentsManager(blimp_compositor_dependencies_.get(), | |
| 121 ime_feature_.get(), | |
| 122 navigation_feature_.get(), | |
| 123 render_widget_feature_.get(), | |
| 124 tab_control_feature_.get())), | |
| 125 pipe_manager_(std::move(pipe_manager)), | |
| 126 weak_factory_(this) { | |
| 127 net_components_.reset(new ClientNetworkComponents( | |
| 128 base::MakeUnique<CrossThreadNetworkEventObserver>( | |
| 129 connection_status_.GetWeakPtr(), | |
| 130 base::SequencedTaskRunnerHandle::Get()))); | |
| 131 | |
| 132 // The |pipe_manager_| must be set up correctly before features are | |
| 133 // registered. | |
| 134 if (!pipe_manager_) { | |
| 135 pipe_manager_ = base::MakeUnique<ThreadPipeManager>( | |
| 136 io_thread_task_runner_, net_components_->GetBrowserConnectionHandler()); | |
| 137 } | |
| 138 | |
| 139 RegisterFeatures(); | |
| 140 settings_feature_->PushSettings(); | |
| 141 | |
| 142 connection_status_.AddObserver(this); | |
| 143 | |
| 144 // Initialize must only be posted after the features have been | |
| 145 // registered. | |
| 146 io_thread_task_runner_->PostTask( | |
| 147 FROM_HERE, base::Bind(&ClientNetworkComponents::Initialize, | |
| 148 base::Unretained(net_components_.get()))); | |
| 149 | |
| 150 UMA_HISTOGRAM_BOOLEAN("Blimp.Supported", true); | |
| 151 } | |
| 152 | |
| 153 BlimpClientContextImpl::~BlimpClientContextImpl() { | |
| 154 io_thread_task_runner_->DeleteSoon(FROM_HERE, net_components_.release()); | |
| 155 connection_status_.RemoveObserver(this); | |
| 156 } | |
| 157 | |
| 158 void BlimpClientContextImpl::SetDelegate(BlimpClientContextDelegate* delegate) { | |
| 159 DCHECK(!delegate_ || !delegate); | |
| 160 delegate_ = delegate; | |
| 161 | |
| 162 // TODO(xingliu): Pass the IdentityProvider needed by |assignment_fetcher_| | |
| 163 // in the constructor, see crbug/661848. | |
| 164 if (delegate_) { | |
| 165 assignment_fetcher_ = base::MakeUnique<AssignmentFetcher>( | |
| 166 io_thread_task_runner_, file_thread_task_runner_, | |
| 167 delegate_->CreateIdentityProvider(), GetAssignerURL(), | |
| 168 base::Bind(&BlimpClientContextImpl::OnAssignmentReceived, | |
| 169 weak_factory_.GetWeakPtr()), | |
| 170 base::Bind(&BlimpClientContextDelegate::OnAuthenticationError, | |
| 171 base::Unretained(delegate_))); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 std::unique_ptr<BlimpContents> BlimpClientContextImpl::CreateBlimpContents( | |
| 176 gfx::NativeWindow window) { | |
| 177 std::unique_ptr<BlimpContents> blimp_contents = | |
| 178 blimp_contents_manager_->CreateBlimpContents(window); | |
| 179 if (blimp_contents) | |
| 180 delegate_->AttachBlimpContentsHelpers(blimp_contents.get()); | |
| 181 return blimp_contents; | |
| 182 } | |
| 183 | |
| 184 void BlimpClientContextImpl::Connect() { | |
| 185 DCHECK(delegate_); | |
| 186 DCHECK(assignment_fetcher_); | |
| 187 assignment_fetcher_->Fetch(); | |
| 188 } | |
| 189 | |
| 190 void BlimpClientContextImpl::ConnectWithAssignment( | |
| 191 const Assignment& assignment) { | |
| 192 io_thread_task_runner_->PostTask( | |
| 193 FROM_HERE, | |
| 194 base::Bind(&ClientNetworkComponents::ConnectWithAssignment, | |
| 195 base::Unretained(net_components_.get()), assignment)); | |
| 196 } | |
| 197 | |
| 198 std::unordered_map<std::string, std::string> | |
| 199 BlimpClientContextImpl::CreateFeedbackData() { | |
| 200 IdentitySource* identity_source = GetIdentitySource(); | |
| 201 DCHECK(identity_source); | |
| 202 return CreateBlimpFeedbackData(blimp_contents_manager_.get(), | |
| 203 identity_source->GetActiveUsername()); | |
| 204 } | |
| 205 | |
| 206 IdentitySource* BlimpClientContextImpl::GetIdentitySource() { | |
| 207 DCHECK(delegate_); | |
| 208 DCHECK(assignment_fetcher_); | |
| 209 return assignment_fetcher_->GetIdentitySource(); | |
| 210 } | |
| 211 | |
| 212 ConnectionStatus* BlimpClientContextImpl::GetConnectionStatus() { | |
| 213 return &connection_status_; | |
| 214 } | |
| 215 | |
| 216 GURL BlimpClientContextImpl::GetAssignerURL() { | |
| 217 return GURL(kDefaultAssignerUrl); | |
| 218 } | |
| 219 | |
| 220 void BlimpClientContextImpl::OnAssignmentReceived( | |
| 221 AssignmentRequestResult result, | |
| 222 const Assignment& assignment) { | |
| 223 VLOG(1) << "Assignment result: " << result; | |
| 224 | |
| 225 // Cache engine info. | |
| 226 connection_status_.OnAssignmentResult(result, assignment); | |
| 227 | |
| 228 // Inform the embedder of the assignment result. | |
| 229 if (delegate_) { | |
| 230 delegate_->OnAssignmentConnectionAttempted(result, assignment); | |
| 231 } | |
| 232 | |
| 233 if (result != ASSIGNMENT_REQUEST_RESULT_OK) { | |
| 234 LOG(ERROR) << "Assignment failed, reason: " << result; | |
| 235 return; | |
| 236 } | |
| 237 | |
| 238 ConnectWithAssignment(assignment); | |
| 239 } | |
| 240 | |
| 241 void BlimpClientContextImpl::RegisterFeatures() { | |
| 242 // Register features' message senders and receivers. | |
| 243 pipe_manager_->RegisterFeature(BlimpMessage::kBlobChannel, | |
| 244 blob_channel_feature_.get()); | |
| 245 geolocation_feature_->set_outgoing_message_processor( | |
| 246 pipe_manager_->RegisterFeature(BlimpMessage::kGeolocation, | |
| 247 geolocation_feature_.get())); | |
| 248 ime_feature_->set_outgoing_message_processor( | |
| 249 pipe_manager_->RegisterFeature(BlimpMessage::kIme, | |
| 250 ime_feature_.get())); | |
| 251 navigation_feature_->set_outgoing_message_processor( | |
| 252 pipe_manager_->RegisterFeature(BlimpMessage::kNavigation, | |
| 253 navigation_feature_.get())); | |
| 254 render_widget_feature_->set_outgoing_input_message_processor( | |
| 255 pipe_manager_->RegisterFeature(BlimpMessage::kInput, | |
| 256 render_widget_feature_.get())); | |
| 257 render_widget_feature_->set_outgoing_compositor_message_processor( | |
| 258 pipe_manager_->RegisterFeature(BlimpMessage::kCompositor, | |
| 259 render_widget_feature_.get())); | |
| 260 pipe_manager_->RegisterFeature(BlimpMessage::kRenderWidget, | |
| 261 render_widget_feature_.get()); | |
| 262 settings_feature_->set_outgoing_message_processor( | |
| 263 pipe_manager_->RegisterFeature(BlimpMessage::kSettings, | |
| 264 settings_feature_.get())); | |
| 265 tab_control_feature_->set_outgoing_message_processor( | |
| 266 pipe_manager_->RegisterFeature(BlimpMessage::kTabControl, | |
| 267 tab_control_feature_.get())); | |
| 268 } | |
| 269 | |
| 270 void BlimpClientContextImpl::DropConnection() { | |
| 271 io_thread_task_runner_->PostTask( | |
| 272 FROM_HERE, base::Bind(&DropConnectionOnIOThread, net_components_.get())); | |
| 273 } | |
| 274 | |
| 275 void BlimpClientContextImpl::OnImageDecodeError() { | |
| 276 // Currently we just drop the connection on image decoding error. | |
| 277 DropConnection(); | |
| 278 } | |
| 279 | |
| 280 void BlimpClientContextImpl::OnConnected() { | |
| 281 if (delegate_) { | |
| 282 delegate_->OnConnected(); | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 void BlimpClientContextImpl::OnDisconnected(int result) { | |
| 287 if (delegate_) { | |
| 288 if (result >= 0) { | |
| 289 delegate_->OnEngineDisconnected(result); | |
| 290 } else { | |
| 291 delegate_->OnNetworkDisconnected(result); | |
| 292 } | |
| 293 } | |
| 294 } | |
| 295 | |
| 296 } // namespace client | |
| 297 } // namespace blimp | |
| OLD | NEW |