Commit 0bc103ac authored by Guido Urdaneta's avatar Guido Urdaneta Committed by Commit Bot

Update WakeLock policy for RTCPeerConnections.

This CL changes the policy so that the WakeLock is enabled only when an
RTCPeerConnection becomes connected.
Previously the WakeLock was enabled as soon as the RTCPeerConnection was
created. This caused problems for some users, since some sites create
peer connections that are never connected and are only closed when the
peer connection is removed.

Bug: 866200
Change-Id: I47e573f9555960c1a64a0f9834afb9d5dc048a94
Reviewed-on: https://chromium-review.googlesource.com/c/1315210Reviewed-by: default avatarHenrik Boström <hbos@chromium.org>
Reviewed-by: default avatarHarald Alvestrand <hta@chromium.org>
Commit-Queue: Guido Urdaneta <guidou@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608905}
parent 3fce642e
...@@ -104,7 +104,7 @@ WebRTCInternals::WebRTCInternals(int aggregate_updates_ms, ...@@ -104,7 +104,7 @@ WebRTCInternals::WebRTCInternals(int aggregate_updates_ms,
base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
switches::kWebRtcLocalEventLogging)), switches::kWebRtcLocalEventLogging)),
event_log_recordings_(false), event_log_recordings_(false),
num_open_connections_(0), num_connected_connections_(0),
should_block_power_saving_(should_block_power_saving), should_block_power_saving_(should_block_power_saving),
aggregate_updates_ms_(aggregate_updates_ms), aggregate_updates_ms_(aggregate_updates_ms),
weak_factory_(this) { weak_factory_(this) {
...@@ -180,13 +180,12 @@ void WebRTCInternals::OnAddPeerConnection(int render_process_id, ...@@ -180,13 +180,12 @@ void WebRTCInternals::OnAddPeerConnection(int render_process_id,
dict->SetString("constraints", constraints); dict->SetString("constraints", constraints);
dict->SetString("url", url); dict->SetString("url", url);
dict->SetBoolean("isOpen", true); dict->SetBoolean("isOpen", true);
dict->SetBoolean("connected", false);
if (observers_.might_have_observers()) if (observers_.might_have_observers())
SendUpdate("addPeerConnection", dict->CreateDeepCopy()); SendUpdate("addPeerConnection", dict->CreateDeepCopy());
peer_connection_data_.Append(std::move(dict)); peer_connection_data_.Append(std::move(dict));
++num_open_connections_;
UpdateWakeLock();
if (render_process_id_set_.insert(render_process_id).second) { if (render_process_id_set_.insert(render_process_id).second) {
RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); RenderProcessHost* host = RenderProcessHost::FromID(render_process_id);
...@@ -221,8 +220,16 @@ void WebRTCInternals::OnUpdatePeerConnection( ...@@ -221,8 +220,16 @@ void WebRTCInternals::OnUpdatePeerConnection(
if (!record) if (!record)
return; return;
if (type == "stop") if (type == "iceConnectionStateChange") {
if (value == "connected" || value == "checking" || value == "completed") {
MaybeMarkPeerConnectionAsConnected(record);
} else if (value == "failed" || value == "disconnected" ||
value == "closed" || value == "new") {
MaybeMarkPeerConnectionAsNotConnected(record);
}
} else if (type == "stop") {
MaybeClosePeerConnection(record); MaybeClosePeerConnection(record);
}
// Don't update entries if there aren't any observers. // Don't update entries if there aren't any observers.
if (!observers_.might_have_observers()) if (!observers_.might_have_observers())
...@@ -556,9 +563,30 @@ void WebRTCInternals::MaybeClosePeerConnection(base::DictionaryValue* record) { ...@@ -556,9 +563,30 @@ void WebRTCInternals::MaybeClosePeerConnection(base::DictionaryValue* record) {
return; return;
record->SetBoolean("isOpen", false); record->SetBoolean("isOpen", false);
--num_open_connections_; MaybeMarkPeerConnectionAsNotConnected(record);
DCHECK_GE(num_open_connections_, 0); }
UpdateWakeLock();
void WebRTCInternals::MaybeMarkPeerConnectionAsConnected(
base::DictionaryValue* record) {
bool was_connected = true;
record->GetBoolean("connected", &was_connected);
if (!was_connected) {
++num_connected_connections_;
record->SetBoolean("connected", true);
UpdateWakeLock();
}
}
void WebRTCInternals::MaybeMarkPeerConnectionAsNotConnected(
base::DictionaryValue* record) {
bool was_connected = false;
record->GetBoolean("connected", &was_connected);
if (was_connected) {
record->SetBoolean("connected", false);
--num_connected_connections_;
DCHECK_GE(num_connected_connections_, 0);
UpdateWakeLock();
}
} }
void WebRTCInternals::UpdateWakeLock() { void WebRTCInternals::UpdateWakeLock() {
...@@ -566,12 +594,13 @@ void WebRTCInternals::UpdateWakeLock() { ...@@ -566,12 +594,13 @@ void WebRTCInternals::UpdateWakeLock() {
if (!should_block_power_saving_) if (!should_block_power_saving_)
return; return;
if (num_open_connections_ == 0) { if (num_connected_connections_ == 0) {
DVLOG(1) DVLOG(1)
<< ("Cancel the wake lock on application suspension since no " << ("Cancel the wake lock on application suspension since no "
"PeerConnections are active anymore."); "PeerConnections are active anymore.");
GetWakeLock()->CancelWakeLock(); GetWakeLock()->CancelWakeLock();
} else if (num_open_connections_ != 0) { } else {
DCHECK_GT(num_connected_connections_, 0);
DVLOG(1) << ("Preventing the application from being suspended while one or " DVLOG(1) << ("Preventing the application from being suspended while one or "
"more PeerConnections are active."); "more PeerConnections are active.");
GetWakeLock()->RequestWakeLock(); GetWakeLock()->RequestWakeLock();
......
...@@ -122,7 +122,7 @@ class CONTENT_EXPORT WebRTCInternals : public RenderProcessHostObserver, ...@@ -122,7 +122,7 @@ class CONTENT_EXPORT WebRTCInternals : public RenderProcessHostObserver,
bool IsEventLogRecordingsEnabled() const; bool IsEventLogRecordingsEnabled() const;
bool CanToggleEventLogRecordings() const; bool CanToggleEventLogRecordings() const;
int num_open_connections() const { return num_open_connections_; } int num_connected_connections() const { return num_connected_connections_; }
protected: protected:
// Constructor/Destructor are protected to allow tests to derive from the // Constructor/Destructor are protected to allow tests to derive from the
...@@ -170,6 +170,9 @@ class CONTENT_EXPORT WebRTCInternals : public RenderProcessHostObserver, ...@@ -170,6 +170,9 @@ class CONTENT_EXPORT WebRTCInternals : public RenderProcessHostObserver,
// is stopped or removed. // is stopped or removed.
void MaybeClosePeerConnection(base::DictionaryValue* record); void MaybeClosePeerConnection(base::DictionaryValue* record);
void MaybeMarkPeerConnectionAsConnected(base::DictionaryValue* record);
void MaybeMarkPeerConnectionAsNotConnected(base::DictionaryValue* record);
// Called whenever a PeerConnection is created or stopped in order to // Called whenever a PeerConnection is created or stopped in order to
// request/cancel a wake lock on suspending the current application for power // request/cancel a wake lock on suspending the current application for power
// saving. // saving.
...@@ -235,9 +238,10 @@ class CONTENT_EXPORT WebRTCInternals : public RenderProcessHostObserver, ...@@ -235,9 +238,10 @@ class CONTENT_EXPORT WebRTCInternals : public RenderProcessHostObserver,
bool event_log_recordings_; bool event_log_recordings_;
base::FilePath event_log_recordings_file_path_; base::FilePath event_log_recordings_file_path_;
// While |num_open_connections_| is greater than zero, request a wake lock // While |num_connected_connections_| is greater than zero, request a wake
// service. This prevents the application from being suspended while remoting. // lock service. This prevents the application from being suspended while
int num_open_connections_; // remoting.
int num_connected_connections_;
const bool should_block_power_saving_; const bool should_block_power_saving_;
// Set of render process hosts that |this| is registered as an observer on. // Set of render process hosts that |this| is registered as an observer on.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment