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

Unified Diff: chrome/browser/net/nqe/ui_network_quality_estimator_service.cc

Issue 2103323007: Exposing NQE on the Browser UI thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 5 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: chrome/browser/net/nqe/ui_network_quality_estimator_service.cc
diff --git a/chrome/browser/net/nqe/ui_network_quality_estimator_service.cc b/chrome/browser/net/nqe/ui_network_quality_estimator_service.cc
new file mode 100644
index 0000000000000000000000000000000000000000..810c769caaa93545098829419e0bade10fd418ae
--- /dev/null
+++ b/chrome/browser/net/nqe/ui_network_quality_estimator_service.cc
@@ -0,0 +1,102 @@
+// 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 "chrome/browser/net/nqe/ui_network_quality_estimator_service.h"
+
+#include "base/bind.h"
+#include "base/message_loop/message_loop.h"
+#include "base/task_runner.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/io_thread.h"
+
+namespace chrome_browser_net {
+
+class UINetworkQualityEstimatorService::IONetworkQualityObserver
tbansal1 2016/07/13 00:07:27 Add class comments. Something along the lines of:
RyanSturm 2016/07/13 17:28:14 Done.
+ : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver {
+ public:
+ IONetworkQualityObserver(
+ scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
+ base::WeakPtr<UINetworkQualityEstimatorService> service)
+ : ui_task_runner_(ui_task_runner), service_(service) {
+ // This is created on the UI thread, but used only on the UI thread.
+ thread_checker_.DetachFromThread();
tbansal1 2016/07/13 00:07:28 DCHECK(ui_task_runner_);
RyanSturm 2016/07/13 17:28:14 Done.
+ }
+
+ ~IONetworkQualityObserver() override {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ if (network_quality_estimator_)
+ network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this);
+ }
+
+ void InitializeOnIOThread(IOThread* io_thread) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ if (!io_thread->globals()->network_quality_estimator)
+ return;
+ network_quality_estimator_ =
+ io_thread->globals()->network_quality_estimator->GetWeakPtr();
tbansal1 2016/07/13 00:07:28 Just use the raw pointer.
RyanSturm 2016/07/13 17:28:14 I'm not sure that the network_quality_estimator_->
tbansal1 2016/07/13 17:56:11 The IOThread::globals are destroyed in IOThread::C
tbansal1 2016/07/13 18:08:13 Because of the above, you can remove the WeakPtr,
+ network_quality_estimator_->AddEffectiveConnectionTypeObserver(this);
tbansal1 2016/07/13 00:07:28 Check if network_quality_estimator_ is non-null be
RyanSturm 2016/07/13 17:28:14 Done.
+ ui_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(
+ &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged,
+ service_,
+ network_quality_estimator_->GetEffectiveConnectionType()));
+ }
+
+ protected:
+ void OnEffectiveConnectionTypeChanged(
tbansal1 2016/07/13 00:07:28 Why protected? Also, add comment: // net::Network
RyanSturm 2016/07/13 17:28:14 Done.
+ net::NetworkQualityEstimator::EffectiveConnectionType type) override {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ ui_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(
+ &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged,
+ service_, type));
+ }
+
+ private:
+ scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
+ base::WeakPtr<UINetworkQualityEstimatorService> service_;
+ base::WeakPtr<net::NetworkQualityEstimator> network_quality_estimator_;
+ base::ThreadChecker thread_checker_;
+
+ DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver);
+};
+
+UINetworkQualityEstimatorService::UINetworkQualityEstimatorService(
+ Profile* profile)
+ : type_(net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
+ io_observer_(nullptr),
+ weak_factory_(this) {}
+
+UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(io_task_runner_->DeleteSoon(FROM_HERE, io_observer_));
+}
+
+void UINetworkQualityEstimatorService::Initialize(
+ scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
+ scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ io_task_runner_ = io_task_runner;
+ io_observer_ =
+ new IONetworkQualityObserver(ui_task_runner, weak_factory_.GetWeakPtr());
+ io_task_runner_->PostTask(
+ FROM_HERE, base::Bind(&IONetworkQualityObserver::InitializeOnIOThread,
+ base::Unretained(io_observer_),
+ g_browser_process->io_thread()));
+}
+
+void UINetworkQualityEstimatorService::Shutdown() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ weak_factory_.InvalidateWeakPtrs();
+}
+
+void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged(
+ net::NetworkQualityEstimator::EffectiveConnectionType type) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ type_ = type;
+}
+
+} // namespace chrome_browser_net

Powered by Google App Engine
This is Rietveld 408576698