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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/task_runner.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/io_thread.h"
12
13 namespace chrome_browser_net {
14
15 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.
16 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver {
17 public:
18 IONetworkQualityObserver(
19 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
20 base::WeakPtr<UINetworkQualityEstimatorService> service)
21 : ui_task_runner_(ui_task_runner), service_(service) {
22 // This is created on the UI thread, but used only on the UI thread.
23 thread_checker_.DetachFromThread();
tbansal1 2016/07/13 00:07:28 DCHECK(ui_task_runner_);
RyanSturm 2016/07/13 17:28:14 Done.
24 }
25
26 ~IONetworkQualityObserver() override {
27 DCHECK(thread_checker_.CalledOnValidThread());
28 if (network_quality_estimator_)
29 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this);
30 }
31
32 void InitializeOnIOThread(IOThread* io_thread) {
33 DCHECK(thread_checker_.CalledOnValidThread());
34 if (!io_thread->globals()->network_quality_estimator)
35 return;
36 network_quality_estimator_ =
37 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,
38 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.
39 ui_task_runner_->PostTask(
40 FROM_HERE,
41 base::Bind(
42 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged,
43 service_,
44 network_quality_estimator_->GetEffectiveConnectionType()));
45 }
46
47 protected:
48 void OnEffectiveConnectionTypeChanged(
tbansal1 2016/07/13 00:07:28 Why protected? Also, add comment: // net::Network
RyanSturm 2016/07/13 17:28:14 Done.
49 net::NetworkQualityEstimator::EffectiveConnectionType type) override {
50 DCHECK(thread_checker_.CalledOnValidThread());
51 ui_task_runner_->PostTask(
52 FROM_HERE,
53 base::Bind(
54 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged,
55 service_, type));
56 }
57
58 private:
59 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
60 base::WeakPtr<UINetworkQualityEstimatorService> service_;
61 base::WeakPtr<net::NetworkQualityEstimator> network_quality_estimator_;
62 base::ThreadChecker thread_checker_;
63
64 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver);
65 };
66
67 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService(
68 Profile* profile)
69 : type_(net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
70 io_observer_(nullptr),
71 weak_factory_(this) {}
72
73 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() {
74 DCHECK(thread_checker_.CalledOnValidThread());
75 DCHECK(io_task_runner_->DeleteSoon(FROM_HERE, io_observer_));
76 }
77
78 void UINetworkQualityEstimatorService::Initialize(
79 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
80 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) {
81 DCHECK(thread_checker_.CalledOnValidThread());
82 io_task_runner_ = io_task_runner;
83 io_observer_ =
84 new IONetworkQualityObserver(ui_task_runner, weak_factory_.GetWeakPtr());
85 io_task_runner_->PostTask(
86 FROM_HERE, base::Bind(&IONetworkQualityObserver::InitializeOnIOThread,
87 base::Unretained(io_observer_),
88 g_browser_process->io_thread()));
89 }
90
91 void UINetworkQualityEstimatorService::Shutdown() {
92 DCHECK(thread_checker_.CalledOnValidThread());
93 weak_factory_.InvalidateWeakPtrs();
94 }
95
96 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged(
97 net::NetworkQualityEstimator::EffectiveConnectionType type) {
98 DCHECK(thread_checker_.CalledOnValidThread());
99 type_ = type;
100 }
101
102 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698