Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/chromeos/display/output_protection_delegate.h" | 5 #include "chrome/browser/chromeos/display/output_protection_delegate.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/render_frame_host.h" | 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/common/service_manager_connection.h" | |
| 13 #include "services/service_manager/public/cpp/connector.h" | |
| 14 #include "services/service_manager/runner/common/client_util.h" | |
| 15 #include "services/ui/public/interfaces/display/display_controller.mojom.h" | |
| 12 #include "ui/display/display.h" | 16 #include "ui/display/display.h" |
| 17 #include "ui/display/manager/chromeos/display_configurator.h" | |
| 13 #include "ui/display/screen.h" | 18 #include "ui/display/screen.h" |
| 19 #include "ui/display/types/display_constants.h" | |
| 14 | 20 |
| 15 namespace chromeos { | 21 namespace chromeos { |
| 16 | 22 |
| 17 namespace { | 23 namespace { |
| 18 | 24 |
| 25 bool IsRunningInMash() { | |
| 26 return service_manager::ServiceManagerIsRemote(); | |
| 27 } | |
| 28 | |
| 19 bool GetCurrentDisplayId(content::RenderFrameHost* rfh, int64_t* display_id) { | 29 bool GetCurrentDisplayId(content::RenderFrameHost* rfh, int64_t* display_id) { |
| 20 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 30 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 21 DCHECK(rfh); | 31 DCHECK(rfh); |
| 22 DCHECK(display_id); | 32 DCHECK(display_id); |
| 23 | 33 |
| 24 display::Screen* screen = display::Screen::GetScreen(); | 34 display::Screen* screen = display::Screen::GetScreen(); |
| 25 if (!screen) | 35 if (!screen) |
| 26 return false; | 36 return false; |
| 27 display::Display display = | 37 display::Display display = |
| 28 screen->GetDisplayNearestWindow(rfh->GetNativeView()); | 38 screen->GetDisplayNearestWindow(rfh->GetNativeView()); |
| 29 *display_id = display.id(); | 39 *display_id = display.id(); |
| 30 return true; | 40 return true; |
| 31 } | 41 } |
| 32 | 42 |
| 33 void DoNothing(bool status) { | 43 void DoNothing(bool status) { |
| 34 } | 44 } |
| 35 | 45 |
| 46 // Display content protection controller for running on Aura. | |
| 47 class ControllerAura : public OutputProtectionDelegate::Controller { | |
| 48 public: | |
| 49 ControllerAura() {} | |
| 50 | |
| 51 ~ControllerAura() override { | |
| 52 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 53 if (client_id_ == display::DisplayConfigurator::kInvalidClientId) | |
| 54 return; | |
| 55 display::DisplayConfigurator* configurator = | |
| 56 ash::Shell::GetInstance()->display_configurator(); | |
| 57 configurator->UnregisterContentProtectionClient(client_id_); | |
| 58 } | |
| 59 | |
| 60 bool Init() override { | |
| 61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 62 display::DisplayConfigurator* configurator = | |
| 63 ash::Shell::GetInstance()->display_configurator(); | |
| 64 client_id_ = configurator->RegisterContentProtectionClient(); | |
| 65 return client_id_ != display::DisplayConfigurator::kInvalidClientId; | |
| 66 } | |
| 67 | |
| 68 void QueryStatus( | |
| 69 int64_t display_id, | |
| 70 const OutputProtectionDelegate::QueryStatusCallback& callback) override { | |
| 71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 72 display::DisplayConfigurator* configurator = | |
| 73 ash::Shell::GetInstance()->display_configurator(); | |
| 74 configurator->QueryContentProtectionStatus(client_id_, display_id, | |
| 75 callback); | |
| 76 } | |
| 77 | |
| 78 void EnableProtection( | |
| 79 int64_t display_id, | |
| 80 uint32_t desired_method_mask, | |
| 81 const OutputProtectionDelegate::EnableProtectionCallback& callback) | |
| 82 override { | |
| 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 84 display::DisplayConfigurator* configurator = | |
| 85 ash::Shell::GetInstance()->display_configurator(); | |
| 86 configurator->EnableContentProtection(client_id_, display_id, | |
| 87 desired_method_mask, callback); | |
| 88 } | |
| 89 | |
| 90 private: | |
| 91 display::DisplayConfigurator::ContentProtectionClientId client_id_ = | |
| 92 display::DisplayConfigurator::kInvalidClientId; | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(ControllerAura); | |
| 95 }; | |
| 96 | |
| 97 // Display content protection controller for running with Mus server. | |
| 98 class ControllerMus : public OutputProtectionDelegate::Controller { | |
| 99 public: | |
| 100 ControllerMus() {} | |
| 101 | |
| 102 ~ControllerMus() override { | |
| 103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 104 if (client_id_ == display::DisplayConfigurator::kInvalidClientId) | |
| 105 return; | |
| 106 display_controller_->UnregisterContentProtectionClient(client_id_); | |
| 107 } | |
| 108 | |
| 109 bool Init() override { | |
| 110 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 111 content::ServiceManagerConnection::GetForProcess() | |
| 112 ->GetConnector() | |
| 113 ->BindInterface("ui", &display_controller_); | |
| 114 return display_controller_->RegisterContentProtectionClient(&client_id_) && | |
| 115 client_id_ != display::DisplayConfigurator::kInvalidClientId; | |
| 116 } | |
| 117 | |
| 118 void QueryStatus( | |
| 119 int64_t display_id, | |
| 120 const OutputProtectionDelegate::QueryStatusCallback& callback) override { | |
| 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 122 display_controller_->QueryContentProtectionStatus(client_id_, display_id, | |
| 123 callback); | |
| 124 } | |
| 125 | |
| 126 void EnableProtection( | |
| 127 int64_t display_id, | |
| 128 uint32_t desired_method_mask, | |
| 129 const OutputProtectionDelegate::EnableProtectionCallback& callback) | |
| 130 override { | |
| 131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 132 display_controller_->EnableContentProtection(client_id_, display_id, | |
| 133 desired_method_mask, callback); | |
| 134 } | |
| 135 | |
| 136 private: | |
| 137 display::mojom::DisplayControllerPtr display_controller_; | |
| 138 display::DisplayConfigurator::ContentProtectionClientId client_id_ = | |
| 139 display::DisplayConfigurator::kInvalidClientId; | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(ControllerMus); | |
| 142 }; | |
| 143 | |
| 36 } // namespace | 144 } // namespace |
| 37 | 145 |
| 146 OutputProtectionDelegate::Controller::Controller() {} | |
| 147 | |
| 148 OutputProtectionDelegate::Controller::~Controller() {} | |
| 149 | |
| 38 OutputProtectionDelegate::OutputProtectionDelegate(int render_process_id, | 150 OutputProtectionDelegate::OutputProtectionDelegate(int render_process_id, |
| 39 int render_frame_id) | 151 int render_frame_id) |
| 40 : render_process_id_(render_process_id), | 152 : render_process_id_(render_process_id), |
| 41 render_frame_id_(render_frame_id), | 153 render_frame_id_(render_frame_id), |
| 42 window_(nullptr), | 154 window_(nullptr), |
| 43 client_id_(display::DisplayConfigurator::kInvalidClientId), | 155 display_id_(display::kInvalidDisplayId), |
| 44 display_id_(0), | |
| 45 weak_ptr_factory_(this) { | 156 weak_ptr_factory_(this) { |
| 46 // This can be constructed on IO or UI thread. | 157 // This can be constructed on IO or UI thread. |
| 47 } | 158 } |
| 48 | 159 |
| 49 OutputProtectionDelegate::~OutputProtectionDelegate() { | 160 OutputProtectionDelegate::~OutputProtectionDelegate() { |
| 50 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 161 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 51 | 162 controller_.reset(); |
| 52 display::DisplayConfigurator* configurator = | |
| 53 ash::Shell::GetInstance()->display_configurator(); | |
| 54 configurator->UnregisterContentProtectionClient(client_id_); | |
| 55 | |
| 56 if (window_) | 163 if (window_) |
| 57 window_->RemoveObserver(this); | 164 window_->RemoveObserver(this); |
| 58 } | 165 } |
| 59 | 166 |
| 60 display::DisplayConfigurator::ContentProtectionClientId | 167 bool OutputProtectionDelegate::InitializeControllerIfNecessary() { |
| 61 OutputProtectionDelegate::GetClientId() { | |
| 62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 168 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 63 if (client_id_ == display::DisplayConfigurator::kInvalidClientId) { | 169 if (controller_) |
| 64 content::RenderFrameHost* rfh = | 170 return true; |
| 65 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); | |
| 66 if (!rfh || !GetCurrentDisplayId(rfh, &display_id_)) | |
| 67 return display::DisplayConfigurator::kInvalidClientId; | |
| 68 | 171 |
| 69 aura::Window* window = rfh->GetNativeView(); | 172 content::RenderFrameHost* rfh = |
| 70 if (!window) | 173 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); |
| 71 return display::DisplayConfigurator::kInvalidClientId; | 174 if (!rfh || !GetCurrentDisplayId(rfh, &display_id_)) |
| 175 return false; | |
| 72 | 176 |
| 73 display::DisplayConfigurator* configurator = | 177 aura::Window* window = rfh->GetNativeView(); |
| 74 ash::Shell::GetInstance()->display_configurator(); | 178 if (!window) { |
| 75 client_id_ = configurator->RegisterContentProtectionClient(); | 179 display_id_ = display::kInvalidDisplayId; |
| 180 return false; | |
| 181 } | |
| 76 | 182 |
| 77 if (client_id_ != display::DisplayConfigurator::kInvalidClientId) { | 183 if (IsRunningInMash()) |
| 78 window->AddObserver(this); | 184 controller_ = base::MakeUnique<ControllerMus>(); |
| 79 window_ = window; | 185 else |
| 80 } | 186 controller_ = base::MakeUnique<ControllerAura>(); |
| 187 if (!controller_->Init()) { | |
| 188 display_id_ = display::kInvalidDisplayId; | |
| 189 controller_.reset(); | |
| 190 return false; | |
| 81 } | 191 } |
| 82 return client_id_; | 192 |
| 193 window->AddObserver(this); | |
| 194 window_ = window; | |
| 195 return true; | |
| 83 } | 196 } |
| 84 | 197 |
| 85 void OutputProtectionDelegate::QueryStatus( | 198 void OutputProtectionDelegate::QueryStatus( |
| 86 const QueryStatusCallback& callback) { | 199 const QueryStatusCallback& callback) { |
| 87 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 200 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 88 | 201 |
| 89 content::RenderFrameHost* rfh = | 202 content::RenderFrameHost* rfh = |
| 90 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); | 203 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); |
| 91 if (!rfh) { | 204 if (!rfh) { |
| 92 LOG(WARNING) << "RenderFrameHost is not alive."; | 205 LOG(WARNING) << "RenderFrameHost is not alive."; |
| 93 callback.Run(false, 0, 0); | 206 callback.Run(false, 0, 0); |
| 94 return; | 207 return; |
| 95 } | 208 } |
| 96 | 209 |
| 97 display::DisplayConfigurator* configurator = | 210 if (!InitializeControllerIfNecessary()) { |
| 98 ash::Shell::GetInstance()->display_configurator(); | 211 callback.Run(false, 0, 0); |
| 99 configurator->QueryContentProtectionStatus( | 212 return; |
| 100 GetClientId(), display_id_, | 213 } |
| 101 base::Bind(&OutputProtectionDelegate::QueryStatusComplete, | 214 controller_->QueryStatus(display_id_, callback); |
| 102 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 103 } | 215 } |
| 104 | 216 |
| 105 void OutputProtectionDelegate::EnableProtection( | 217 void OutputProtectionDelegate::EnableProtection( |
| 106 uint32_t desired_method_mask, | 218 uint32_t desired_method_mask, |
| 107 const EnableProtectionCallback& callback) { | 219 const EnableProtectionCallback& callback) { |
| 108 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 220 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 109 | 221 if (!InitializeControllerIfNecessary()) { |
| 110 display::DisplayConfigurator* configurator = | 222 callback.Run(false); |
| 111 ash::Shell::GetInstance()->display_configurator(); | 223 return; |
| 112 configurator->EnableContentProtection( | 224 } |
| 113 GetClientId(), display_id_, desired_method_mask, | 225 controller_->EnableProtection(display_id_, desired_method_mask, callback); |
| 114 base::Bind(&OutputProtectionDelegate::EnableProtectionComplete, | |
| 115 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 116 desired_method_mask_ = desired_method_mask; | 226 desired_method_mask_ = desired_method_mask; |
| 117 } | 227 } |
| 118 | 228 |
| 119 void OutputProtectionDelegate::QueryStatusComplete( | 229 void OutputProtectionDelegate::QueryStatusComplete( |
| 120 const QueryStatusCallback& callback, | 230 const QueryStatusCallback& callback, |
| 121 const display::DisplayConfigurator::QueryProtectionResponse& response) { | 231 bool success, |
| 232 uint32_t link_mask, | |
| 233 uint32_t protection_mask) { | |
| 122 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 234 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 123 | 235 |
| 124 content::RenderFrameHost* rfh = | 236 content::RenderFrameHost* rfh = |
| 125 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); | 237 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); |
| 126 // TODO(xjz): Investigate whether this check (and the other one above) should | 238 // TODO(xjz): Investigate whether this check (and the other one above) should |
| 127 // be removed. | 239 // be removed. |
| 128 if (!rfh) { | 240 if (!rfh) { |
| 129 LOG(WARNING) << "RenderFrameHost is not alive."; | 241 LOG(WARNING) << "RenderFrameHost is not alive."; |
| 130 callback.Run(false, 0, 0); | 242 callback.Run(false, 0, 0); |
| 131 return; | 243 return; |
| 132 } | 244 } |
| 133 | 245 callback.Run(success, link_mask, protection_mask); |
| 134 callback.Run(response.success, response.link_mask, response.protection_mask); | |
| 135 } | 246 } |
| 136 | 247 |
| 137 void OutputProtectionDelegate::EnableProtectionComplete( | 248 void OutputProtectionDelegate::EnableProtectionComplete( |
| 138 const EnableProtectionCallback& callback, | 249 const EnableProtectionCallback& callback, |
| 139 bool success) { | 250 bool success) { |
| 140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 251 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 141 | 252 |
| 142 callback.Run(success); | 253 callback.Run(success); |
| 143 } | 254 } |
| 144 | 255 |
| 145 void OutputProtectionDelegate::OnWindowHierarchyChanged( | 256 void OutputProtectionDelegate::OnWindowHierarchyChanged( |
| 146 const aura::WindowObserver::HierarchyChangeParams& params) { | 257 const aura::WindowObserver::HierarchyChangeParams& params) { |
| 147 content::RenderFrameHost* rfh = | 258 content::RenderFrameHost* rfh = |
| 148 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); | 259 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); |
| 149 if (!rfh) { | 260 if (!rfh) { |
| 150 LOG(WARNING) << "RenderFrameHost is not alive."; | 261 LOG(WARNING) << "RenderFrameHost is not alive."; |
| 151 return; | 262 return; |
| 152 } | 263 } |
| 153 | 264 |
| 154 int64_t new_display_id = 0; | 265 int64_t new_display_id = display::kInvalidDisplayId; |
| 155 if (!GetCurrentDisplayId(rfh, &new_display_id)) | 266 if (!GetCurrentDisplayId(rfh, &new_display_id)) |
| 156 return; | 267 return; |
| 157 if (display_id_ == new_display_id) | 268 if (display_id_ == new_display_id) |
| 158 return; | 269 return; |
| 159 | 270 |
| 160 if (desired_method_mask_ != display::CONTENT_PROTECTION_METHOD_NONE) { | 271 if (desired_method_mask_ != display::CONTENT_PROTECTION_METHOD_NONE) { |
| 161 // Display changed and should enable output protections on new display. | 272 bool result = InitializeControllerIfNecessary(); |
| 162 display::DisplayConfigurator* configurator = | 273 DCHECK(result); |
| 163 ash::Shell::GetInstance()->display_configurator(); | 274 controller_->EnableProtection(display_id_, desired_method_mask_, |
|
kcwu
2017/02/08 23:06:02
should be new_display_id.
Peng
2017/02/08 23:13:35
Done
| |
| 164 configurator->EnableContentProtection(GetClientId(), new_display_id, | 275 base::Bind(&DoNothing)); |
| 165 desired_method_mask_, | 276 controller_->EnableProtection(display_id_, |
| 166 base::Bind(&DoNothing)); | 277 display::CONTENT_PROTECTION_METHOD_NONE, |
| 167 configurator->EnableContentProtection( | 278 base::Bind(&DoNothing)); |
| 168 GetClientId(), display_id_, display::CONTENT_PROTECTION_METHOD_NONE, | |
| 169 base::Bind(&DoNothing)); | |
| 170 } | 279 } |
| 171 display_id_ = new_display_id; | 280 display_id_ = new_display_id; |
| 172 } | 281 } |
| 173 | 282 |
| 174 void OutputProtectionDelegate::OnWindowDestroying(aura::Window* window) { | 283 void OutputProtectionDelegate::OnWindowDestroying(aura::Window* window) { |
| 175 DCHECK_EQ(window, window_); | 284 DCHECK_EQ(window, window_); |
| 176 window_->RemoveObserver(this); | 285 window_->RemoveObserver(this); |
| 177 window_ = nullptr; | 286 window_ = nullptr; |
| 178 } | 287 } |
| 179 | 288 |
| 180 } // namespace chromeos | 289 } // namespace chromeos |
| OLD | NEW |