| 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 <set> | 7 #include <list> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "remoting/host/desktop_resizer.h" | 10 #include "remoting/host/desktop_resizer.h" |
| 11 #include "remoting/host/host_status_monitor.h" | |
| 12 | 11 |
| 13 namespace remoting { | 12 namespace { |
| 14 | |
| 15 ResizingHostObserver::ResizingHostObserver( | |
| 16 DesktopResizer* desktop_resizer, | |
| 17 base::WeakPtr<HostStatusMonitor> monitor) | |
| 18 : desktop_resizer_(desktop_resizer), | |
| 19 monitor_(monitor), | |
| 20 original_size_(SkISize::Make(0, 0)) { | |
| 21 monitor_->AddStatusObserver(this); | |
| 22 } | |
| 23 | |
| 24 ResizingHostObserver::~ResizingHostObserver() { | |
| 25 if (monitor_) | |
| 26 monitor_->RemoveStatusObserver(this); | |
| 27 } | |
| 28 | |
| 29 void ResizingHostObserver::OnClientAuthenticated(const std::string& jid) { | |
| 30 // This implementation assumes a single connected client, which is what the | |
| 31 // host currently supports | |
| 32 DCHECK(client_jid_.empty()); | |
| 33 original_size_ = desktop_resizer_->GetCurrentSize(); | |
| 34 } | |
| 35 | |
| 36 void ResizingHostObserver::OnClientDisconnected(const std::string& jid) { | |
| 37 if (!original_size_.isZero()) { | |
| 38 desktop_resizer_->RestoreSize(original_size_); | |
| 39 original_size_.set(0, 0); | |
| 40 } | |
| 41 client_jid_.clear(); | |
| 42 } | |
| 43 | 13 |
| 44 class CandidateSize { | 14 class CandidateSize { |
| 45 public: | 15 public: |
| 46 CandidateSize(const SkISize& candidate, const SkISize& preferred) | 16 CandidateSize(const SkISize& candidate, const SkISize& preferred) |
| 47 : size_(candidate) { | 17 : size_(candidate) { |
| 48 // Protect against division by zero. | 18 // Protect against division by zero. |
| 49 CHECK(!candidate.isEmpty()); | 19 CHECK(!candidate.isEmpty()); |
| 50 DCHECK(!preferred.isEmpty()); | 20 DCHECK(!preferred.isEmpty()); |
| 51 | 21 |
| 52 // The client scale factor is the smaller of the candidate:preferred ratios | 22 // The client scale factor is the smaller of the candidate:preferred ratios |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 // are typically designed for landscape aspect ratios. | 86 // are typically designed for landscape aspect ratios. |
| 117 return size().width() > other.size().width(); | 87 return size().width() > other.size().width(); |
| 118 } | 88 } |
| 119 | 89 |
| 120 private: | 90 private: |
| 121 float client_scale_factor_; | 91 float client_scale_factor_; |
| 122 float aspect_ratio_goodness_; | 92 float aspect_ratio_goodness_; |
| 123 SkISize size_; | 93 SkISize size_; |
| 124 }; | 94 }; |
| 125 | 95 |
| 96 } // namespace |
| 97 |
| 98 namespace remoting { |
| 99 |
| 100 ResizingHostObserver::ResizingHostObserver( |
| 101 scoped_ptr<DesktopResizer> desktop_resizer) |
| 102 : desktop_resizer_(desktop_resizer.Pass()), |
| 103 original_size_(desktop_resizer_->GetCurrentSize()) { |
| 104 } |
| 105 |
| 106 ResizingHostObserver::~ResizingHostObserver() { |
| 107 if (!original_size_.isZero()) |
| 108 desktop_resizer_->RestoreSize(original_size_); |
| 109 } |
| 110 |
| 126 void ResizingHostObserver::OnClientResolutionChanged( | 111 void ResizingHostObserver::OnClientResolutionChanged( |
| 127 const std::string& jid, | 112 const SkIPoint& client_dpi, |
| 128 const SkISize& preferred_size, | 113 const SkISize& client_size) { |
| 129 const SkIPoint& dpi) { | 114 if (client_size.isEmpty()) { |
| 130 if (preferred_size.isEmpty()) { | |
| 131 return; | 115 return; |
| 132 } | 116 } |
| 133 | 117 |
| 134 // If the implementation returns any sizes, pick the best one according to | 118 // If the implementation returns any sizes, pick the best one according to |
| 135 // the algorithm described in CandidateSize::IsBetterThen. | 119 // the algorithm described in CandidateSize::IsBetterThen. |
| 136 std::list<SkISize> sizes = | 120 std::list<SkISize> sizes = |
| 137 desktop_resizer_->GetSupportedSizes(preferred_size); | 121 desktop_resizer_->GetSupportedSizes(client_size); |
| 138 if (sizes.empty()) { | 122 if (sizes.empty()) { |
| 139 return; | 123 return; |
| 140 } | 124 } |
| 141 CandidateSize best_size(sizes.front(), preferred_size); | 125 CandidateSize best_size(sizes.front(), client_size); |
| 142 for (std::list<SkISize>::const_iterator i = ++sizes.begin(); | 126 for (std::list<SkISize>::const_iterator i = ++sizes.begin(); |
| 143 i != sizes.end(); ++i) { | 127 i != sizes.end(); ++i) { |
| 144 CandidateSize candidate_size(*i, preferred_size); | 128 CandidateSize candidate_size(*i, client_size); |
| 145 if (candidate_size.IsBetterThan(best_size)) { | 129 if (candidate_size.IsBetterThan(best_size)) { |
| 146 best_size = candidate_size; | 130 best_size = candidate_size; |
| 147 } | 131 } |
| 148 } | 132 } |
| 149 SkISize current_size = desktop_resizer_->GetCurrentSize(); | 133 SkISize current_size = desktop_resizer_->GetCurrentSize(); |
| 150 if (best_size.size() != current_size) | 134 if (best_size.size() != current_size) |
| 151 desktop_resizer_->SetSize(best_size.size()); | 135 desktop_resizer_->SetSize(best_size.size()); |
| 152 } | 136 } |
| 153 | 137 |
| 154 } // namespace remoting | 138 } // namespace remoting |
| OLD | NEW |