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. 4 5#ifndef BASE_IOS_SCOPED_CRITICAL_ACTION_H_ 6#define BASE_IOS_SCOPED_CRITICAL_ACTION_H_ 7 8#include "base/memory/ref_counted.h" 9#include "base/synchronization/lock.h" 10 11namespace base { 12namespace ios { 13 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. 19// 20// Note there is no guarantee that the task will continue to finish when the 21// application is moved to the background. 22// 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 26// save such data. 27class ScopedCriticalAction { 28 public: 29 ScopedCriticalAction(); 30 ~ScopedCriticalAction(); 31 32 private: 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 36 // refer to. 37 class Core : public base::RefCountedThreadSafe<Core> { 38 public: 39 Core(); 40 41 // Informs the OS that the background task has completed. 42 void EndBackgroundTask(); 43 44 private: 45 friend base::RefCountedThreadSafe<Core>; 46 ~Core(); 47 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 52 // .cc files. 53 unsigned int background_task_id_; 54 Lock background_task_id_lock_; 55 56 DISALLOW_COPY_AND_ASSIGN(Core); 57 }; 58 59 // The instance of the core that drives the background task. 60 scoped_refptr<Core> core_; 61 62 DISALLOW_COPY_AND_ASSIGN(ScopedCriticalAction); 63}; 64 65} // namespace ios 66} // namespace base 67 68#endif // BASE_IOS_SCOPED_CRITICAL_ACTION_H_ 69