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: |
yusukes | ef9c775 | 2017-11-28 17:57:44 | [diff] [blame] | 34 | // Returns the factory instance for this class. |
| 35 | static BrowserContextKeyedServiceFactory* GetFactory(); |
| 36 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 37 | // Returns singleton instance for the given BrowserContext, |
| 38 | // or nullptr if the browser |context| is not allowed to use ARC. |
| 39 | static ArcMetricsService* GetForBrowserContext( |
| 40 | content::BrowserContext* context); |
| 41 | |
| 42 | ArcMetricsService(content::BrowserContext* context, |
| 43 | ArcBridgeService* bridge_service); |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 44 | ~ArcMetricsService() override; |
| 45 | |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 46 | // Implementations for ConnectionObserver<mojom::ProcessInstance>. |
| 47 | void OnProcessConnectionReady(); |
| 48 | void OnProcessConnectionClosed(); |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 49 | |
cywang | 020b77ac | 2016-04-27 06:13:09 | [diff] [blame] | 50 | // MetricsHost overrides. |
yusukes | d6ea165b | 2017-05-26 23:26:04 | [diff] [blame] | 51 | void ReportBootProgress(std::vector<mojom::BootProgressEventPtr> events, |
| 52 | mojom::BootType boot_type) override; |
cywang | 020b77ac | 2016-04-27 06:13:09 | [diff] [blame] | 53 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 54 | private: |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 55 | // Adapter to be able to also observe ProcessInstance events. |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 56 | class ProcessObserver : public ConnectionObserver<mojom::ProcessInstance> { |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 57 | public: |
| 58 | explicit ProcessObserver(ArcMetricsService* arc_metrics_service); |
| 59 | ~ProcessObserver() override; |
| 60 | |
| 61 | private: |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 62 | // ConnectionObserver<mojom::ProcessInstance> overrides. |
| 63 | void OnConnectionReady() override; |
| 64 | void OnConnectionClosed() override; |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 65 | |
| 66 | ArcMetricsService* arc_metrics_service_; |
yusukes | dbfd395 | 2017-01-16 02:48:56 | [diff] [blame] | 67 | |
| 68 | DISALLOW_COPY_AND_ASSIGN(ProcessObserver); |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 69 | }; |
| 70 | |
Hidehiko Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 71 | void RequestProcessList(); |
| 72 | void ParseProcessList(std::vector<mojom::RunningAppProcessInfoPtr> processes); |
| 73 | |
| 74 | // DBus callbacks. |
Hidehiko Abe | d0c8e46 | 2017-11-22 06:02:17 | [diff] [blame] | 75 | void OnArcStartTimeRetrieved(std::vector<mojom::BootProgressEventPtr> events, |
| 76 | mojom::BootType boot_type, |
| 77 | base::Optional<base::TimeTicks> arc_start_time); |
Hidehiko Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 78 | |
| 79 | THREAD_CHECKER(thread_checker_); |
| 80 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 81 | ArcBridgeService* const arc_bridge_service_; // Owned by ArcServiceManager. |
| 82 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 83 | ProcessObserver process_observer_; |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 84 | base::RepeatingTimer timer_; |
| 85 | |
| 86 | // Always keep this the last member of this class to make sure it's the |
| 87 | // first thing to be destructed. |
| 88 | base::WeakPtrFactory<ArcMetricsService> weak_ptr_factory_; |
| 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(ArcMetricsService); |
| 91 | }; |
| 92 | |
| 93 | } // namespace arc |
| 94 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 95 | #endif // COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_ |