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

Side by Side Diff: content/browser/battery_status/battery_monitor_integration_browsertest.cc

Issue 2326913003: Privatize StrongBinding lifetime management (Closed)
Patch Set: rebase Created 4 years, 3 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <utility> 5 #include <utility>
6 6
7 #include "base/callback_list.h" 7 #include "base/callback_list.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/threading/thread_task_runner_handle.h" 10 #include "base/threading/thread_task_runner_handle.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 LAZY_INSTANCE_INITIALIZER; 42 LAZY_INSTANCE_INITIALIZER;
43 43
44 // Updates the global battery state and notifies existing test monitors. 44 // Updates the global battery state and notifies existing test monitors.
45 void UpdateBattery(const device::BatteryStatus& battery_status) { 45 void UpdateBattery(const device::BatteryStatus& battery_status) {
46 g_battery_status = battery_status; 46 g_battery_status = battery_status;
47 g_callback_list.Get().Notify(battery_status); 47 g_callback_list.Get().Notify(battery_status);
48 } 48 }
49 49
50 class FakeBatteryMonitor : public device::BatteryMonitor { 50 class FakeBatteryMonitor : public device::BatteryMonitor {
51 public: 51 public:
52 FakeBatteryMonitor() {}
53 ~FakeBatteryMonitor() override {}
54
52 static void Create(mojo::InterfaceRequest<BatteryMonitor> request) { 55 static void Create(mojo::InterfaceRequest<BatteryMonitor> request) {
53 new FakeBatteryMonitor(std::move(request)); 56 mojo::MakeStrongBinding(base::MakeUnique<FakeBatteryMonitor>(),
57 std::move(request));
54 } 58 }
55 59
56 private: 60 private:
57 FakeBatteryMonitor(mojo::InterfaceRequest<BatteryMonitor> request)
58 : binding_(this, std::move(request)) {}
59 ~FakeBatteryMonitor() override {}
60
61 void QueryNextStatus(const QueryNextStatusCallback& callback) override { 61 void QueryNextStatus(const QueryNextStatusCallback& callback) override {
62 // We don't expect overlapped calls to QueryNextStatus. 62 // We don't expect overlapped calls to QueryNextStatus.
63 DCHECK(callback_.is_null()); 63 DCHECK(callback_.is_null());
64 64
65 callback_ = callback; 65 callback_ = callback;
66 66
67 if (!subscription_) { 67 if (!subscription_) {
68 subscription_ = 68 subscription_ =
69 g_callback_list.Get().Add(base::Bind(&FakeBatteryMonitor::DidChange, 69 g_callback_list.Get().Add(base::Bind(&FakeBatteryMonitor::DidChange,
70 base::Unretained(this))); 70 base::Unretained(this)));
71 // Report initial value. 71 // Report initial value.
72 DidChange(g_battery_status); 72 DidChange(g_battery_status);
73 } 73 }
74 } 74 }
75 75
76 void DidChange(const device::BatteryStatus& battery_status) { 76 void DidChange(const device::BatteryStatus& battery_status) {
77 if (!callback_.is_null()) { 77 if (!callback_.is_null()) {
78 callback_.Run(battery_status.Clone()); 78 callback_.Run(battery_status.Clone());
79 callback_.Reset(); 79 callback_.Reset();
80 } 80 }
81 } 81 }
82 82
83 std::unique_ptr<BatteryUpdateSubscription> subscription_; 83 std::unique_ptr<BatteryUpdateSubscription> subscription_;
84 mojo::StrongBinding<BatteryMonitor> binding_;
85 QueryNextStatusCallback callback_; 84 QueryNextStatusCallback callback_;
86 }; 85 };
87 86
88 // Overrides the default service implementation with the test implementation 87 // Overrides the default service implementation with the test implementation
89 // declared above. 88 // declared above.
90 class TestContentBrowserClient : public ContentBrowserClient { 89 class TestContentBrowserClient : public ContentBrowserClient {
91 public: 90 public:
92 void ExposeInterfacesToRenderer( 91 void ExposeInterfacesToRenderer(
93 shell::InterfaceRegistry* registry, 92 shell::InterfaceRegistry* registry,
94 RenderProcessHost* render_process_host) override { 93 RenderProcessHost* render_process_host) override {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); 181 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1);
183 status.level = 0.6; 182 status.level = 0.6;
184 UpdateBattery(status); 183 UpdateBattery(status);
185 same_tab_observer2.Wait(); 184 same_tab_observer2.Wait();
186 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); 185 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
187 } 186 }
188 187
189 } // namespace 188 } // namespace
190 189
191 } // namespace content 190 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698