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

Side by Side Diff: device/battery/battery_monitor_impl.cc

Issue 2398833003: Fix double-delete in BatteryMonitor. (Closed)
Patch Set: Created 4 years, 2 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
« no previous file with comments | « device/battery/battery_monitor_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "device/battery/battery_monitor_impl.h" 5 #include "device/battery/battery_monitor_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
11 #include "mojo/public/cpp/bindings/strong_binding.h" 12 #include "mojo/public/cpp/bindings/strong_binding.h"
12 13
13 namespace device { 14 namespace device {
14 15
15 // static 16 // static
16 void BatteryMonitorImpl::Create(BatteryMonitorRequest request) { 17 void BatteryMonitorImpl::Create(BatteryMonitorRequest request) {
17 mojo::MakeStrongBinding(base::MakeUnique<BatteryMonitorImpl>(), 18 auto* impl = new BatteryMonitorImpl;
18 std::move(request)); 19 auto binding =
20 mojo::MakeStrongBinding(base::WrapUnique(impl), std::move(request));
21 impl->binding_ = binding;
19 } 22 }
20 23
21 BatteryMonitorImpl::BatteryMonitorImpl() : status_to_report_(false) { 24 BatteryMonitorImpl::BatteryMonitorImpl() : status_to_report_(false) {
22 // NOTE: DidChange may be called before AddCallback returns. This is done to 25 // NOTE: DidChange may be called before AddCallback returns. This is done to
23 // report current status. 26 // report current status.
24 subscription_ = BatteryStatusService::GetInstance()->AddCallback( 27 subscription_ = BatteryStatusService::GetInstance()->AddCallback(
25 base::Bind(&BatteryMonitorImpl::DidChange, base::Unretained(this))); 28 base::Bind(&BatteryMonitorImpl::DidChange, base::Unretained(this)));
26 } 29 }
27 30
28 BatteryMonitorImpl::~BatteryMonitorImpl() { 31 BatteryMonitorImpl::~BatteryMonitorImpl() {
29 } 32 }
30 33
31 void BatteryMonitorImpl::QueryNextStatus( 34 void BatteryMonitorImpl::QueryNextStatus(
32 const QueryNextStatusCallback& callback) { 35 const QueryNextStatusCallback& callback) {
33 if (!callback_.is_null()) { 36 if (!callback_.is_null()) {
34 DVLOG(1) << "Overlapped call to QueryNextStatus!"; 37 DVLOG(1) << "Overlapped call to QueryNextStatus!";
35 delete this; 38 binding_->Close();
36 return; 39 return;
37 } 40 }
38 callback_ = callback; 41 callback_ = callback;
39 42
40 if (status_to_report_) 43 if (status_to_report_)
41 ReportStatus(); 44 ReportStatus();
42 } 45 }
43 46
44 void BatteryMonitorImpl::RegisterSubscription() { 47 void BatteryMonitorImpl::RegisterSubscription() {
45 } 48 }
46 49
47 void BatteryMonitorImpl::DidChange(const BatteryStatus& battery_status) { 50 void BatteryMonitorImpl::DidChange(const BatteryStatus& battery_status) {
48 status_ = battery_status; 51 status_ = battery_status;
49 status_to_report_ = true; 52 status_to_report_ = true;
50 53
51 if (!callback_.is_null()) 54 if (!callback_.is_null())
52 ReportStatus(); 55 ReportStatus();
53 } 56 }
54 57
55 void BatteryMonitorImpl::ReportStatus() { 58 void BatteryMonitorImpl::ReportStatus() {
56 callback_.Run(status_.Clone()); 59 callback_.Run(status_.Clone());
57 callback_.Reset(); 60 callback_.Reset();
58 61
59 status_to_report_ = false; 62 status_to_report_ = false;
60 } 63 }
61 64
62 } // namespace device 65 } // namespace device
OLDNEW
« no previous file with comments | « device/battery/battery_monitor_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698