| 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. Note that this |
| 18 // constant is duplicated by the ResizingHostObserverTest.RateLimited |
| 19 // unit-test and must be kept in sync. |
| 20 const int kMinimumResizeIntervalMs = 1000; |
| 21 |
| 15 class CandidateSize { | 22 class CandidateSize { |
| 16 public: | 23 public: |
| 17 CandidateSize(const SkISize& candidate, const SkISize& preferred) | 24 CandidateSize(const SkISize& candidate, const SkISize& preferred) |
| 18 : size_(candidate) { | 25 : size_(candidate) { |
| 19 // Protect against division by zero. | 26 // Protect against division by zero. |
| 20 CHECK(!candidate.isEmpty()); | 27 CHECK(!candidate.isEmpty()); |
| 21 DCHECK(!preferred.isEmpty()); | 28 DCHECK(!preferred.isEmpty()); |
| 22 | 29 |
| 23 // The client scale factor is the smaller of the candidate:preferred ratios | 30 // The client scale factor is the smaller of the candidate:preferred ratios |
| 24 // for width and height. | 31 // for width and height. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 SkISize size_; | 101 SkISize size_; |
| 95 }; | 102 }; |
| 96 | 103 |
| 97 } // namespace | 104 } // namespace |
| 98 | 105 |
| 99 namespace remoting { | 106 namespace remoting { |
| 100 | 107 |
| 101 ResizingHostObserver::ResizingHostObserver( | 108 ResizingHostObserver::ResizingHostObserver( |
| 102 scoped_ptr<DesktopResizer> desktop_resizer) | 109 scoped_ptr<DesktopResizer> desktop_resizer) |
| 103 : desktop_resizer_(desktop_resizer.Pass()), | 110 : desktop_resizer_(desktop_resizer.Pass()), |
| 104 original_size_(desktop_resizer_->GetCurrentSize()) { | 111 original_size_(desktop_resizer_->GetCurrentSize()), |
| 112 now_function_(base::Bind(base::Time::Now)), |
| 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) { |
| 123 // Get the current time. This function is called exactly once for each call |
| 124 // to SetScreenResolution to simplify the implementation of unit-tests. |
| 125 base::Time now = now_function_.Run(); |
| 126 |
| 114 if (resolution.IsEmpty()) | 127 if (resolution.IsEmpty()) |
| 115 return; | 128 return; |
| 116 | 129 |
| 130 // Resizing the desktop too often is probably not a good idea, so apply a |
| 131 // simple rate-limiting scheme. |
| 132 base::TimeDelta minimum_resize_interval = |
| 133 base::TimeDelta::FromMilliseconds(kMinimumResizeIntervalMs); |
| 134 base::Time next_allowed_resize = |
| 135 previous_resize_time_ + minimum_resize_interval; |
| 136 |
| 137 if (now < next_allowed_resize) { |
| 138 deferred_resize_timer_.Start( |
| 139 FROM_HERE, |
| 140 next_allowed_resize - now, |
| 141 base::Bind(&ResizingHostObserver::SetScreenResolution, |
| 142 weak_factory_.GetWeakPtr(), resolution)); |
| 143 return; |
| 144 } |
| 145 |
| 117 // If the implementation returns any sizes, pick the best one according to | 146 // If the implementation returns any sizes, pick the best one according to |
| 118 // the algorithm described in CandidateSize::IsBetterThen. | 147 // the algorithm described in CandidateSize::IsBetterThen. |
| 119 SkISize dimentions = SkISize::Make( | 148 SkISize dimensions = SkISize::Make( |
| 120 resolution.dimensions().width(), resolution.dimensions().height()); | 149 resolution.dimensions().width(), resolution.dimensions().height()); |
| 121 std::list<SkISize> sizes = desktop_resizer_->GetSupportedSizes(dimentions); | 150 std::list<SkISize> sizes = desktop_resizer_->GetSupportedSizes(dimensions); |
| 122 if (sizes.empty()) | 151 if (sizes.empty()) |
| 123 return; | 152 return; |
| 124 CandidateSize best_size(sizes.front(), dimentions); | 153 CandidateSize best_size(sizes.front(), dimensions); |
| 125 for (std::list<SkISize>::const_iterator i = ++sizes.begin(); | 154 for (std::list<SkISize>::const_iterator i = ++sizes.begin(); |
| 126 i != sizes.end(); ++i) { | 155 i != sizes.end(); ++i) { |
| 127 CandidateSize candidate_size(*i, dimentions); | 156 CandidateSize candidate_size(*i, dimensions); |
| 128 if (candidate_size.IsBetterThan(best_size)) { | 157 if (candidate_size.IsBetterThan(best_size)) { |
| 129 best_size = candidate_size; | 158 best_size = candidate_size; |
| 130 } | 159 } |
| 131 } | 160 } |
| 132 SkISize current_size = desktop_resizer_->GetCurrentSize(); | 161 SkISize current_size = desktop_resizer_->GetCurrentSize(); |
| 133 if (best_size.size() != current_size) | 162 if (best_size.size() != current_size) |
| 134 desktop_resizer_->SetSize(best_size.size()); | 163 desktop_resizer_->SetSize(best_size.size()); |
| 164 |
| 165 // Update the time of last resize to allow it to be rate-limited. |
| 166 previous_resize_time_ = now; |
| 167 } |
| 168 |
| 169 void ResizingHostObserver::SetNowFunctionForTesting( |
| 170 const base::Callback<base::Time(void)>& now_function) { |
| 171 now_function_ = now_function; |
| 135 } | 172 } |
| 136 | 173 |
| 137 } // namespace remoting | 174 } // namespace remoting |
| OLD | NEW |