Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: remoting/host/resizing_host_observer.cc

Issue 15927033: Add host-side rate-limiting to desktop resize events. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 const int kMinimumResizeIntervalMs = 1000;
Jamie 2013/06/03 23:26:28 This value is taken directly from the previous imp
alexeypa (please no reviews) 2013/06/04 00:05:26 nit #1: Add a comment explaining what this timeout
Jamie 2013/06/04 01:27:49 Done.
18
15 class CandidateSize { 19 class CandidateSize {
16 public: 20 public:
17 CandidateSize(const SkISize& candidate, const SkISize& preferred) 21 CandidateSize(const SkISize& candidate, const SkISize& preferred)
18 : size_(candidate) { 22 : size_(candidate) {
19 // Protect against division by zero. 23 // Protect against division by zero.
20 CHECK(!candidate.isEmpty()); 24 CHECK(!candidate.isEmpty());
21 DCHECK(!preferred.isEmpty()); 25 DCHECK(!preferred.isEmpty());
22 26
23 // The client scale factor is the smaller of the candidate:preferred ratios 27 // The client scale factor is the smaller of the candidate:preferred ratios
24 // for width and height. 28 // for width and height.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 SkISize size_; 98 SkISize size_;
95 }; 99 };
96 100
97 } // namespace 101 } // namespace
98 102
99 namespace remoting { 103 namespace remoting {
100 104
101 ResizingHostObserver::ResizingHostObserver( 105 ResizingHostObserver::ResizingHostObserver(
102 scoped_ptr<DesktopResizer> desktop_resizer) 106 scoped_ptr<DesktopResizer> desktop_resizer)
103 : desktop_resizer_(desktop_resizer.Pass()), 107 : desktop_resizer_(desktop_resizer.Pass()),
104 original_size_(desktop_resizer_->GetCurrentSize()) { 108 original_size_(desktop_resizer_->GetCurrentSize()),
109 resize_pending_(false),
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::TimeDelta time_since_previous_resize =
126 base::Time::Now() - previous_resize_time_;
alexeypa (please no reviews) 2013/06/04 00:05:26 nit: This looks like potential integer overflow.
Jamie 2013/06/04 01:27:49 I've rewritten it to something I think is a bit cl
127 base::TimeDelta time_until_resize_permitted =
128 base::TimeDelta::FromMilliseconds(kMinimumResizeIntervalMs) -
129 time_since_previous_resize;
130 if (time_until_resize_permitted > base::TimeDelta::FromMilliseconds(0)) {
131 pending_resolution_ = resolution;
132 if (!resize_pending_) {
133 resize_pending_ = true;
134 base::MessageLoop::current()->PostDelayedTask(
alexeypa (please no reviews) 2013/06/04 00:05:26 nit: Actually base::OneShotTimer expresses the int
Jamie 2013/06/04 01:27:49 Nice, I was wasn't familiar with that.
135 FROM_HERE,
136 base::Bind(&ResizingHostObserver::SetPendingScreenResolution,
137 weak_factory_.GetWeakPtr()),
138 time_until_resize_permitted);
139 }
140 return;
141 }
142
117 // If the implementation returns any sizes, pick the best one according to 143 // If the implementation returns any sizes, pick the best one according to
118 // the algorithm described in CandidateSize::IsBetterThen. 144 // the algorithm described in CandidateSize::IsBetterThen.
119 SkISize dimentions = SkISize::Make( 145 SkISize dimensions = SkISize::Make(
120 resolution.dimensions().width(), resolution.dimensions().height()); 146 resolution.dimensions().width(), resolution.dimensions().height());
121 std::list<SkISize> sizes = desktop_resizer_->GetSupportedSizes(dimentions); 147 std::list<SkISize> sizes = desktop_resizer_->GetSupportedSizes(dimensions);
122 if (sizes.empty()) 148 if (sizes.empty())
123 return; 149 return;
124 CandidateSize best_size(sizes.front(), dimentions); 150 CandidateSize best_size(sizes.front(), dimensions);
125 for (std::list<SkISize>::const_iterator i = ++sizes.begin(); 151 for (std::list<SkISize>::const_iterator i = ++sizes.begin();
126 i != sizes.end(); ++i) { 152 i != sizes.end(); ++i) {
127 CandidateSize candidate_size(*i, dimentions); 153 CandidateSize candidate_size(*i, dimensions);
128 if (candidate_size.IsBetterThan(best_size)) { 154 if (candidate_size.IsBetterThan(best_size)) {
129 best_size = candidate_size; 155 best_size = candidate_size;
130 } 156 }
131 } 157 }
132 SkISize current_size = desktop_resizer_->GetCurrentSize(); 158 SkISize current_size = desktop_resizer_->GetCurrentSize();
133 if (best_size.size() != current_size) 159 if (best_size.size() != current_size)
134 desktop_resizer_->SetSize(best_size.size()); 160 desktop_resizer_->SetSize(best_size.size());
161
162 // Update the time of last resize to allow it to be rate-limited.
163 previous_resize_time_ = base::Time::Now();
164 }
165
166 void ResizingHostObserver::SetPendingScreenResolution() {
167 resize_pending_ = false;
alexeypa (please no reviews) 2013/06/04 00:05:26 nit: This flag and the whole function is not neede
Jamie 2013/06/04 01:27:49 Done.
168 SetScreenResolution(pending_resolution_);
135 } 169 }
136 170
137 } // namespace remoting 171 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698