Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(512)

Side by Side Diff: components/arc/arc_bridge_service_impl.cc

Issue 2379223004: Switch from Delegate to Observer. (Closed)
Patch Set: Rebase Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/arc/arc_bridge_service_impl.h ('k') | components/arc/arc_bridge_service_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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 "components/arc/arc_bridge_service_impl.h" 5 #include "components/arc/arc_bridge_service_impl.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
12 #include "base/sequenced_task_runner.h" 12 #include "base/sequenced_task_runner.h"
13 #include "base/sys_info.h" 13 #include "base/sys_info.h"
14 #include "base/task_runner_util.h" 14 #include "base/task_runner_util.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "chromeos/chromeos_switches.h" 17 #include "chromeos/chromeos_switches.h"
18 #include "chromeos/dbus/dbus_method_call_status.h" 18 #include "chromeos/dbus/dbus_method_call_status.h"
19 #include "chromeos/dbus/dbus_thread_manager.h" 19 #include "chromeos/dbus/dbus_thread_manager.h"
20 #include "components/arc/arc_bridge_host_impl.h"
21 #include "components/prefs/pref_registry_simple.h" 20 #include "components/prefs/pref_registry_simple.h"
22 #include "components/prefs/pref_service.h" 21 #include "components/prefs/pref_service.h"
23 22
24 namespace arc { 23 namespace arc {
25 24
26 extern ArcBridgeService* g_arc_bridge_service; 25 extern ArcBridgeService* g_arc_bridge_service;
27 26
28 namespace { 27 namespace {
29 constexpr int64_t kReconnectDelayInSeconds = 5; 28 constexpr int64_t kReconnectDelayInSeconds = 5;
30 } // namespace 29 } // namespace
31 30
32 ArcBridgeServiceImpl::ArcBridgeServiceImpl() 31 ArcBridgeServiceImpl::ArcBridgeServiceImpl()
33 : session_started_(false), 32 : session_started_(false),
34 factory_(base::Bind(ArcBridgeBootstrap::Create)), 33 factory_(base::Bind(ArcBridgeBootstrap::Create)),
35 weak_factory_(this) { 34 weak_factory_(this) {
36 DCHECK(!g_arc_bridge_service); 35 DCHECK(!g_arc_bridge_service);
37 g_arc_bridge_service = this; 36 g_arc_bridge_service = this;
38 } 37 }
39 38
40 ArcBridgeServiceImpl::~ArcBridgeServiceImpl() { 39 ArcBridgeServiceImpl::~ArcBridgeServiceImpl() {
40 if (bootstrap_)
41 bootstrap_->RemoveObserver(this);
42
41 DCHECK(g_arc_bridge_service == this); 43 DCHECK(g_arc_bridge_service == this);
42 g_arc_bridge_service = nullptr; 44 g_arc_bridge_service = nullptr;
43 } 45 }
44 46
45 void ArcBridgeServiceImpl::HandleStartup() { 47 void ArcBridgeServiceImpl::HandleStartup() {
46 DCHECK(CalledOnValidThread()); 48 DCHECK(CalledOnValidThread());
47 if (session_started_) 49 if (session_started_)
48 return; 50 return;
49 VLOG(1) << "Session started"; 51 VLOG(1) << "Session started";
50 session_started_ = true; 52 session_started_ = true;
(...skipping 24 matching lines...) Expand all
75 DCHECK(CalledOnValidThread()); 77 DCHECK(CalledOnValidThread());
76 VLOG(1) << "Prerequisites changed. " 78 VLOG(1) << "Prerequisites changed. "
77 << "state=" << static_cast<uint32_t>(state()) 79 << "state=" << static_cast<uint32_t>(state())
78 << ", session_started=" << session_started_; 80 << ", session_started=" << session_started_;
79 if (state() == State::STOPPED) { 81 if (state() == State::STOPPED) {
80 if (!session_started_) 82 if (!session_started_)
81 return; 83 return;
82 VLOG(0) << "Prerequisites met, starting ARC"; 84 VLOG(0) << "Prerequisites met, starting ARC";
83 SetStopReason(StopReason::SHUTDOWN); 85 SetStopReason(StopReason::SHUTDOWN);
84 86
87 if (bootstrap_)
88 bootstrap_->RemoveObserver(this);
89
85 SetState(State::CONNECTING); 90 SetState(State::CONNECTING);
86 bootstrap_ = factory_.Run(); 91 bootstrap_ = factory_.Run();
87 bootstrap_->set_delegate(this); 92 bootstrap_->AddObserver(this);
88 bootstrap_->Start(); 93 bootstrap_->Start();
89 } else { 94 } else {
90 if (session_started_) 95 if (session_started_)
91 return; 96 return;
92 VLOG(0) << "Prerequisites stopped being met, stopping ARC"; 97 VLOG(0) << "Prerequisites stopped being met, stopping ARC";
93 StopInstance(); 98 StopInstance();
94 } 99 }
95 } 100 }
96 101
97 void ArcBridgeServiceImpl::StopInstance() { 102 void ArcBridgeServiceImpl::StopInstance() {
98 DCHECK(CalledOnValidThread()); 103 DCHECK(CalledOnValidThread());
99 if (state() == State::STOPPED || state() == State::STOPPING) { 104 if (state() == State::STOPPED || state() == State::STOPPING) {
100 VLOG(1) << "StopInstance() called when ARC is not running"; 105 VLOG(1) << "StopInstance() called when ARC is not running";
101 return; 106 return;
102 } 107 }
103 108
104 // We were explicitly asked to stop, so do not reconnect. 109 // We were explicitly asked to stop, so do not reconnect.
105 reconnect_ = false; 110 reconnect_ = false;
106 111
107 VLOG(1) << "Stopping ARC"; 112 VLOG(1) << "Stopping ARC";
108 DCHECK(bootstrap_.get()); 113 DCHECK(bootstrap_.get());
109 SetState(State::STOPPING); 114 SetState(State::STOPPING);
110 arc_bridge_host_.reset();
111 115
112 // Note: this can call OnStopped() internally as a callback. 116 // Note: this can call OnStopped() internally as a callback.
113 bootstrap_->Stop(); 117 bootstrap_->Stop();
114 } 118 }
115 119
116 void ArcBridgeServiceImpl::OnConnectionEstablished( 120 void ArcBridgeServiceImpl::OnReady() {
117 mojom::ArcBridgeInstancePtr instance) {
118 DCHECK(CalledOnValidThread()); 121 DCHECK(CalledOnValidThread());
119 if (state() != State::CONNECTING) { 122 if (state() != State::CONNECTING) {
120 VLOG(1) << "StopInstance() called while connecting"; 123 VLOG(1) << "StopInstance() called while connecting";
121 return; 124 return;
122 } 125 }
123 126
124 arc_bridge_host_.reset(new ArcBridgeHostImpl(std::move(instance)));
125
126 // The container can be considered to have been successfully launched, so 127 // The container can be considered to have been successfully launched, so
127 // restart if the connection goes down without being requested. 128 // restart if the connection goes down without being requested.
128 reconnect_ = true; 129 reconnect_ = true;
129 VLOG(0) << "ARC ready"; 130 VLOG(0) << "ARC ready";
130 SetState(State::READY); 131 SetState(State::READY);
131 } 132 }
132 133
133 void ArcBridgeServiceImpl::OnStopped(StopReason stop_reason) { 134 void ArcBridgeServiceImpl::OnStopped(StopReason stop_reason) {
134 DCHECK(CalledOnValidThread()); 135 DCHECK(CalledOnValidThread());
135 VLOG(0) << "ARC stopped: " << stop_reason; 136 VLOG(0) << "ARC stopped: " << stop_reason;
136 arc_bridge_host_.reset(); 137 bootstrap_->RemoveObserver(this);
137 bootstrap_.reset(); 138 bootstrap_.reset();
138 SetStopReason(stop_reason); 139 SetStopReason(stop_reason);
139 SetState(State::STOPPED); 140 SetState(State::STOPPED);
140 141
141 if (reconnect_) { 142 if (reconnect_) {
142 // There was a previous invocation and it crashed for some reason. Try 143 // There was a previous invocation and it crashed for some reason. Try
143 // starting the container again. 144 // starting the container again.
144 reconnect_ = false; 145 reconnect_ = false;
145 VLOG(0) << "ARC reconnecting"; 146 VLOG(0) << "ARC reconnecting";
146 if (use_delay_before_reconnecting_) { 147 if (use_delay_before_reconnecting_) {
147 // Instead of immediately trying to restart the container, give it some 148 // Instead of immediately trying to restart the container, give it some
148 // time to finish tearing down in case it is still in the process of 149 // time to finish tearing down in case it is still in the process of
149 // stopping. 150 // stopping.
150 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 151 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
151 FROM_HERE, base::Bind(&ArcBridgeServiceImpl::PrerequisitesChanged, 152 FROM_HERE, base::Bind(&ArcBridgeServiceImpl::PrerequisitesChanged,
152 weak_factory_.GetWeakPtr()), 153 weak_factory_.GetWeakPtr()),
153 base::TimeDelta::FromSeconds(kReconnectDelayInSeconds)); 154 base::TimeDelta::FromSeconds(kReconnectDelayInSeconds));
154 } else { 155 } else {
155 // Restart immediately. 156 // Restart immediately.
156 PrerequisitesChanged(); 157 PrerequisitesChanged();
157 } 158 }
158 } 159 }
159 } 160 }
160 161
161 } // namespace arc 162 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/arc_bridge_service_impl.h ('k') | components/arc/arc_bridge_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698