cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 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 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 5 | #ifndef COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_ |
| 6 | #define COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_ |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 7 | |
lhchavez | db94ad7 | 2016-11-12 04:08:47 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 10 | #include "base/macros.h" |
| 11 | #include "base/memory/weak_ptr.h" |
Hidehiko Abe | 3c446ed | 2017-10-11 10:31:25 | [diff] [blame] | 12 | #include "base/optional.h" |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 13 | #include "base/threading/thread_checker.h" |
| 14 | #include "base/timer/timer.h" |
yusukes | 883612b | 2016-10-13 18:07:05 | [diff] [blame] | 15 | #include "components/arc/common/metrics.mojom.h" |
| 16 | #include "components/arc/common/process.mojom.h" |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 17 | #include "components/arc/connection_observer.h" |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 18 | #include "components/keyed_service/core/keyed_service.h" |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 19 | |
yusukes | ef9c775 | 2017-11-28 17:57:44 | [diff] [blame] | 20 | class BrowserContextKeyedServiceFactory; |
| 21 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 22 | namespace content { |
| 23 | class BrowserContext; |
| 24 | } // namespace content |
| 25 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 26 | namespace arc { |
| 27 | |
yusukes | 883612b | 2016-10-13 18:07:05 | [diff] [blame] | 28 | class ArcBridgeService; |
| 29 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 30 | // Collects information from other ArcServices and send UMA metrics. |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 31 | class ArcMetricsService : public KeyedService, |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 32 | public mojom::MetricsHost { |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 33 | public: |
Lev Rumyantsev | deae325 | 2017-12-06 04:57:05 | [diff] [blame^] | 34 | // This is public for testing only. |
| 35 | enum class NativeBridgeType { |
| 36 | // Native bridge value has not been received from the container yet. |
| 37 | UNKNOWN = 0, |
| 38 | // Native bridge is not used. |
| 39 | NONE = 1, |
| 40 | // Using houdini translator. |
| 41 | HOUDINI = 2, |
| 42 | // Using ndk-translation translator. |
| 43 | NDK_TRANSLATION = 3, |
| 44 | COUNT |
| 45 | }; |
| 46 | |
yusukes | ef9c775 | 2017-11-28 17:57:44 | [diff] [blame] | 47 | // Returns the factory instance for this class. |
| 48 | static BrowserContextKeyedServiceFactory* GetFactory(); |
| 49 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 50 | // Returns singleton instance for the given BrowserContext, |
| 51 | // or nullptr if the browser |context| is not allowed to use ARC. |
| 52 | static ArcMetricsService* GetForBrowserContext( |
| 53 | content::BrowserContext* context); |
| 54 | |
| 55 | ArcMetricsService(content::BrowserContext* context, |
| 56 | ArcBridgeService* bridge_service); |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 57 | ~ArcMetricsService() override; |
| 58 | |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 59 | // Implementations for ConnectionObserver<mojom::ProcessInstance>. |
| 60 | void OnProcessConnectionReady(); |
| 61 | void OnProcessConnectionClosed(); |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 62 | |
cywang | 020b77ac | 2016-04-27 06:13:09 | [diff] [blame] | 63 | // MetricsHost overrides. |
yusukes | d6ea165b | 2017-05-26 23:26:04 | [diff] [blame] | 64 | void ReportBootProgress(std::vector<mojom::BootProgressEventPtr> events, |
| 65 | mojom::BootType boot_type) override; |
Lev Rumyantsev | deae325 | 2017-12-06 04:57:05 | [diff] [blame^] | 66 | void ReportNativeBridge(mojom::NativeBridgeType native_bridge_type) override; |
| 67 | |
| 68 | NativeBridgeType native_bridge_type_for_testing() const { |
| 69 | return native_bridge_type_; |
| 70 | } |
cywang | 020b77ac | 2016-04-27 06:13:09 | [diff] [blame] | 71 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 72 | private: |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 73 | // Adapter to be able to also observe ProcessInstance events. |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 74 | class ProcessObserver : public ConnectionObserver<mojom::ProcessInstance> { |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 75 | public: |
| 76 | explicit ProcessObserver(ArcMetricsService* arc_metrics_service); |
| 77 | ~ProcessObserver() override; |
| 78 | |
| 79 | private: |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 80 | // ConnectionObserver<mojom::ProcessInstance> overrides. |
| 81 | void OnConnectionReady() override; |
| 82 | void OnConnectionClosed() override; |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 83 | |
| 84 | ArcMetricsService* arc_metrics_service_; |
yusukes | dbfd395 | 2017-01-16 02:48:56 | [diff] [blame] | 85 | |
| 86 | DISALLOW_COPY_AND_ASSIGN(ProcessObserver); |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 87 | }; |
| 88 | |
Hidehiko Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 89 | void RequestProcessList(); |
| 90 | void ParseProcessList(std::vector<mojom::RunningAppProcessInfoPtr> processes); |
| 91 | |
| 92 | // DBus callbacks. |
Hidehiko Abe | d0c8e46 | 2017-11-22 06:02:17 | [diff] [blame] | 93 | void OnArcStartTimeRetrieved(std::vector<mojom::BootProgressEventPtr> events, |
| 94 | mojom::BootType boot_type, |
| 95 | base::Optional<base::TimeTicks> arc_start_time); |
Hidehiko Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 96 | |
| 97 | THREAD_CHECKER(thread_checker_); |
| 98 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 99 | ArcBridgeService* const arc_bridge_service_; // Owned by ArcServiceManager. |
| 100 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 101 | ProcessObserver process_observer_; |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 102 | base::RepeatingTimer timer_; |
| 103 | |
Lev Rumyantsev | deae325 | 2017-12-06 04:57:05 | [diff] [blame^] | 104 | NativeBridgeType native_bridge_type_; |
| 105 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 106 | // Always keep this the last member of this class to make sure it's the |
| 107 | // first thing to be destructed. |
| 108 | base::WeakPtrFactory<ArcMetricsService> weak_ptr_factory_; |
| 109 | |
| 110 | DISALLOW_COPY_AND_ASSIGN(ArcMetricsService); |
| 111 | }; |
| 112 | |
| 113 | } // namespace arc |
| 114 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 115 | #endif // COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_ |