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
3
// found in the LICENSE file.
5
#ifndef BASE_IOS_SCOPED_CRITICAL_ACTION_H_
6
#define BASE_IOS_SCOPED_CRITICAL_ACTION_H_
8
#include "base/memory/ref_counted.h"
9
#include "base/synchronization/lock.h"
14
// This class attempts to allow the application to continue to run for a period
15
// of time after it transitions to the background. The construction of an
16
// instance of this class marks the beginning of a task that needs background
17
// running time when the application is moved to the background and the
18
// destruction marks the end of such a task.
20
// Note there is no guarantee that the task will continue to finish when the
21
// application is moved to the background.
23
// This class should be used at times where leaving a task unfinished might be
24
// detrimental to user experience. For example, it should be used to ensure that
25
// the application has enough time to save important data or at least attempt to
27
class ScopedCriticalAction {
29
ScopedCriticalAction();
30
~ScopedCriticalAction();
33
// Core logic; ScopedCriticalAction should not be reference counted so
34
// that it follows the normal pattern of stack-allocating ScopedFoo objects,
35
// but the expiration handler needs to have a reference counted object to
37
class Core : public base::RefCountedThreadSafe<Core> {
41
// Informs the OS that the background task has completed.
42
void EndBackgroundTask();
45
friend base::RefCountedThreadSafe<Core>;
48
// |UIBackgroundTaskIdentifier| returned by
49
// |beginBackgroundTaskWithExpirationHandler:| when marking the beginning of
50
// a long-running background task. It is defined as an |unsigned int|
51
// instead of a |UIBackgroundTaskIdentifier| so this class can be used in
53
unsigned int background_task_id_;
54
Lock background_task_id_lock_;
56
DISALLOW_COPY_AND_ASSIGN(Core);
59
// The instance of the core that drives the background task.
60
scoped_refptr<Core> core_;
62
DISALLOW_COPY_AND_ASSIGN(ScopedCriticalAction);
68
#endif // BASE_IOS_SCOPED_CRITICAL_ACTION_H_