widget/cocoa/nsIdleServiceX.mm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "nsIdleServiceX.h"
     6 #include "nsObjCExceptions.h"
     7 #include "nsIServiceManager.h"
     8 #import <Foundation/Foundation.h>
    10 NS_IMPL_ISUPPORTS_INHERITED0(nsIdleServiceX, nsIdleService)
    12 bool
    13 nsIdleServiceX::PollIdleTime(uint32_t *aIdleTime)
    14 {
    15   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
    17   kern_return_t rval;
    18   mach_port_t masterPort;
    20   rval = IOMasterPort(kIOMasterPortDefault, &masterPort);
    21   if (rval != KERN_SUCCESS)
    22     return false;
    24   io_iterator_t hidItr;
    25   rval = IOServiceGetMatchingServices(masterPort,
    26                                       IOServiceMatching("IOHIDSystem"),
    27                                       &hidItr);
    29   if (rval != KERN_SUCCESS)
    30     return false;
    31   NS_ASSERTION(hidItr, "Our iterator is null, but it ought not to be!");
    33   io_registry_entry_t entry = IOIteratorNext(hidItr);
    34   NS_ASSERTION(entry, "Our IO Registry Entry is null, but it shouldn't be!");
    36   IOObjectRelease(hidItr);
    38   NSMutableDictionary *hidProps;
    39   rval = IORegistryEntryCreateCFProperties(entry,
    40                                            (CFMutableDictionaryRef*)&hidProps,
    41                                            kCFAllocatorDefault, 0);
    42   if (rval != KERN_SUCCESS)
    43     return false;
    44   NS_ASSERTION(hidProps, "HIDProperties is null, but no error was returned.");
    45   [hidProps autorelease];
    47   id idleObj = [hidProps objectForKey:@"HIDIdleTime"];
    48   NS_ASSERTION([idleObj isKindOfClass: [NSData class]] ||
    49                [idleObj isKindOfClass: [NSNumber class]],
    50                "What we got for the idle object is not what we expect!");
    52   uint64_t time;
    53   if ([idleObj isKindOfClass: [NSData class]])
    54     [idleObj getBytes: &time];
    55   else
    56     time = [idleObj unsignedLongLongValue];
    58   IOObjectRelease(entry);
    60   // convert to ms from ns
    61   time /= 1000000;
    62   if (time > UINT32_MAX) // Overflow will occur
    63     return false;
    65   *aIdleTime = static_cast<uint32_t>(time);
    67   return true;
    69   NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false);
    70 }
    72 bool
    73 nsIdleServiceX::UsePollMode()
    74 {
    75   return true;
    76 }

mercurial