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

Side by Side Diff: content/browser/webrtc/webrtc_internals.cc

Issue 2502413002: Release PowerSaveBlock on PeerConnection stop instead of removal (Closed)
Patch Set: Created 4 years, 1 month 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
OLDNEW
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>
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 return value_.get(); 72 return value_.get();
73 } 73 }
74 74
75 WebRTCInternals::WebRTCInternals() : WebRTCInternals(500, true) {} 75 WebRTCInternals::WebRTCInternals() : WebRTCInternals(500, true) {}
76 76
77 WebRTCInternals::WebRTCInternals(int aggregate_updates_ms, 77 WebRTCInternals::WebRTCInternals(int aggregate_updates_ms,
78 bool should_block_power_saving) 78 bool should_block_power_saving)
79 : audio_debug_recordings_(false), 79 : audio_debug_recordings_(false),
80 event_log_recordings_(false), 80 event_log_recordings_(false),
81 selecting_event_log_(false), 81 selecting_event_log_(false),
82 num_open_connections_(0),
82 should_block_power_saving_(should_block_power_saving), 83 should_block_power_saving_(should_block_power_saving),
83 aggregate_updates_ms_(aggregate_updates_ms), 84 aggregate_updates_ms_(aggregate_updates_ms),
84 weak_factory_(this) { 85 weak_factory_(this) {
85 // TODO(grunell): Shouldn't all the webrtc_internals* files be excluded from the 86 // TODO(grunell): Shouldn't all the webrtc_internals* files be excluded from the
86 // build if WebRTC is disabled? 87 // build if WebRTC is disabled?
87 #if defined(ENABLE_WEBRTC) 88 #if defined(ENABLE_WEBRTC)
88 audio_debug_recordings_file_path_ = 89 audio_debug_recordings_file_path_ =
89 GetContentClient()->browser()->GetDefaultDownloadDirectory(); 90 GetContentClient()->browser()->GetDefaultDownloadDirectory();
90 event_log_recordings_file_path_ = audio_debug_recordings_file_path_; 91 event_log_recordings_file_path_ = audio_debug_recordings_file_path_;
91 92
(...skipping 28 matching lines...) Expand all
120 const string& constraints) { 121 const string& constraints) {
121 DCHECK_CURRENTLY_ON(BrowserThread::UI); 122 DCHECK_CURRENTLY_ON(BrowserThread::UI);
122 123
123 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); 124 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
124 dict->SetInteger("rid", render_process_id); 125 dict->SetInteger("rid", render_process_id);
125 dict->SetInteger("pid", static_cast<int>(pid)); 126 dict->SetInteger("pid", static_cast<int>(pid));
126 dict->SetInteger("lid", lid); 127 dict->SetInteger("lid", lid);
127 dict->SetString("rtcConfiguration", rtc_configuration); 128 dict->SetString("rtcConfiguration", rtc_configuration);
128 dict->SetString("constraints", constraints); 129 dict->SetString("constraints", constraints);
129 dict->SetString("url", url); 130 dict->SetString("url", url);
131 dict->SetBoolean("isOpen", true);
130 132
131 if (observers_.might_have_observers()) 133 if (observers_.might_have_observers())
132 SendUpdate("addPeerConnection", dict->CreateDeepCopy()); 134 SendUpdate("addPeerConnection", dict->CreateDeepCopy());
133 135
134 peer_connection_data_.Append(std::move(dict)); 136 peer_connection_data_.Append(std::move(dict));
137 ++num_open_connections_;
135 CreateOrReleasePowerSaveBlocker(); 138 CreateOrReleasePowerSaveBlocker();
136 139
137 if (render_process_id_set_.insert(render_process_id).second) { 140 if (render_process_id_set_.insert(render_process_id).second) {
138 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); 141 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id);
139 if (host) 142 if (host)
140 host->AddObserver(this); 143 host->AddObserver(this);
141 } 144 }
142 } 145 }
143 146
144 void WebRTCInternals::OnRemovePeerConnection(ProcessId pid, int lid) { 147 void WebRTCInternals::OnRemovePeerConnection(ProcessId pid, int lid) {
145 DCHECK_CURRENTLY_ON(BrowserThread::UI); 148 DCHECK_CURRENTLY_ON(BrowserThread::UI);
146 for (size_t i = 0; i < peer_connection_data_.GetSize(); ++i) { 149 for (size_t i = 0; i < peer_connection_data_.GetSize(); ++i) {
147 base::DictionaryValue* dict = NULL; 150 base::DictionaryValue* dict = NULL;
148 peer_connection_data_.GetDictionary(i, &dict); 151 peer_connection_data_.GetDictionary(i, &dict);
149 152
150 int this_pid = 0; 153 int this_pid = 0;
151 int this_lid = 0; 154 int this_lid = 0;
152 dict->GetInteger("pid", &this_pid); 155 dict->GetInteger("pid", &this_pid);
153 dict->GetInteger("lid", &this_lid); 156 dict->GetInteger("lid", &this_lid);
154 157
155 if (this_pid != static_cast<int>(pid) || this_lid != lid) 158 if (this_pid != static_cast<int>(pid) || this_lid != lid)
156 continue; 159 continue;
157 160
161 MaybeClosePeerConnection(dict);
158 peer_connection_data_.Remove(i, NULL); 162 peer_connection_data_.Remove(i, NULL);
159 CreateOrReleasePowerSaveBlocker();
160 163
161 if (observers_.might_have_observers()) { 164 if (observers_.might_have_observers()) {
162 std::unique_ptr<base::DictionaryValue> id(new base::DictionaryValue()); 165 std::unique_ptr<base::DictionaryValue> id(new base::DictionaryValue());
163 id->SetInteger("pid", static_cast<int>(pid)); 166 id->SetInteger("pid", static_cast<int>(pid));
164 id->SetInteger("lid", lid); 167 id->SetInteger("lid", lid);
165 SendUpdate("removePeerConnection", std::move(id)); 168 SendUpdate("removePeerConnection", std::move(id));
166 } 169 }
167 break; 170 break;
168 } 171 }
169 } 172 }
170 173
171 void WebRTCInternals::OnUpdatePeerConnection( 174 void WebRTCInternals::OnUpdatePeerConnection(
172 ProcessId pid, int lid, const string& type, const string& value) { 175 ProcessId pid, int lid, const string& type, const string& value) {
173 DCHECK_CURRENTLY_ON(BrowserThread::UI); 176 DCHECK_CURRENTLY_ON(BrowserThread::UI);
174 177
175 for (size_t i = 0; i < peer_connection_data_.GetSize(); ++i) { 178 for (size_t i = 0; i < peer_connection_data_.GetSize(); ++i) {
176 base::DictionaryValue* record = NULL; 179 base::DictionaryValue* record = NULL;
177 peer_connection_data_.GetDictionary(i, &record); 180 peer_connection_data_.GetDictionary(i, &record);
178 181
179 int this_pid = 0, this_lid = 0; 182 int this_pid = 0, this_lid = 0;
180 record->GetInteger("pid", &this_pid); 183 record->GetInteger("pid", &this_pid);
181 record->GetInteger("lid", &this_lid); 184 record->GetInteger("lid", &this_lid);
182 185
183 if (this_pid != static_cast<int>(pid) || this_lid != lid) 186 if (this_pid != static_cast<int>(pid) || this_lid != lid)
184 continue; 187 continue;
185 188
189 if (type == "stop") {
190 MaybeClosePeerConnection(record);
191 }
192
186 // Append the update to the end of the log. 193 // Append the update to the end of the log.
187 base::ListValue* log = EnsureLogList(record); 194 base::ListValue* log = EnsureLogList(record);
188 if (!log) 195 if (!log)
189 return; 196 return;
190 197
191 std::unique_ptr<base::DictionaryValue> log_entry( 198 std::unique_ptr<base::DictionaryValue> log_entry(
192 new base::DictionaryValue()); 199 new base::DictionaryValue());
193 200
194 double epoch_time = base::Time::Now().ToJsTime(); 201 double epoch_time = base::Time::Now().ToJsTime();
195 string time = base::DoubleToString(epoch_time); 202 string time = base::DoubleToString(epoch_time);
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 DCHECK_CURRENTLY_ON(BrowserThread::UI); 502 DCHECK_CURRENTLY_ON(BrowserThread::UI);
496 503
497 event_log_recordings_ = true; 504 event_log_recordings_ = true;
498 for (RenderProcessHost::iterator i( 505 for (RenderProcessHost::iterator i(
499 content::RenderProcessHost::AllHostsIterator()); 506 content::RenderProcessHost::AllHostsIterator());
500 !i.IsAtEnd(); i.Advance()) 507 !i.IsAtEnd(); i.Advance())
501 i.GetCurrentValue()->StartWebRTCEventLog(event_log_recordings_file_path_); 508 i.GetCurrentValue()->StartWebRTCEventLog(event_log_recordings_file_path_);
502 } 509 }
503 #endif 510 #endif
504 511
512 void WebRTCInternals::MaybeClosePeerConnection(base::DictionaryValue* record) {
513 bool is_open;
514 bool did_read = record->GetBoolean("isOpen", &is_open);
515 DCHECK(did_read);
516 if (!is_open)
517 return;
518
519 record->SetBoolean("isOpen", false);
520 --num_open_connections_;
521 DCHECK_GE(num_open_connections_, 0);
522 CreateOrReleasePowerSaveBlocker();
523 }
524
505 void WebRTCInternals::CreateOrReleasePowerSaveBlocker() { 525 void WebRTCInternals::CreateOrReleasePowerSaveBlocker() {
506 DCHECK_CURRENTLY_ON(BrowserThread::UI); 526 DCHECK_CURRENTLY_ON(BrowserThread::UI);
507 if (!should_block_power_saving_) 527 if (!should_block_power_saving_)
508 return; 528 return;
509 529
510 if (peer_connection_data_.empty() && power_save_blocker_) { 530 if (num_open_connections_ == 0 && power_save_blocker_) {
511 DVLOG(1) << ("Releasing the block on application suspension since no " 531 DVLOG(1) << ("Releasing the block on application suspension since no "
512 "PeerConnections are active anymore."); 532 "PeerConnections are active anymore.");
513 power_save_blocker_.reset(); 533 power_save_blocker_.reset();
514 } else if (!peer_connection_data_.empty() && !power_save_blocker_) { 534 } else if (num_open_connections_ != 0 && !power_save_blocker_) {
515 DVLOG(1) << ("Preventing the application from being suspended while one or " 535 DVLOG(1) << ("Preventing the application from being suspended while one or "
516 "more PeerConnections are active."); 536 "more PeerConnections are active.");
517 power_save_blocker_.reset(new device::PowerSaveBlocker( 537 power_save_blocker_.reset(new device::PowerSaveBlocker(
518 device::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 538 device::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
519 device::PowerSaveBlocker::kReasonOther, 539 device::PowerSaveBlocker::kReasonOther,
520 "WebRTC has active PeerConnections", 540 "WebRTC has active PeerConnections",
521 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI), 541 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI),
522 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE))); 542 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE)));
523 } 543 }
524 } 544 }
525 545
526 void WebRTCInternals::ProcessPendingUpdates() { 546 void WebRTCInternals::ProcessPendingUpdates() {
527 DCHECK_CURRENTLY_ON(BrowserThread::UI); 547 DCHECK_CURRENTLY_ON(BrowserThread::UI);
528 while (!pending_updates_.empty()) { 548 while (!pending_updates_.empty()) {
529 const auto& update = pending_updates_.front(); 549 const auto& update = pending_updates_.front();
530 for (auto& observer : observers_) 550 for (auto& observer : observers_)
531 observer.OnUpdate(update.command(), update.value()); 551 observer.OnUpdate(update.command(), update.value());
532 pending_updates_.pop(); 552 pending_updates_.pop();
533 } 553 }
534 } 554 }
535 555
536 } // namespace content 556 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698