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 |
15 class CandidateSize { | 17 class CandidateSize { |
16 public: | 18 public: |
17 CandidateSize(const SkISize& candidate, const SkISize& preferred) | 19 CandidateSize(const SkISize& candidate, const SkISize& preferred) |
18 : size_(candidate) { | 20 : size_(candidate) { |
19 // Protect against division by zero. | 21 // Protect against division by zero. |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 SkISize size_; | 96 SkISize size_; |
95 }; | 97 }; |
96 | 98 |
97 } // namespace | 99 } // namespace |
98 | 100 |
99 namespace remoting { | 101 namespace remoting { |
100 | 102 |
101 ResizingHostObserver::ResizingHostObserver( | 103 ResizingHostObserver::ResizingHostObserver( |
102 scoped_ptr<DesktopResizer> desktop_resizer) | 104 scoped_ptr<DesktopResizer> desktop_resizer) |
103 : desktop_resizer_(desktop_resizer.Pass()), | 105 : desktop_resizer_(desktop_resizer.Pass()), |
104 original_size_(desktop_resizer_->GetCurrentSize()) { | 106 original_size_(desktop_resizer_->GetCurrentSize()), |
107 now_function_(base::Bind(base::Time::Now)), | |
108 weak_factory_(this) { | |
105 } | 109 } |
106 | 110 |
107 ResizingHostObserver::~ResizingHostObserver() { | 111 ResizingHostObserver::~ResizingHostObserver() { |
108 if (!original_size_.isZero()) | 112 if (!original_size_.isZero()) |
109 desktop_resizer_->RestoreSize(original_size_); | 113 desktop_resizer_->RestoreSize(original_size_); |
110 } | 114 } |
111 | 115 |
112 void ResizingHostObserver::SetScreenResolution( | 116 void ResizingHostObserver::SetScreenResolution( |
113 const ScreenResolution& resolution) { | 117 const ScreenResolution& resolution) { |
118 // Get the current time. This function is called exactly once for each call | |
119 // to SetScreenResolution to simplify the implementation of unit-tests. | |
120 base::Time now = now_function_.Run(); | |
121 | |
114 if (resolution.IsEmpty()) | 122 if (resolution.IsEmpty()) |
115 return; | 123 return; |
116 | 124 |
125 // Resizing the desktop too often is probably not a good idea, so apply a | |
126 // simple rate-limiting scheme. | |
127 base::TimeDelta minimum_resize_interval = base::TimeDelta::FromSeconds(1); | |
alexeypa (please no reviews)
2013/06/07 19:50:25
I think it was better with kDefaultMinimumResizeIn
Jamie
2013/06/07 20:20:14
Done (without the Default, since it can't be overr
| |
128 base::Time next_allowed_resize = | |
129 previous_resize_time_ + minimum_resize_interval; | |
130 | |
131 if (now < next_allowed_resize) { | |
132 deferred_resize_timer_.Start( | |
133 FROM_HERE, | |
134 next_allowed_resize - now, | |
135 base::Bind(&ResizingHostObserver::SetScreenResolution, | |
136 weak_factory_.GetWeakPtr(), resolution)); | |
137 return; | |
138 } | |
139 | |
117 // If the implementation returns any sizes, pick the best one according to | 140 // If the implementation returns any sizes, pick the best one according to |
118 // the algorithm described in CandidateSize::IsBetterThen. | 141 // the algorithm described in CandidateSize::IsBetterThen. |
119 SkISize dimentions = SkISize::Make( | 142 SkISize dimensions = SkISize::Make( |
120 resolution.dimensions().width(), resolution.dimensions().height()); | 143 resolution.dimensions().width(), resolution.dimensions().height()); |
121 std::list<SkISize> sizes = desktop_resizer_->GetSupportedSizes(dimentions); | 144 std::list<SkISize> sizes = desktop_resizer_->GetSupportedSizes(dimensions); |
122 if (sizes.empty()) | 145 if (sizes.empty()) |
123 return; | 146 return; |
124 CandidateSize best_size(sizes.front(), dimentions); | 147 CandidateSize best_size(sizes.front(), dimensions); |
125 for (std::list<SkISize>::const_iterator i = ++sizes.begin(); | 148 for (std::list<SkISize>::const_iterator i = ++sizes.begin(); |
126 i != sizes.end(); ++i) { | 149 i != sizes.end(); ++i) { |
127 CandidateSize candidate_size(*i, dimentions); | 150 CandidateSize candidate_size(*i, dimensions); |
128 if (candidate_size.IsBetterThan(best_size)) { | 151 if (candidate_size.IsBetterThan(best_size)) { |
129 best_size = candidate_size; | 152 best_size = candidate_size; |
130 } | 153 } |
131 } | 154 } |
132 SkISize current_size = desktop_resizer_->GetCurrentSize(); | 155 SkISize current_size = desktop_resizer_->GetCurrentSize(); |
133 if (best_size.size() != current_size) | 156 if (best_size.size() != current_size) |
134 desktop_resizer_->SetSize(best_size.size()); | 157 desktop_resizer_->SetSize(best_size.size()); |
158 | |
159 // Update the time of last resize to allow it to be rate-limited. | |
160 previous_resize_time_ = now; | |
161 } | |
162 | |
163 void ResizingHostObserver::SetNowFunctionForTesting( | |
164 const base::Callback<base::Time(void)>& now_function) { | |
165 now_function_ = now_function; | |
135 } | 166 } |
136 | 167 |
137 } // namespace remoting | 168 } // namespace remoting |
OLD | NEW |