Commit ece963bf authored by lgrey's avatar lgrey Committed by Commit Bot

[Mac] Wrap NSUserActivity begin/end so that it can be called from common files

This is prep for turning on AppNap as a variation of the
MacAllowBackgroundingProcesses experiment.

BUG=460102

Review-Url: https://codereview.chromium.org/2913883002
Cr-Commit-Position: refs/heads/master@{#476379}
parent 7d0b5dfa
...@@ -204,6 +204,8 @@ source_set("common") { ...@@ -204,6 +204,8 @@ source_set("common") {
"inter_process_time_ticks_converter.h", "inter_process_time_ticks_converter.h",
"layer_tree_settings_factory.cc", "layer_tree_settings_factory.cc",
"layer_tree_settings_factory.h", "layer_tree_settings_factory.h",
"mac/app_nap_activity.h",
"mac/app_nap_activity.mm",
"mac/attributed_string_coder.h", "mac/attributed_string_coder.h",
"mac/attributed_string_coder.mm", "mac/attributed_string_coder.mm",
"mac/font_descriptor.h", "mac/font_descriptor.h",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_COMMON_MAC_APP_NAP_ACTIVITY_H
#define CONTENT_COMMON_MAC_APP_NAP_ACTIVITY_H
#include <memory>
#include "base/macros.h"
#include "content/common/content_export.h"
namespace content {
// Can't import scoped_nsobject here, so wrap it.
struct AssertionWrapper;
// A wrapper around the macOS "activity" system, which is required to
// make renderers eligible for AppNap.
//
// When doing work, processes are expected to begin an activity, receiving
// an opaque token called an "assertion". On finishing, they end the activity.
// When a process has no outstanding assertions, it becomes eligible for
// AppNap.
class CONTENT_EXPORT AppNapActivity {
public:
AppNapActivity();
~AppNapActivity();
// Begin an activity and store the provided token.
void Begin();
// End the activity represented by |assertion_|.
void End();
private:
// An opaque token provided by the OS on beginning an activity.
std::unique_ptr<AssertionWrapper> assertion_;
DISALLOW_COPY_AND_ASSIGN(AppNapActivity);
};
} // namespace content
#endif // CONTENT_COMMON_MAC_APP_NAP_ACTIVITY_H
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/common/mac/app_nap_activity.h"
#import <Foundation/Foundation.h>
#include "base/mac/scoped_nsobject.h"
namespace content {
namespace {
NSString* const kActivityReason = @"Process foregrounded";
const NSActivityOptions kActivityOptions =
(NSActivityUserInitiatedAllowingIdleSystemSleep |
NSActivityLatencyCritical) &
~(NSActivitySuddenTerminationDisabled |
NSActivityAutomaticTerminationDisabled);
} // namespace
struct AssertionWrapper {
base::scoped_nsobject<id> obj;
};
AppNapActivity::AppNapActivity() {
assertion_.reset(new AssertionWrapper());
};
AppNapActivity::~AppNapActivity() {
DCHECK(!assertion_->obj.get());
};
void AppNapActivity::Begin() {
DCHECK(!assertion_->obj.get());
id assertion =
[[NSProcessInfo processInfo] beginActivityWithOptions:kActivityOptions
reason:kActivityReason];
assertion_->obj.reset([assertion retain]);
}
void AppNapActivity::End() {
id assertion = assertion_->obj.autorelease();
DCHECK(assertion);
[[NSProcessInfo processInfo] endActivity:assertion];
}
} // namespace content
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/common/mac/app_nap_activity.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
class AppNapActivityTest : public PlatformTest {};
TEST_F(AppNapActivityTest, StoresAssertion) {
const NSActivityOptions expectedOptions =
(NSActivityUserInitiatedAllowingIdleSystemSleep |
NSActivityLatencyCritical) &
~(NSActivitySuddenTerminationDisabled |
NSActivityAutomaticTerminationDisabled);
id processInfoMock =
[OCMockObject partialMockForObject:[NSProcessInfo processInfo]];
id assertion = @"An activity assertion";
[[[processInfoMock expect] andReturn:assertion]
beginActivityWithOptions:expectedOptions
reason:OCMOCK_ANY];
content::AppNapActivity activity;
activity.Begin();
EXPECT_OCMOCK_VERIFY(processInfoMock);
[[processInfoMock expect] endActivity:assertion];
activity.End();
EXPECT_OCMOCK_VERIFY(processInfoMock);
[processInfoMock stopMocking];
}
...@@ -1372,6 +1372,7 @@ test("content_unittests") { ...@@ -1372,6 +1372,7 @@ test("content_unittests") {
"../common/input/input_param_traits_unittest.cc", "../common/input/input_param_traits_unittest.cc",
"../common/input/touch_event_stream_validator_unittest.cc", "../common/input/touch_event_stream_validator_unittest.cc",
"../common/inter_process_time_ticks_converter_unittest.cc", "../common/inter_process_time_ticks_converter_unittest.cc",
"../common/mac/app_nap_activity_unittest.mm",
"../common/mac/attributed_string_coder_unittest.mm", "../common/mac/attributed_string_coder_unittest.mm",
"../common/mac/font_descriptor_unittest.mm", "../common/mac/font_descriptor_unittest.mm",
"../common/manifest_util_unittest.cc", "../common/manifest_util_unittest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment