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

Side by Side Diff: base/power_monitor/power_monitor_mac.mm

Issue 10959020: SystemMonitor refactoring: move power state monitor into a separate class called PowerMonitor (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Revert using Singleton pattern for PowerMonitor Created 8 years, 1 month 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // Implementation based on sample code from 5 // Implementation based on sample code from
6 // http://developer.apple.com/library/mac/#qa/qa1340/_index.html. 6 // http://developer.apple.com/library/mac/#qa/qa1340/_index.html.
7 7
8 #include "base/system_monitor/system_monitor.h" 8 #include "base/power_monitor/power_monitor.h"
9 9
10 #include <IOKit/pwr_mgt/IOPMLib.h> 10 #include <IOKit/pwr_mgt/IOPMLib.h>
11 #include <IOKit/IOMessage.h> 11 #include <IOKit/IOMessage.h>
12 12
13 namespace base { 13 namespace base {
14 14
15 namespace { 15 namespace {
16 16
17 io_connect_t g_system_power_io_port = 0; 17 io_connect_t g_system_power_io_port = 0;
18 IONotificationPortRef g_notification_port_ref = 0; 18 IONotificationPortRef g_notification_port_ref = 0;
19 io_object_t g_notifier_object = 0; 19 io_object_t g_signaler_object = 0;
vandebo (ex-Chrome) 2012/10/29 18:04:38 no reason to change the name of this variable.
Hongbo Min 2012/10/30 14:33:47 It mainly arises from the grep and replace action
20 PowerMonitor::Signaler* g_power_signaler = NULL;
20 21
21 void SystemPowerEventCallback(void*, 22 void SystemPowerEventCallback(void*,
22 io_service_t service, 23 io_service_t service,
23 natural_t message_type, 24 natural_t message_type,
24 void* message_argument) { 25 void* message_argument) {
25 SystemMonitor* sys_monitor = SystemMonitor::Get(); 26
26 DCHECK(sys_monitor); 27 CHECK(g_power_signaler);
27 switch (message_type) { 28 switch (message_type) {
28 case kIOMessageSystemWillSleep: 29 case kIOMessageSystemWillSleep:
29 sys_monitor->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); 30 g_power_signaler->ProcessPowerEvent(base::PowerMonitor::SUSPEND_EVENT);
30 IOAllowPowerChange(g_system_power_io_port, 31 IOAllowPowerChange(g_system_power_io_port,
31 reinterpret_cast<intptr_t>(message_argument)); 32 reinterpret_cast<intptr_t>(message_argument));
32 break; 33 break;
33 34
34 case kIOMessageSystemWillPowerOn: 35 case kIOMessageSystemWillPowerOn:
35 sys_monitor->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); 36 g_power_signaler->ProcessPowerEvent(PowerMonitor::RESUME_EVENT);
36 break; 37 break;
37 } 38 }
38 } 39 }
39 40
40 } // namespace 41 } // namespace
41 42
42 // The reason we can't include this code in the constructor is because 43 // The reason we can't include this code in the constructor is because
43 // PlatformInit() requires an active runloop and the IO port needs to be 44 // PlatformInit() requires an active runloop and the IO port needs to be
44 // allocated at sandbox initialization time, before there's a runloop. 45 // allocated at sandbox initialization time, before there's a runloop.
45 // See crbug.com/83783 . 46 // See crbug.com/83783 .
46 47
47 // static 48 // static
48 void SystemMonitor::AllocateSystemIOPorts() { 49 void PowerMonitor::AllocateSystemIOPorts() {
49 DCHECK_EQ(g_system_power_io_port, 0u); 50 if (g_system_power_io_port != 0u)
51 return;
50 52
51 // Notification port allocated by IORegisterForSystemPower. 53 // Notification port allocated by IORegisterForSystemPower.
52 54
53 g_system_power_io_port = IORegisterForSystemPower( 55 g_system_power_io_port = IORegisterForSystemPower(
54 NULL, &g_notification_port_ref, SystemPowerEventCallback, 56 NULL, &g_notification_port_ref, SystemPowerEventCallback,
55 &g_notifier_object); 57 &g_signaler_object);
vandebo (ex-Chrome) 2012/10/29 18:04:38 and here
Hongbo Min 2012/10/30 14:33:47 Done.
56 58
57 DCHECK_NE(g_system_power_io_port, 0u); 59 DCHECK_NE(g_system_power_io_port, 0u);
58 } 60 }
59 61
60 void SystemMonitor::PlatformInit() { 62 void PowerMonitor::PlatformInit() {
61 // Need to call AllocateSystemIOPorts() before constructing a SystemMonitor 63 // Need to call AllocateSystemIOPorts() before creating a PowerMonitor
62 // object. 64 // object.
63 DCHECK_NE(g_system_power_io_port, 0u); 65 CHECK(!g_power_signaler);
64 if (g_system_power_io_port == 0) 66 g_power_signaler = PowerMonitor::Get()->GetSignalerOnce();
65 return; 67 CHECK_NE(g_system_power_io_port, 0u);
vandebo (ex-Chrome) 2012/10/29 18:04:38 Move this just under the comment online 64
Hongbo Min 2012/10/30 14:33:47 Done.
66
67 // Add the notification port to the application runloop 68 // Add the notification port to the application runloop
68 CFRunLoopAddSource( 69 CFRunLoopAddSource(
69 CFRunLoopGetCurrent(), 70 CFRunLoopGetCurrent(),
70 IONotificationPortGetRunLoopSource(g_notification_port_ref), 71 IONotificationPortGetRunLoopSource(g_notification_port_ref),
71 kCFRunLoopCommonModes); 72 kCFRunLoopCommonModes);
72 } 73 }
73 74
74 void SystemMonitor::PlatformDestroy() { 75
75 DCHECK_NE(g_system_power_io_port, 0u); 76 void PowerMonitor::PlatformDestroy() {
vandebo (ex-Chrome) 2012/10/29 18:04:38 Maybe PlatformDestroy should clean up g_power_sign
Hongbo Min 2012/10/30 14:33:47 I don't clean it in PlatformDestroy. The reason is
vandebo (ex-Chrome) 2012/10/30 20:41:24 I'm ok having PlatformInit/PlatformDestroy on all
76 if (g_system_power_io_port == 0) 77 CHECK_NE(g_system_power_io_port, 0u);
vandebo (ex-Chrome) 2012/10/29 18:04:38 Leave these lines in.
Hongbo Min 2012/10/30 14:33:47 Done.
vandebo (ex-Chrome) 2012/10/29 18:04:38 Levae this as a DCHECK
Hongbo Min 2012/10/30 14:33:47 Done.
77 return;
78 78
79 // Remove the sleep notification port from the application runloop 79 // Remove the sleep notification port from the application runloop
80 CFRunLoopRemoveSource( 80 CFRunLoopRemoveSource(
81 CFRunLoopGetCurrent(), 81 CFRunLoopGetCurrent(),
82 IONotificationPortGetRunLoopSource(g_notification_port_ref), 82 IONotificationPortGetRunLoopSource(g_notification_port_ref),
83 kCFRunLoopCommonModes); 83 kCFRunLoopCommonModes);
84 84
85 // Deregister for system sleep notifications 85 // Deregister for system sleep notifications
86 IODeregisterForSystemPower(&g_notifier_object); 86 IODeregisterForSystemPower(&g_signaler_object);
vandebo (ex-Chrome) 2012/10/29 18:04:38 and here
Hongbo Min 2012/10/30 14:33:47 Done.
87 87
88 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService, 88 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService,
89 // so we close it here. 89 // so we close it here.
90 IOServiceClose(g_system_power_io_port); 90 IOServiceClose(g_system_power_io_port);
91 91
92 g_system_power_io_port = 0; 92 g_system_power_io_port = 0;
93 93
94 // Destroy the notification port allocated by IORegisterForSystemPower. 94 // Destroy the notification port allocated by IORegisterForSystemPower.
95 IONotificationPortDestroy(g_notification_port_ref); 95 IONotificationPortDestroy(g_notification_port_ref);
96 } 96 }
97 97
98 } // namespace base 98 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698