blob: 872582bc64b09989a9bba8a12795f07f97b60df7 [file] [log] [blame]
khmel46050cb2015-11-26 09:49:331// 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#ifndef COMPONENTS_ARC_ARC_BRIDGE_SERVICE_IMPL_H_
6#define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_IMPL_H_
7
dchenga0ee5fb82016-04-26 02:46:558#include <memory>
lhchavezf1c055372015-12-07 22:51:189#include <string>
10#include <vector>
11
khmel46050cb2015-11-26 09:49:3312#include "base/files/scoped_file.h"
lhchavezf1c055372015-12-07 22:51:1813#include "base/gtest_prod_util.h"
khmel46050cb2015-11-26 09:49:3314#include "base/macros.h"
lhchavezf1c055372015-12-07 22:51:1815#include "components/arc/arc_bridge_bootstrap.h"
khmel46050cb2015-11-26 09:49:3316#include "components/arc/arc_bridge_service.h"
lhchavezf1c055372015-12-07 22:51:1817#include "mojo/public/cpp/bindings/binding.h"
khmel46050cb2015-11-26 09:49:3318
19namespace base {
20class SequencedTaskRunner;
21class SingleThreadTaskRunner;
22} // namespace base
23
24namespace arc {
25
26// Real IPC based ArcBridgeService that is used in production.
27class ArcBridgeServiceImpl : public ArcBridgeService,
hidehiko93f78688c2016-07-27 04:53:3128 public ArcBridgeBootstrap::Delegate {
khmel46050cb2015-11-26 09:49:3329 public:
hidehiko321d423c2016-09-30 15:51:3030 // This is the factory interface to inject ArcBridgeBootstrap instance
31 // for testing purpose.
32 using ArcBridgeBootstrapFactory =
33 base::Callback<std::unique_ptr<ArcBridgeBootstrap>()>;
34
35 ArcBridgeServiceImpl();
khmel46050cb2015-11-26 09:49:3336 ~ArcBridgeServiceImpl() override;
37
khmel46050cb2015-11-26 09:49:3338 void HandleStartup() override;
39
40 void Shutdown() override;
41
hidehiko321d423c2016-09-30 15:51:3042 // Inject a factory to create ArcBridgeBootstrap instance for testing
43 // purpose. |factory| must not be null.
44 void SetArcBridgeBootstrapFactoryForTesting(
45 const ArcBridgeBootstrapFactory& factory);
46
47 // Returns the current bootstrap instance for testing purpose.
48 ArcBridgeBootstrap* GetBootstrapForTesting() { return bootstrap_.get(); }
49
lhchaveze143a022016-05-04 03:15:5950 // Normally, reconnecting after connection shutdown happens after a short
51 // delay. When testing, however, we'd like it to happen immediately to avoid
52 // adding unnecessary delays.
53 void DisableReconnectDelayForTesting();
54
khmel46050cb2015-11-26 09:49:3355 private:
56 friend class ArcBridgeTest;
lhchavezaff66ee2016-01-05 19:43:1357 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Restart);
nya0c9dabf2016-07-13 19:54:2758 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, OnBridgeStopped);
khmel46050cb2015-11-26 09:49:3359
60 // If all pre-requisites are true (ARC is available, it has been enabled, and
61 // the session has started), and ARC is stopped, start ARC. If ARC is running
62 // and the pre-requisites stop being true, stop ARC.
63 void PrerequisitesChanged();
64
khmel46050cb2015-11-26 09:49:3365 // Stops the running instance.
66 void StopInstance();
67
lhchavezf1c055372015-12-07 22:51:1868 // ArcBridgeBootstrap::Delegate:
leon.han890322912016-04-16 23:22:5369 void OnConnectionEstablished(mojom::ArcBridgeInstancePtr instance) override;
nya0c9dabf2016-07-13 19:54:2770 void OnStopped(StopReason reason) override;
khmel46050cb2015-11-26 09:49:3371
dchenga0ee5fb82016-04-26 02:46:5572 std::unique_ptr<ArcBridgeBootstrap> bootstrap_;
khmel46050cb2015-11-26 09:49:3373
khmel46050cb2015-11-26 09:49:3374 // If the user's session has started.
75 bool session_started_;
76
hidehiko93f78688c2016-07-27 04:53:3177 // Mojo endpoint.
78 // TODO(hidehiko): Move this to ArcBridgeBootstrap.
79 std::unique_ptr<mojom::ArcBridgeHost> arc_bridge_host_;
80
lhchavezaff66ee2016-01-05 19:43:1381 // If the instance had already been started but the connection to it was
82 // lost. This should make the instance restart.
83 bool reconnect_ = false;
84
lhchaveze143a022016-05-04 03:15:5985 // Delay the reconnection.
86 bool use_delay_before_reconnecting_ = true;
87
hidehiko321d423c2016-09-30 15:51:3088 // Factory to inject a fake ArcBridgeBootstrap instance for testing.
89 ArcBridgeBootstrapFactory factory_;
90
khmel46050cb2015-11-26 09:49:3391 // WeakPtrFactory to use callbacks.
92 base::WeakPtrFactory<ArcBridgeServiceImpl> weak_factory_;
93
94 DISALLOW_COPY_AND_ASSIGN(ArcBridgeServiceImpl);
95};
96
97} // namespace arc
98
99#endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_IMPL_H_