| OLD | NEW |
| 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 "base/callback_list.h" | 5 #include "base/callback_list.h" |
| 6 #include "base/lazy_instance.h" | 6 #include "base/lazy_instance.h" |
| 7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
| 8 #include "content/public/browser/content_browser_client.h" | 8 #include "content/public/browser/content_browser_client.h" |
| 9 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/common/content_client.h" | 10 #include "content/public/common/content_client.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 g_callback_list.Get().Notify(battery_status); | 43 g_callback_list.Get().Notify(battery_status); |
| 44 } | 44 } |
| 45 | 45 |
| 46 class FakeBatteryMonitor : public device::BatteryMonitor { | 46 class FakeBatteryMonitor : public device::BatteryMonitor { |
| 47 public: | 47 public: |
| 48 static void Create(mojo::InterfaceRequest<BatteryMonitor> request) { | 48 static void Create(mojo::InterfaceRequest<BatteryMonitor> request) { |
| 49 new FakeBatteryMonitor(request.Pass()); | 49 new FakeBatteryMonitor(request.Pass()); |
| 50 } | 50 } |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 typedef mojo::Callback<void(device::BatteryStatusPtr)> BatteryStatusCallback; |
| 54 |
| 53 FakeBatteryMonitor(mojo::InterfaceRequest<BatteryMonitor> request) | 55 FakeBatteryMonitor(mojo::InterfaceRequest<BatteryMonitor> request) |
| 54 : subscription_( | 56 : binding_(this, request.Pass()) { |
| 55 g_callback_list.Get().Add(base::Bind(&FakeBatteryMonitor::DidChange, | |
| 56 base::Unretained(this)))), | |
| 57 binding_(this, request.Pass()) { | |
| 58 DidChange(g_battery_status); | |
| 59 } | 57 } |
| 60 ~FakeBatteryMonitor() override {} | 58 ~FakeBatteryMonitor() override {} |
| 61 | 59 |
| 60 void QueryNextStatus(const BatteryStatusCallback& callback) override { |
| 61 // We don't expect overlapped calls to QueryNextStatus. |
| 62 DCHECK(callback_.is_null()); |
| 63 |
| 64 callback_ = callback; |
| 65 |
| 66 if (!subscription_) { |
| 67 subscription_ = |
| 68 g_callback_list.Get().Add(base::Bind(&FakeBatteryMonitor::DidChange, |
| 69 base::Unretained(this))); |
| 70 // Report initial value. |
| 71 DidChange(g_battery_status); |
| 72 } |
| 73 } |
| 74 |
| 62 void DidChange(const device::BatteryStatus& battery_status) { | 75 void DidChange(const device::BatteryStatus& battery_status) { |
| 63 device::BatteryStatusPtr status(device::BatteryStatus::New()); | 76 if (!callback_.is_null()) { |
| 64 *status = battery_status; | 77 callback_.Run(battery_status.Clone()); |
| 65 binding_.client()->DidChange(status.Pass()); | 78 callback_.reset(); |
| 79 } |
| 66 } | 80 } |
| 67 | 81 |
| 68 scoped_ptr<BatteryUpdateSubscription> subscription_; | 82 scoped_ptr<BatteryUpdateSubscription> subscription_; |
| 69 mojo::StrongBinding<BatteryMonitor> binding_; | 83 mojo::StrongBinding<BatteryMonitor> binding_; |
| 84 BatteryStatusCallback callback_; |
| 70 }; | 85 }; |
| 71 | 86 |
| 72 // Overrides the default service implementation with the test implementation | 87 // Overrides the default service implementation with the test implementation |
| 73 // declared above. | 88 // declared above. |
| 74 class TestContentBrowserClient : public ContentBrowserClient { | 89 class TestContentBrowserClient : public ContentBrowserClient { |
| 75 public: | 90 public: |
| 76 void OverrideRenderProcessMojoServices(ServiceRegistry* registry) override { | 91 void OverrideRenderProcessMojoServices(ServiceRegistry* registry) override { |
| 77 registry->AddService(base::Bind(&FakeBatteryMonitor::Create)); | 92 registry->AddService(base::Bind(&FakeBatteryMonitor::Create)); |
| 78 } | 93 } |
| 79 }; | 94 }; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); | 158 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); |
| 144 status.level = 0.6; | 159 status.level = 0.6; |
| 145 UpdateBattery(status); | 160 UpdateBattery(status); |
| 146 same_tab_observer2.Wait(); | 161 same_tab_observer2.Wait(); |
| 147 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 162 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 148 } | 163 } |
| 149 | 164 |
| 150 } // namespace | 165 } // namespace |
| 151 | 166 |
| 152 } // namespace content | 167 } // namespace content |
| OLD | NEW |