1 // Copyright (c) 2012 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 REMOTING_HOST_RESIZING_HOST_OBSERVER_H_
6 #define REMOTING_HOST_RESIZING_HOST_OBSERVER_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 
12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h"
15 #include "base/timer/timer.h"
16 #include "remoting/host/screen_controls.h"
17 #include "remoting/host/screen_resolution.h"
18 
19 namespace base {
20 class TickClock;
21 }
22 
23 namespace remoting {
24 
25 class DesktopResizer;
26 
27 // TODO(alexeypa): Rename this class to reflect that it is not
28 // HostStatusObserver any more.
29 
30 // Uses the specified DesktopResizer to match host desktop size to the client
31 // view size as closely as is possible. When the connection closes, restores
32 // the original desktop size if restore is true.
33 class ResizingHostObserver : public ScreenControls {
34  public:
35   explicit ResizingHostObserver(
36       std::unique_ptr<DesktopResizer> desktop_resizer,
37       bool restore);
38   ~ResizingHostObserver() override;
39 
40   // ScreenControls interface.
41   void SetScreenResolution(const ScreenResolution& resolution) override;
42 
43   // Provide a replacement for base::TimeTicks::Now so that this class can be
44   // unit-tested in a timely manner. This function will be called exactly
45   // once for each call to SetScreenResolution.
46   void SetClockForTesting(const base::TickClock* clock);
47 
48  private:
49   void RestoreScreenResolution();
50 
51   std::unique_ptr<DesktopResizer> desktop_resizer_;
52   ScreenResolution original_resolution_;
53   bool restore_;
54 
55   // State to manage rate-limiting of desktop resizes.
56   base::OneShotTimer deferred_resize_timer_;
57   base::TimeTicks previous_resize_time_;
58   const base::TickClock* clock_;
59 
60   base::WeakPtrFactory<ResizingHostObserver> weak_factory_{this};
61 
62   DISALLOW_COPY_AND_ASSIGN(ResizingHostObserver);
63 };
64 
65 }  // namespace remoting
66 
67 #endif  // REMOTING_HOST_RESIZING_HOST_OBSERVER_H_
68