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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 WebRTCInternals::WebRTCInternals() : WebRTCInternals(500, true) {} | 86 WebRTCInternals::WebRTCInternals() : WebRTCInternals(500, true) {} |
84 | 87 |
85 WebRTCInternals::WebRTCInternals(int aggregate_updates_ms, | 88 WebRTCInternals::WebRTCInternals(int aggregate_updates_ms, |
86 bool should_block_power_saving) | 89 bool should_block_power_saving) |
87 : audio_debug_recordings_(false), | 90 : audio_debug_recordings_(false), |
88 event_log_recordings_(false), | 91 event_log_recordings_(false), |
89 selecting_event_log_(false), | 92 selecting_event_log_(false), |
90 num_open_connections_(0), | 93 num_open_connections_(0), |
91 should_block_power_saving_(should_block_power_saving), | 94 should_block_power_saving_(should_block_power_saving), |
92 aggregate_updates_ms_(aggregate_updates_ms), | 95 aggregate_updates_ms_(aggregate_updates_ms), |
| 96 has_wake_lock_for_testing_(false), |
93 weak_factory_(this) { | 97 weak_factory_(this) { |
94 // TODO(grunell): Shouldn't all the webrtc_internals* files be excluded from the | 98 // TODO(grunell): Shouldn't all the webrtc_internals* files be excluded from the |
95 // build if WebRTC is disabled? | 99 // build if WebRTC is disabled? |
96 #if BUILDFLAG(ENABLE_WEBRTC) | 100 #if BUILDFLAG(ENABLE_WEBRTC) |
97 audio_debug_recordings_file_path_ = | 101 audio_debug_recordings_file_path_ = |
98 GetContentClient()->browser()->GetDefaultDownloadDirectory(); | 102 GetContentClient()->browser()->GetDefaultDownloadDirectory(); |
99 event_log_recordings_file_path_ = audio_debug_recordings_file_path_; | 103 event_log_recordings_file_path_ = audio_debug_recordings_file_path_; |
100 | 104 |
101 if (audio_debug_recordings_file_path_.empty()) { | 105 if (audio_debug_recordings_file_path_.empty()) { |
102 // In this case the default path (|audio_debug_recordings_file_path_|) will | 106 // In this case the default path (|audio_debug_recordings_file_path_|) will |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 dict->SetString("rtcConfiguration", rtc_configuration); | 143 dict->SetString("rtcConfiguration", rtc_configuration); |
140 dict->SetString("constraints", constraints); | 144 dict->SetString("constraints", constraints); |
141 dict->SetString("url", url); | 145 dict->SetString("url", url); |
142 dict->SetBoolean("isOpen", true); | 146 dict->SetBoolean("isOpen", true); |
143 | 147 |
144 if (observers_.might_have_observers()) | 148 if (observers_.might_have_observers()) |
145 SendUpdate("addPeerConnection", dict->CreateDeepCopy()); | 149 SendUpdate("addPeerConnection", dict->CreateDeepCopy()); |
146 | 150 |
147 peer_connection_data_.Append(std::move(dict)); | 151 peer_connection_data_.Append(std::move(dict)); |
148 ++num_open_connections_; | 152 ++num_open_connections_; |
149 CreateOrReleasePowerSaveBlocker(); | 153 UpdateWakeLock(); |
150 | 154 |
151 if (render_process_id_set_.insert(render_process_id).second) { | 155 if (render_process_id_set_.insert(render_process_id).second) { |
152 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); | 156 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); |
153 if (host) | 157 if (host) |
154 host->AddObserver(this); | 158 host->AddObserver(this); |
155 } | 159 } |
156 } | 160 } |
157 | 161 |
158 void WebRTCInternals::OnRemovePeerConnection(ProcessId pid, int lid) { | 162 void WebRTCInternals::OnRemovePeerConnection(ProcessId pid, int lid) { |
159 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 163 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( | 461 std::unique_ptr<base::DictionaryValue> update( |
458 new base::DictionaryValue()); | 462 new base::DictionaryValue()); |
459 update->SetInteger("lid", lid); | 463 update->SetInteger("lid", lid); |
460 update->SetInteger("pid", pid); | 464 update->SetInteger("pid", pid); |
461 SendUpdate("removePeerConnection", std::move(update)); | 465 SendUpdate("removePeerConnection", std::move(update)); |
462 } | 466 } |
463 MaybeClosePeerConnection(record); | 467 MaybeClosePeerConnection(record); |
464 peer_connection_data_.Remove(i, NULL); | 468 peer_connection_data_.Remove(i, NULL); |
465 } | 469 } |
466 } | 470 } |
467 CreateOrReleasePowerSaveBlocker(); | 471 UpdateWakeLock(); |
468 | 472 |
469 bool found_any = false; | 473 bool found_any = false; |
470 // Iterates from the end of the list to remove the getUserMedia requests | 474 // Iterates from the end of the list to remove the getUserMedia requests |
471 // created by the exiting renderer. | 475 // created by the exiting renderer. |
472 for (int i = get_user_media_requests_.GetSize() - 1; i >= 0; --i) { | 476 for (int i = get_user_media_requests_.GetSize() - 1; i >= 0; --i) { |
473 base::DictionaryValue* record = NULL; | 477 base::DictionaryValue* record = NULL; |
474 get_user_media_requests_.GetDictionary(i, &record); | 478 get_user_media_requests_.GetDictionary(i, &record); |
475 | 479 |
476 int this_rid = 0; | 480 int this_rid = 0; |
477 record->GetInteger("rid", &this_rid); | 481 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) { | 531 void WebRTCInternals::MaybeClosePeerConnection(base::DictionaryValue* record) { |
528 bool is_open; | 532 bool is_open; |
529 bool did_read = record->GetBoolean("isOpen", &is_open); | 533 bool did_read = record->GetBoolean("isOpen", &is_open); |
530 DCHECK(did_read); | 534 DCHECK(did_read); |
531 if (!is_open) | 535 if (!is_open) |
532 return; | 536 return; |
533 | 537 |
534 record->SetBoolean("isOpen", false); | 538 record->SetBoolean("isOpen", false); |
535 --num_open_connections_; | 539 --num_open_connections_; |
536 DCHECK_GE(num_open_connections_, 0); | 540 DCHECK_GE(num_open_connections_, 0); |
537 CreateOrReleasePowerSaveBlocker(); | 541 UpdateWakeLock(); |
538 } | 542 } |
539 | 543 |
540 void WebRTCInternals::CreateOrReleasePowerSaveBlocker() { | 544 void WebRTCInternals::UpdateWakeLock() { |
541 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 545 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
542 if (!should_block_power_saving_) | 546 if (!should_block_power_saving_) |
543 return; | 547 return; |
544 | 548 |
545 if (num_open_connections_ == 0 && power_save_blocker_) { | 549 if (num_open_connections_ == 0) { |
546 DVLOG(1) << ("Releasing the block on application suspension since no " | 550 DVLOG(1) |
547 "PeerConnections are active anymore."); | 551 << ("Cancel the wake lock on application suspension since no " |
548 power_save_blocker_.reset(); | 552 "PeerConnections are active anymore."); |
549 } else if (num_open_connections_ != 0 && !power_save_blocker_) { | 553 GetWakeLock()->CancelWakeLock(); |
| 554 has_wake_lock_for_testing_ = false; |
| 555 } else if (num_open_connections_ != 0) { |
550 DVLOG(1) << ("Preventing the application from being suspended while one or " | 556 DVLOG(1) << ("Preventing the application from being suspended while one or " |
551 "more PeerConnections are active."); | 557 "more PeerConnections are active."); |
552 power_save_blocker_.reset(new device::PowerSaveBlocker( | 558 GetWakeLock()->RequestWakeLock(); |
553 device::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | 559 has_wake_lock_for_testing_ = true; |
554 device::PowerSaveBlocker::kReasonOther, | |
555 "WebRTC has active PeerConnections", | |
556 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI), | |
557 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE))); | |
558 } | 560 } |
559 } | 561 } |
560 | 562 |
| 563 device::mojom::WakeLockService* WebRTCInternals::GetWakeLock() { |
| 564 // Here is a lazy binding, and will not reconnect after connection error. |
| 565 if (!wake_lock_) { |
| 566 device::mojom::WakeLockServiceRequest request = |
| 567 mojo::MakeRequest(&wake_lock_); |
| 568 // In some testing contexts, the service manager connection isn't |
| 569 // initialized. |
| 570 if (ServiceManagerConnection::GetForProcess()) { |
| 571 service_manager::Connector* connector = |
| 572 ServiceManagerConnection::GetForProcess()->GetConnector(); |
| 573 DCHECK(connector); |
| 574 device::mojom::WakeLockProviderPtr wake_lock_provider; |
| 575 connector->BindInterface(device::mojom::kServiceName, |
| 576 mojo::MakeRequest(&wake_lock_provider)); |
| 577 wake_lock_provider->GetWakeLockWithoutContext( |
| 578 device::mojom::WakeLockType::PreventAppSuspension, |
| 579 device::mojom::WakeLockReason::ReasonOther, |
| 580 "WebRTC has active PeerConnections", std::move(request)); |
| 581 } |
| 582 } |
| 583 return wake_lock_.get(); |
| 584 } |
| 585 |
561 void WebRTCInternals::ProcessPendingUpdates() { | 586 void WebRTCInternals::ProcessPendingUpdates() { |
562 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 587 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
563 while (!pending_updates_.empty()) { | 588 while (!pending_updates_.empty()) { |
564 const auto& update = pending_updates_.front(); | 589 const auto& update = pending_updates_.front(); |
565 for (auto& observer : observers_) | 590 for (auto& observer : observers_) |
566 observer.OnUpdate(update.command(), update.value()); | 591 observer.OnUpdate(update.command(), update.value()); |
567 pending_updates_.pop(); | 592 pending_updates_.pop(); |
568 } | 593 } |
569 } | 594 } |
570 | 595 |
(...skipping 13 matching lines...) Expand all Loading... |
584 | 609 |
585 if (this_pid == static_cast<int>(pid) && this_lid == lid) { | 610 if (this_pid == static_cast<int>(pid) && this_lid == lid) { |
586 if (index) | 611 if (index) |
587 *index = i; | 612 *index = i; |
588 return record; | 613 return record; |
589 } | 614 } |
590 } | 615 } |
591 return nullptr; | 616 return nullptr; |
592 } | 617 } |
593 } // namespace content | 618 } // namespace content |
OLD | NEW |