1 // Copyright 2020 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 CHROME_BROWSER_UI_WEBUI_CHROMEOS_ARC_POWER_CONTROL_ARC_POWER_CONTROL_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_ARC_POWER_CONTROL_ARC_POWER_CONTROL_HANDLER_H_
7 
8 #include <memory>
9 #include <string>
10 #include <utility>
11 #include <vector>
12 
13 #include "base/memory/weak_ptr.h"
14 #include "base/timer/timer.h"
15 #include "chrome/browser/chromeos/throttle_service.h"
16 #include "components/arc/mojom/power.mojom.h"
17 #include "components/arc/power/arc_power_bridge.h"
18 #include "content/public/browser/web_ui_message_handler.h"
19 #include "ui/aura/window_observer.h"
20 #include "ui/events/event_handler.h"
21 #include "ui/wm/public/activation_change_observer.h"
22 
23 namespace arc {
24 class ArcInstanceThrottle;
25 class ArcSystemStatCollector;
26 }  // namespace arc
27 
28 namespace chromeos {
29 
30 class ArcPowerControlHandler : public content::WebUIMessageHandler,
31                                public arc::ArcPowerBridge::Observer,
32                                public ThrottleService::ServiceObserver {
33  public:
34   using WakefulnessModeEvents =
35       std::vector<std::pair<base::TimeTicks, arc::mojom::WakefulnessMode>>;
36   using ThrottlingEvents =
37       std::vector<std::pair<base::TimeTicks, ThrottleObserver::PriorityLevel>>;
38 
39   ArcPowerControlHandler();
40   ~ArcPowerControlHandler() override;
41 
42   // content::WebUIMessageHandler:
43   void RegisterMessages() override;
44 
45   // arc::ArcPowerBridge::Observer:
46   void OnWakefulnessChanged(arc::mojom::WakefulnessMode mode) override;
47 
48   // ThrottleService::ServiceObserver:
49   void OnThrottle(ThrottleObserver::PriorityLevel level) override;
50 
51  private:
52   // Handlers for calls from JS.
53   void HandleReady(const base::ListValue* args);
54   void HandleSetWakefulnessMode(const base::ListValue* args);
55   void HandleSetThrottling(const base::ListValue* args);
56   void HandleStartTracing(const base::ListValue* args);
57   void HandleStopTracing(const base::ListValue* args);
58 
59   void StartTracing();
60   void StopTracing();
61   void OnTracingModelReady(base::Value result);
62 
63   void UpdatePowerControlStatus();
64   void SetTracingStatus(const std::string& status);
65 
66   void OnIsDeveloperMode(bool developer_mode);
67 
68   // Unowned pointers.
69   arc::ArcPowerBridge* const power_bridge_;
70   arc::ArcInstanceThrottle* const instance_throttle_;
71 
72   // Collects system stats runtime.
73   base::Time timestamp_;
74   base::TimeTicks tracing_time_min_;
75   base::OneShotTimer stop_tracing_timer_;
76   std::unique_ptr<arc::ArcSystemStatCollector> system_stat_colletor_;
77 
78   // It collects power mode and throttling events in case tracing is active.
79   WakefulnessModeEvents wakefulness_mode_events_;
80   ThrottlingEvents throttling_events_;
81 
82   // Keeps current wakefulness mode.
83   arc::mojom::WakefulnessMode wakefulness_mode_ =
84       arc::mojom::WakefulnessMode::UNKNOWN;
85 
86   // Enabled in dev mode only.
87   bool power_control_enabled_ = false;
88 
89   base::WeakPtrFactory<ArcPowerControlHandler> weak_ptr_factory_{this};
90 
91   ArcPowerControlHandler(ArcPowerControlHandler const&) = delete;
92   ArcPowerControlHandler& operator=(ArcPowerControlHandler const&) = delete;
93 };
94 
95 }  // namespace chromeos
96 
97 #endif  // CHROME_BROWSER_UI_WEBUI_CHROMEOS_ARC_POWER_CONTROL_ARC_POWER_CONTROL_HANDLER_H_
98