OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/resizing_host_observer.h" | 5 #include "remoting/host/resizing_host_observer.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
| 9 #include "base/bind.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" |
10 #include "remoting/host/desktop_resizer.h" | 12 #include "remoting/host/desktop_resizer.h" |
11 #include "remoting/host/screen_resolution.h" | 13 #include "remoting/host/screen_resolution.h" |
12 | 14 |
13 namespace { | 15 namespace { |
14 | 16 |
| 17 // Minimum amount of time to wait between desktop resizes. Can be overridden |
| 18 // for testing. |
| 19 const int kDefaultMinimumResizeIntervalMs = 1000; |
| 20 |
15 class CandidateSize { | 21 class CandidateSize { |
16 public: | 22 public: |
17 CandidateSize(const SkISize& candidate, const SkISize& preferred) | 23 CandidateSize(const SkISize& candidate, const SkISize& preferred) |
18 : size_(candidate) { | 24 : size_(candidate) { |
19 // Protect against division by zero. | 25 // Protect against division by zero. |
20 CHECK(!candidate.isEmpty()); | 26 CHECK(!candidate.isEmpty()); |
21 DCHECK(!preferred.isEmpty()); | 27 DCHECK(!preferred.isEmpty()); |
22 | 28 |
23 // The client scale factor is the smaller of the candidate:preferred ratios | 29 // The client scale factor is the smaller of the candidate:preferred ratios |
24 // for width and height. | 30 // for width and height. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 SkISize size_; | 100 SkISize size_; |
95 }; | 101 }; |
96 | 102 |
97 } // namespace | 103 } // namespace |
98 | 104 |
99 namespace remoting { | 105 namespace remoting { |
100 | 106 |
101 ResizingHostObserver::ResizingHostObserver( | 107 ResizingHostObserver::ResizingHostObserver( |
102 scoped_ptr<DesktopResizer> desktop_resizer) | 108 scoped_ptr<DesktopResizer> desktop_resizer) |
103 : desktop_resizer_(desktop_resizer.Pass()), | 109 : desktop_resizer_(desktop_resizer.Pass()), |
104 original_size_(desktop_resizer_->GetCurrentSize()) { | 110 original_size_(desktop_resizer_->GetCurrentSize()), |
| 111 minimum_resize_interval_( |
| 112 base::TimeDelta::FromMilliseconds(kDefaultMinimumResizeIntervalMs)), |
| 113 weak_factory_(this) { |
105 } | 114 } |
106 | 115 |
107 ResizingHostObserver::~ResizingHostObserver() { | 116 ResizingHostObserver::~ResizingHostObserver() { |
108 if (!original_size_.isZero()) | 117 if (!original_size_.isZero()) |
109 desktop_resizer_->RestoreSize(original_size_); | 118 desktop_resizer_->RestoreSize(original_size_); |
110 } | 119 } |
111 | 120 |
112 void ResizingHostObserver::SetScreenResolution( | 121 void ResizingHostObserver::SetScreenResolution( |
113 const ScreenResolution& resolution) { | 122 const ScreenResolution& resolution) { |
114 if (resolution.IsEmpty()) | 123 if (resolution.IsEmpty()) |
115 return; | 124 return; |
116 | 125 |
| 126 // Resizing the desktop too often is probably not a good idea, so apply a |
| 127 // simple rate-limiting scheme. |
| 128 base::Time next_allowed_resize = |
| 129 previous_resize_time_ + minimum_resize_interval_; |
| 130 if (base::Time::Now() < next_allowed_resize) { |
| 131 deferred_resize_timer_.Start( |
| 132 FROM_HERE, |
| 133 next_allowed_resize - base::Time::Now(), |
| 134 base::Bind(&ResizingHostObserver::SetScreenResolution, |
| 135 weak_factory_.GetWeakPtr(), resolution)); |
| 136 return; |
| 137 } |
| 138 |
117 // If the implementation returns any sizes, pick the best one according to | 139 // If the implementation returns any sizes, pick the best one according to |
118 // the algorithm described in CandidateSize::IsBetterThen. | 140 // the algorithm described in CandidateSize::IsBetterThen. |
119 SkISize dimentions = SkISize::Make( | 141 SkISize dimensions = SkISize::Make( |
120 resolution.dimensions().width(), resolution.dimensions().height()); | 142 resolution.dimensions().width(), resolution.dimensions().height()); |
121 std::list<SkISize> sizes = desktop_resizer_->GetSupportedSizes(dimentions); | 143 std::list<SkISize> sizes = desktop_resizer_->GetSupportedSizes(dimensions); |
122 if (sizes.empty()) | 144 if (sizes.empty()) |
123 return; | 145 return; |
124 CandidateSize best_size(sizes.front(), dimentions); | 146 CandidateSize best_size(sizes.front(), dimensions); |
125 for (std::list<SkISize>::const_iterator i = ++sizes.begin(); | 147 for (std::list<SkISize>::const_iterator i = ++sizes.begin(); |
126 i != sizes.end(); ++i) { | 148 i != sizes.end(); ++i) { |
127 CandidateSize candidate_size(*i, dimentions); | 149 CandidateSize candidate_size(*i, dimensions); |
128 if (candidate_size.IsBetterThan(best_size)) { | 150 if (candidate_size.IsBetterThan(best_size)) { |
129 best_size = candidate_size; | 151 best_size = candidate_size; |
130 } | 152 } |
131 } | 153 } |
132 SkISize current_size = desktop_resizer_->GetCurrentSize(); | 154 SkISize current_size = desktop_resizer_->GetCurrentSize(); |
133 if (best_size.size() != current_size) | 155 if (best_size.size() != current_size) |
134 desktop_resizer_->SetSize(best_size.size()); | 156 desktop_resizer_->SetSize(best_size.size()); |
| 157 |
| 158 // Update the time of last resize to allow it to be rate-limited. |
| 159 previous_resize_time_ = base::Time::Now(); |
| 160 } |
| 161 |
| 162 void ResizingHostObserver::SetMinimumResizeIntervalForTesting( |
| 163 const base::TimeDelta& interval) { |
| 164 minimum_resize_interval_ = interval; |
135 } | 165 } |
136 | 166 |
137 } // namespace remoting | 167 } // namespace remoting |
OLD | NEW |