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. | |
18 const int kMinimumResizeIntervalMs = 1000; | |
19 | |
15 class CandidateSize { | 20 class CandidateSize { |
16 public: | 21 public: |
17 CandidateSize(const SkISize& candidate, const SkISize& preferred) | 22 CandidateSize(const SkISize& candidate, const SkISize& preferred) |
18 : size_(candidate) { | 23 : size_(candidate) { |
19 // Protect against division by zero. | 24 // Protect against division by zero. |
20 CHECK(!candidate.isEmpty()); | 25 CHECK(!candidate.isEmpty()); |
21 DCHECK(!preferred.isEmpty()); | 26 DCHECK(!preferred.isEmpty()); |
22 | 27 |
23 // The client scale factor is the smaller of the candidate:preferred ratios | 28 // The client scale factor is the smaller of the candidate:preferred ratios |
24 // for width and height. | 29 // for width and height. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 SkISize size_; | 99 SkISize size_; |
95 }; | 100 }; |
96 | 101 |
97 } // namespace | 102 } // namespace |
98 | 103 |
99 namespace remoting { | 104 namespace remoting { |
100 | 105 |
101 ResizingHostObserver::ResizingHostObserver( | 106 ResizingHostObserver::ResizingHostObserver( |
102 scoped_ptr<DesktopResizer> desktop_resizer) | 107 scoped_ptr<DesktopResizer> desktop_resizer) |
103 : desktop_resizer_(desktop_resizer.Pass()), | 108 : desktop_resizer_(desktop_resizer.Pass()), |
104 original_size_(desktop_resizer_->GetCurrentSize()) { | 109 original_size_(desktop_resizer_->GetCurrentSize()), |
110 weak_factory_(this) { | |
105 } | 111 } |
106 | 112 |
107 ResizingHostObserver::~ResizingHostObserver() { | 113 ResizingHostObserver::~ResizingHostObserver() { |
108 if (!original_size_.isZero()) | 114 if (!original_size_.isZero()) |
109 desktop_resizer_->RestoreSize(original_size_); | 115 desktop_resizer_->RestoreSize(original_size_); |
110 } | 116 } |
111 | 117 |
112 void ResizingHostObserver::SetScreenResolution( | 118 void ResizingHostObserver::SetScreenResolution( |
113 const ScreenResolution& resolution) { | 119 const ScreenResolution& resolution) { |
114 if (resolution.IsEmpty()) | 120 if (resolution.IsEmpty()) |
115 return; | 121 return; |
116 | 122 |
123 // Resizing the desktop too often is probably not a good idea, so apply a | |
124 // simple rate-limiting scheme. | |
125 base::Time next_allowed_resize = | |
126 previous_resize_time_ + | |
127 base::TimeDelta::FromMilliseconds(kMinimumResizeIntervalMs); | |
128 if (base::Time::Now() < next_allowed_resize) { | |
alexeypa (please no reviews)
2013/06/04 17:07:37
nit: I think |previous_resize_time_| is not really
Jamie
2013/06/04 21:18:03
That saves a bit of state, but I think it's less c
| |
129 deferred_resize_timer_.Start( | |
130 FROM_HERE, | |
131 next_allowed_resize - base::Time::Now(), | |
132 base::Bind(&ResizingHostObserver::SetScreenResolution, | |
133 weak_factory_.GetWeakPtr(), resolution)); | |
134 return; | |
135 } | |
136 | |
117 // If the implementation returns any sizes, pick the best one according to | 137 // If the implementation returns any sizes, pick the best one according to |
118 // the algorithm described in CandidateSize::IsBetterThen. | 138 // the algorithm described in CandidateSize::IsBetterThen. |
119 SkISize dimentions = SkISize::Make( | 139 SkISize dimensions = SkISize::Make( |
120 resolution.dimensions().width(), resolution.dimensions().height()); | 140 resolution.dimensions().width(), resolution.dimensions().height()); |
121 std::list<SkISize> sizes = desktop_resizer_->GetSupportedSizes(dimentions); | 141 std::list<SkISize> sizes = desktop_resizer_->GetSupportedSizes(dimensions); |
122 if (sizes.empty()) | 142 if (sizes.empty()) |
123 return; | 143 return; |
124 CandidateSize best_size(sizes.front(), dimentions); | 144 CandidateSize best_size(sizes.front(), dimensions); |
125 for (std::list<SkISize>::const_iterator i = ++sizes.begin(); | 145 for (std::list<SkISize>::const_iterator i = ++sizes.begin(); |
126 i != sizes.end(); ++i) { | 146 i != sizes.end(); ++i) { |
127 CandidateSize candidate_size(*i, dimentions); | 147 CandidateSize candidate_size(*i, dimensions); |
128 if (candidate_size.IsBetterThan(best_size)) { | 148 if (candidate_size.IsBetterThan(best_size)) { |
129 best_size = candidate_size; | 149 best_size = candidate_size; |
130 } | 150 } |
131 } | 151 } |
132 SkISize current_size = desktop_resizer_->GetCurrentSize(); | 152 SkISize current_size = desktop_resizer_->GetCurrentSize(); |
133 if (best_size.size() != current_size) | 153 if (best_size.size() != current_size) |
134 desktop_resizer_->SetSize(best_size.size()); | 154 desktop_resizer_->SetSize(best_size.size()); |
155 | |
156 // Update the time of last resize to allow it to be rate-limited. | |
157 previous_resize_time_ = base::Time::Now(); | |
135 } | 158 } |
136 | 159 |
137 } // namespace remoting | 160 } // namespace remoting |
OLD | NEW |