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

Unified Diff: net/base/network_stream_throttler.cc

Issue 1901533002: Implementation of network level throttler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months 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 side-by-side diff with in-line comments
Download patch
Index: net/base/network_stream_throttler.cc
diff --git a/net/base/network_stream_throttler.cc b/net/base/network_stream_throttler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6f274295ff4407275fca586a556495a32104d8ad
--- /dev/null
+++ b/net/base/network_stream_throttler.cc
@@ -0,0 +1,69 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/network_stream_throttler.h"
+
+namespace net {
+
+// Currently this is a null implementation that does no throttling;
+// all entries are created in the unthrottled state, and no throttle state
+// change notifications are transmitted.
+
+NetworkStreamThrottler::Throttle::~Throttle() {
+ if (throttler_)
+ throttler_->OnStreamDestroyed(this);
+}
+
+void NetworkStreamThrottler::Throttle::SetPriority(RequestPriority priority) {
+ RequestPriority old_priority = priority_;
+ priority_ = priority;
+ if (throttler_)
+ throttler_->OnStreamPriorityChanged(this, old_priority, priority);
+}
+
+NetworkStreamThrottler::Throttle::Throttle(
+ bool throttled,
+ RequestPriority priority,
+ base::WeakPtr<NetworkStreamThrottler> throttler)
+ : throttled_(throttled),
+ priority_(priority),
+ delegate_(nullptr),
+ throttler_(throttler) {}
+
+void NetworkStreamThrottler::Throttle::NotifyThrottleStateChanged(
+ bool new_throttle_state) {
+ throttled_ = new_throttle_state;
+ // TODO(rdsmith): Figure out if/how this needs to be made asynchronous.
+ if (delegate_)
+ delegate_->OnThrottleStateChanged();
+}
+
+NetworkStreamThrottler::NetworkStreamThrottler()
+ : priority_queue_(MAXIMUM_PRIORITY + 1), factory_(this) {}
+
+NetworkStreamThrottler::~NetworkStreamThrottler() {}
+
+scoped_ptr<NetworkStreamThrottler::Throttle>
+NetworkStreamThrottler::CreateThrottle(RequestPriority priority) {
+ scoped_ptr<Throttle> stream(
+ new Throttle(false, priority, factory_.GetWeakPtr()));
+
+ stream->set_queue_pointer(priority_queue_.Insert(stream.get(), priority));
+
+ return stream;
+}
+
+void NetworkStreamThrottler::OnStreamPriorityChanged(
+ Throttle* stream,
+ RequestPriority old_priority,
+ RequestPriority new_priority) {
+ priority_queue_.Erase(stream->queue_pointer());
+ stream->set_queue_pointer(priority_queue_.Insert(stream, new_priority));
+}
+
+void NetworkStreamThrottler::OnStreamDestroyed(Throttle* stream) {
+ priority_queue_.Erase(stream->queue_pointer());
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698