1 // Copyright 2017 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_CHROMEOS_ARC_INSTANCE_THROTTLE_ARC_INSTANCE_THROTTLE_H_
6 #define CHROME_BROWSER_CHROMEOS_ARC_INSTANCE_THROTTLE_ARC_INSTANCE_THROTTLE_H_
7 
8 #include <memory>
9 #include <string>
10 #include <utility>
11 
12 #include "base/macros.h"
13 #include "chrome/browser/chromeos/throttle_observer.h"
14 #include "chrome/browser/chromeos/throttle_service.h"
15 #include "components/arc/arc_util.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 
18 namespace content {
19 class BrowserContext;
20 }
21 
22 namespace arc {
23 class ArcBridgeService;
24 
25 // This class holds a number of observers which watch for several conditions
26 // (window activation, mojom instance connection, etc) and adjusts the
27 // throttling state of the ARC container on a change in conditions.
28 class ArcInstanceThrottle : public KeyedService,
29                             public chromeos::ThrottleService {
30  public:
31   class Delegate {
32    public:
33     Delegate() = default;
34     virtual ~Delegate() = default;
35 
36     virtual void SetCpuRestriction(
37         CpuRestrictionState cpu_restriction_state) = 0;
38     virtual void RecordCpuRestrictionDisabledUMA(
39         const std::string& observer_name,
40         base::TimeDelta delta) = 0;
41 
42    private:
43     DISALLOW_COPY_AND_ASSIGN(Delegate);
44   };
45 
46   // Returns singleton instance for the given BrowserContext, or nullptr if
47   // the browser |context| is not allowed to use ARC.
48   static ArcInstanceThrottle* GetForBrowserContext(
49       content::BrowserContext* context);
50   static ArcInstanceThrottle* GetForBrowserContextForTesting(
51       content::BrowserContext* context);
52 
53   ArcInstanceThrottle(content::BrowserContext* context,
54                       ArcBridgeService* arc_bridge_service);
55   ~ArcInstanceThrottle() override;
56 
57   // KeyedService:
58   void Shutdown() override;
59 
set_delegate_for_testing(std::unique_ptr<Delegate> delegate)60   void set_delegate_for_testing(std::unique_ptr<Delegate> delegate) {
61     delegate_ = std::move(delegate);
62   }
63 
64  private:
65   // chromeos::ThrottleService:
66   void ThrottleInstance(
67       chromeos::ThrottleObserver::PriorityLevel level) override;
68   void RecordCpuRestrictionDisabledUMA(const std::string& observer_name,
69                                        base::TimeDelta delta) override;
70 
71   std::unique_ptr<Delegate> delegate_;
72 
73   DISALLOW_COPY_AND_ASSIGN(ArcInstanceThrottle);
74 };
75 
76 }  // namespace arc
77 
78 #endif  // CHROME_BROWSER_CHROMEOS_ARC_INSTANCE_THROTTLE_ARC_INSTANCE_THROTTLE_H_
79