| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/common/mac/app_nap_activity.h" | 5 #include "content/common/mac/app_nap_activity.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 | 8 |
| 9 #include "base/mac/scoped_nsobject.h" | 9 #include "base/mac/scoped_nsobject.h" |
| 10 | 10 |
| 11 extern "C" { |
| 12 void __CFRunLoopSetOptionsReason(uint64_t options, |
| 13 NSString* reason, |
| 14 int unused); |
| 15 } |
| 16 |
| 11 namespace content { | 17 namespace content { |
| 12 | 18 |
| 13 namespace { | 19 namespace { |
| 14 | 20 |
| 15 NSString* const kActivityReason = @"Process foregrounded"; | 21 NSString* const kActivityReason = @"Process foregrounded"; |
| 16 const NSActivityOptions kActivityOptions = | 22 const NSActivityOptions kActivityOptions = |
| 17 (NSActivityUserInitiatedAllowingIdleSystemSleep | | 23 (NSActivityUserInitiatedAllowingIdleSystemSleep | |
| 18 NSActivityLatencyCritical) & | 24 NSActivityLatencyCritical) & |
| 19 ~(NSActivitySuddenTerminationDisabled | | 25 ~(NSActivitySuddenTerminationDisabled | |
| 20 NSActivityAutomaticTerminationDisabled); | 26 NSActivityAutomaticTerminationDisabled); |
| 21 | 27 |
| 22 } // namespace | 28 } // namespace |
| 23 | 29 |
| 24 struct AssertionWrapper { | 30 struct AssertionWrapper { |
| 25 base::scoped_nsobject<id> obj; | 31 base::scoped_nsobject<id> obj; |
| 26 }; | 32 }; |
| 27 | 33 |
| 28 AppNapActivity::AppNapActivity() { | 34 AppNapActivity::AppNapActivity() { |
| 29 assertion_.reset(new AssertionWrapper()); | 35 assertion_.reset(new AssertionWrapper()); |
| 30 }; | 36 }; |
| 31 | 37 |
| 32 AppNapActivity::~AppNapActivity() { | 38 AppNapActivity::~AppNapActivity() { |
| 33 DCHECK(!assertion_->obj.get()); | 39 DCHECK(!assertion_->obj.get()); |
| 34 }; | 40 }; |
| 35 | 41 |
| 42 void AppNapActivity::InitializeAppNapSupport() { |
| 43 // Reason strings are the same as |
| 44 // what macOS sends in the corresponding call. |
| 45 // |options| (argument 1) are magic numbers as found in the |
| 46 // callsites mentioned above. |
| 47 // |
| 48 // Normally happens during launch services check-in. (HIToolbox) |
| 49 __CFRunLoopSetOptionsReason( |
| 50 1, @"Finished checking in as application - waiting for events", 0); |
| 51 // Normally happens in a dispatch_once in the NSApplication event loop. |
| 52 // (CoreFoundation). |
| 53 __CFRunLoopSetOptionsReason( |
| 54 0x3b000000, @"Finished delay after app launch and bundle check", 0); |
| 55 } |
| 56 |
| 36 void AppNapActivity::Begin() { | 57 void AppNapActivity::Begin() { |
| 37 DCHECK(!assertion_->obj.get()); | 58 DCHECK(!assertion_->obj.get()); |
| 38 id assertion = | 59 id assertion = |
| 39 [[NSProcessInfo processInfo] beginActivityWithOptions:kActivityOptions | 60 [[NSProcessInfo processInfo] beginActivityWithOptions:kActivityOptions |
| 40 reason:kActivityReason]; | 61 reason:kActivityReason]; |
| 41 assertion_->obj.reset([assertion retain]); | 62 assertion_->obj.reset([assertion retain]); |
| 42 } | 63 } |
| 43 | 64 |
| 44 void AppNapActivity::End() { | 65 void AppNapActivity::End() { |
| 45 id assertion = assertion_->obj.autorelease(); | 66 id assertion = assertion_->obj.autorelease(); |
| 46 DCHECK(assertion); | 67 DCHECK(assertion); |
| 47 [[NSProcessInfo processInfo] endActivity:assertion]; | 68 [[NSProcessInfo processInfo] endActivity:assertion]; |
| 48 } | 69 } |
| 49 | 70 |
| 50 } // namespace content | 71 } // namespace content |
| OLD | NEW |