khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 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 | #ifndef COMPONENTS_ARC_ARC_BRIDGE_SERVICE_IMPL_H_ |
| 6 | #define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_IMPL_H_ |
| 7 | |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 8 | #include <memory> |
lhchavez | f1c05537 | 2015-12-07 22:51:18 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 12 | #include "base/files/scoped_file.h" |
lhchavez | f1c05537 | 2015-12-07 22:51:18 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 14 | #include "base/macros.h" |
lhchavez | f1c05537 | 2015-12-07 22:51:18 | [diff] [blame] | 15 | #include "components/arc/arc_bridge_bootstrap.h" |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 16 | #include "components/arc/arc_bridge_service.h" |
lhchavez | f1c05537 | 2015-12-07 22:51:18 | [diff] [blame] | 17 | #include "mojo/public/cpp/bindings/binding.h" |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 18 | |
| 19 | namespace base { |
| 20 | class SequencedTaskRunner; |
| 21 | class SingleThreadTaskRunner; |
| 22 | } // namespace base |
| 23 | |
| 24 | namespace arc { |
| 25 | |
| 26 | // Real IPC based ArcBridgeService that is used in production. |
| 27 | class ArcBridgeServiceImpl : public ArcBridgeService, |
hidehiko | 93f78688c | 2016-07-27 04:53:31 | [diff] [blame] | 28 | public ArcBridgeBootstrap::Delegate { |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 29 | public: |
hidehiko | 321d423c | 2016-09-30 15:51:30 | [diff] [blame^] | 30 | // 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(); |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 36 | ~ArcBridgeServiceImpl() override; |
| 37 | |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 38 | void HandleStartup() override; |
| 39 | |
| 40 | void Shutdown() override; |
| 41 | |
hidehiko | 321d423c | 2016-09-30 15:51:30 | [diff] [blame^] | 42 | // 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 | |
lhchavez | e143a02 | 2016-05-04 03:15:59 | [diff] [blame] | 50 | // 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 | |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 55 | private: |
| 56 | friend class ArcBridgeTest; |
lhchavez | aff66ee | 2016-01-05 19:43:13 | [diff] [blame] | 57 | FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Restart); |
nya | 0c9dabf | 2016-07-13 19:54:27 | [diff] [blame] | 58 | FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, OnBridgeStopped); |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 59 | |
| 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 | |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 65 | // Stops the running instance. |
| 66 | void StopInstance(); |
| 67 | |
lhchavez | f1c05537 | 2015-12-07 22:51:18 | [diff] [blame] | 68 | // ArcBridgeBootstrap::Delegate: |
leon.han | 89032291 | 2016-04-16 23:22:53 | [diff] [blame] | 69 | void OnConnectionEstablished(mojom::ArcBridgeInstancePtr instance) override; |
nya | 0c9dabf | 2016-07-13 19:54:27 | [diff] [blame] | 70 | void OnStopped(StopReason reason) override; |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 71 | |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 72 | std::unique_ptr<ArcBridgeBootstrap> bootstrap_; |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 73 | |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 74 | // If the user's session has started. |
| 75 | bool session_started_; |
| 76 | |
hidehiko | 93f78688c | 2016-07-27 04:53:31 | [diff] [blame] | 77 | // Mojo endpoint. |
| 78 | // TODO(hidehiko): Move this to ArcBridgeBootstrap. |
| 79 | std::unique_ptr<mojom::ArcBridgeHost> arc_bridge_host_; |
| 80 | |
lhchavez | aff66ee | 2016-01-05 19:43:13 | [diff] [blame] | 81 | // 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 | |
lhchavez | e143a02 | 2016-05-04 03:15:59 | [diff] [blame] | 85 | // Delay the reconnection. |
| 86 | bool use_delay_before_reconnecting_ = true; |
| 87 | |
hidehiko | 321d423c | 2016-09-30 15:51:30 | [diff] [blame^] | 88 | // Factory to inject a fake ArcBridgeBootstrap instance for testing. |
| 89 | ArcBridgeBootstrapFactory factory_; |
| 90 | |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 91 | // 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_ |