OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "content/browser/webrtc/webrtc_internals.h" | 5 #include "content/browser/webrtc/webrtc_internals.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "content/browser/renderer_host/render_process_host_impl.h" | 14 #include "content/browser/renderer_host/render_process_host_impl.h" |
15 #include "content/browser/web_contents/web_contents_view.h" | 15 #include "content/browser/web_contents/web_contents_view.h" |
16 #include "content/browser/webrtc/webrtc_internals_ui_observer.h" | 16 #include "content/browser/webrtc/webrtc_internals_ui_observer.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/content_browser_client.h" | 18 #include "content/public/browser/content_browser_client.h" |
19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
20 #include "device/power_save_blocker/power_save_blocker.h" | 20 #include "content/public/common/service_manager_connection.h" |
| 21 #include "device/wake_lock/public/interfaces/wake_lock_provider.mojom.h" |
21 #include "ipc/ipc_platform_file.h" | 22 #include "ipc/ipc_platform_file.h" |
22 #include "media/audio/audio_manager.h" | 23 #include "media/audio/audio_manager.h" |
23 #include "media/media_features.h" | 24 #include "media/media_features.h" |
| 25 #include "services/device/public/interfaces/constants.mojom.h" |
| 26 #include "services/service_manager/public/cpp/connector.h" |
24 | 27 |
25 #if defined(OS_WIN) | 28 #if defined(OS_WIN) |
26 #define IntToStringType base::IntToString16 | 29 #define IntToStringType base::IntToString16 |
27 #else | 30 #else |
28 #define IntToStringType base::IntToString | 31 #define IntToStringType base::IntToString |
29 #endif | 32 #endif |
30 | 33 |
31 using base::ProcessId; | 34 using base::ProcessId; |
32 using std::string; | 35 using std::string; |
33 | 36 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 dict->SetString("rtcConfiguration", rtc_configuration); | 142 dict->SetString("rtcConfiguration", rtc_configuration); |
140 dict->SetString("constraints", constraints); | 143 dict->SetString("constraints", constraints); |
141 dict->SetString("url", url); | 144 dict->SetString("url", url); |
142 dict->SetBoolean("isOpen", true); | 145 dict->SetBoolean("isOpen", true); |
143 | 146 |
144 if (observers_.might_have_observers()) | 147 if (observers_.might_have_observers()) |
145 SendUpdate("addPeerConnection", dict->CreateDeepCopy()); | 148 SendUpdate("addPeerConnection", dict->CreateDeepCopy()); |
146 | 149 |
147 peer_connection_data_.Append(std::move(dict)); | 150 peer_connection_data_.Append(std::move(dict)); |
148 ++num_open_connections_; | 151 ++num_open_connections_; |
149 CreateOrReleasePowerSaveBlocker(); | 152 UpdateWakeLock(); |
150 | 153 |
151 if (render_process_id_set_.insert(render_process_id).second) { | 154 if (render_process_id_set_.insert(render_process_id).second) { |
152 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); | 155 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); |
153 if (host) | 156 if (host) |
154 host->AddObserver(this); | 157 host->AddObserver(this); |
155 } | 158 } |
156 } | 159 } |
157 | 160 |
158 void WebRTCInternals::OnRemovePeerConnection(ProcessId pid, int lid) { | 161 void WebRTCInternals::OnRemovePeerConnection(ProcessId pid, int lid) { |
159 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 162 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 std::unique_ptr<base::DictionaryValue> update( | 460 std::unique_ptr<base::DictionaryValue> update( |
458 new base::DictionaryValue()); | 461 new base::DictionaryValue()); |
459 update->SetInteger("lid", lid); | 462 update->SetInteger("lid", lid); |
460 update->SetInteger("pid", pid); | 463 update->SetInteger("pid", pid); |
461 SendUpdate("removePeerConnection", std::move(update)); | 464 SendUpdate("removePeerConnection", std::move(update)); |
462 } | 465 } |
463 MaybeClosePeerConnection(record); | 466 MaybeClosePeerConnection(record); |
464 peer_connection_data_.Remove(i, NULL); | 467 peer_connection_data_.Remove(i, NULL); |
465 } | 468 } |
466 } | 469 } |
467 CreateOrReleasePowerSaveBlocker(); | 470 UpdateWakeLock(); |
468 | 471 |
469 bool found_any = false; | 472 bool found_any = false; |
470 // Iterates from the end of the list to remove the getUserMedia requests | 473 // Iterates from the end of the list to remove the getUserMedia requests |
471 // created by the exiting renderer. | 474 // created by the exiting renderer. |
472 for (int i = get_user_media_requests_.GetSize() - 1; i >= 0; --i) { | 475 for (int i = get_user_media_requests_.GetSize() - 1; i >= 0; --i) { |
473 base::DictionaryValue* record = NULL; | 476 base::DictionaryValue* record = NULL; |
474 get_user_media_requests_.GetDictionary(i, &record); | 477 get_user_media_requests_.GetDictionary(i, &record); |
475 | 478 |
476 int this_rid = 0; | 479 int this_rid = 0; |
477 record->GetInteger("rid", &this_rid); | 480 record->GetInteger("rid", &this_rid); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 void WebRTCInternals::MaybeClosePeerConnection(base::DictionaryValue* record) { | 530 void WebRTCInternals::MaybeClosePeerConnection(base::DictionaryValue* record) { |
528 bool is_open; | 531 bool is_open; |
529 bool did_read = record->GetBoolean("isOpen", &is_open); | 532 bool did_read = record->GetBoolean("isOpen", &is_open); |
530 DCHECK(did_read); | 533 DCHECK(did_read); |
531 if (!is_open) | 534 if (!is_open) |
532 return; | 535 return; |
533 | 536 |
534 record->SetBoolean("isOpen", false); | 537 record->SetBoolean("isOpen", false); |
535 --num_open_connections_; | 538 --num_open_connections_; |
536 DCHECK_GE(num_open_connections_, 0); | 539 DCHECK_GE(num_open_connections_, 0); |
537 CreateOrReleasePowerSaveBlocker(); | 540 UpdateWakeLock(); |
538 } | 541 } |
539 | 542 |
540 void WebRTCInternals::CreateOrReleasePowerSaveBlocker() { | 543 void WebRTCInternals::UpdateWakeLock() { |
541 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 544 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
542 if (!should_block_power_saving_) | 545 if (!should_block_power_saving_) |
543 return; | 546 return; |
544 | 547 |
545 if (num_open_connections_ == 0 && power_save_blocker_) { | 548 if (num_open_connections_ == 0) { |
546 DVLOG(1) << ("Releasing the block on application suspension since no " | 549 DVLOG(1) |
547 "PeerConnections are active anymore."); | 550 << ("Cancel the wake lock on application suspension since no " |
548 power_save_blocker_.reset(); | 551 "PeerConnections are active anymore."); |
549 } else if (num_open_connections_ != 0 && !power_save_blocker_) { | 552 GetWakeLockService()->CancelWakeLock(); |
| 553 } else if (num_open_connections_ != 0) { |
550 DVLOG(1) << ("Preventing the application from being suspended while one or " | 554 DVLOG(1) << ("Preventing the application from being suspended while one or " |
551 "more PeerConnections are active."); | 555 "more PeerConnections are active."); |
552 power_save_blocker_.reset(new device::PowerSaveBlocker( | 556 GetWakeLockService()->RequestWakeLock(); |
553 device::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | |
554 device::PowerSaveBlocker::kReasonOther, | |
555 "WebRTC has active PeerConnections", | |
556 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI), | |
557 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE))); | |
558 } | 557 } |
559 } | 558 } |
560 | 559 |
| 560 device::mojom::WakeLockService* WebRTCInternals::GetWakeLockService() { |
| 561 // Here is a lazy binding, and will not reconnect after connection error. |
| 562 if (!wake_lock_) { |
| 563 device::mojom::WakeLockServiceRequest request = |
| 564 mojo::MakeRequest(&wake_lock_); |
| 565 // In some testing contexts, the service manager connection isn't |
| 566 // initialized. |
| 567 if (ServiceManagerConnection::GetForProcess()) { |
| 568 service_manager::Connector* connector = |
| 569 ServiceManagerConnection::GetForProcess()->GetConnector(); |
| 570 DCHECK(connector); |
| 571 device::mojom::WakeLockProviderPtr wake_lock_provider; |
| 572 connector->BindInterface(device::mojom::kServiceName, |
| 573 mojo::MakeRequest(&wake_lock_provider)); |
| 574 wake_lock_provider->GetWakeLockWithoutContext( |
| 575 device::mojom::WakeLockType::PreventAppSuspension, |
| 576 device::mojom::WakeLockReason::ReasonOther, |
| 577 "WebRTC has active PeerConnections", std::move(request)); |
| 578 } |
| 579 } |
| 580 return wake_lock_.get(); |
| 581 } |
| 582 |
561 void WebRTCInternals::ProcessPendingUpdates() { | 583 void WebRTCInternals::ProcessPendingUpdates() { |
562 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 584 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
563 while (!pending_updates_.empty()) { | 585 while (!pending_updates_.empty()) { |
564 const auto& update = pending_updates_.front(); | 586 const auto& update = pending_updates_.front(); |
565 for (auto& observer : observers_) | 587 for (auto& observer : observers_) |
566 observer.OnUpdate(update.command(), update.value()); | 588 observer.OnUpdate(update.command(), update.value()); |
567 pending_updates_.pop(); | 589 pending_updates_.pop(); |
568 } | 590 } |
569 } | 591 } |
570 | 592 |
(...skipping 13 matching lines...) Expand all Loading... |
584 | 606 |
585 if (this_pid == static_cast<int>(pid) && this_lid == lid) { | 607 if (this_pid == static_cast<int>(pid) && this_lid == lid) { |
586 if (index) | 608 if (index) |
587 *index = i; | 609 *index = i; |
588 return record; | 610 return record; |
589 } | 611 } |
590 } | 612 } |
591 return nullptr; | 613 return nullptr; |
592 } | 614 } |
593 } // namespace content | 615 } // namespace content |
OLD | NEW |