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/synchronization/lock.h" 9 10 namespace base { 11 namespace ios { 12 13 // This class attempts to allow the application to continue to run for a period 14 // of time after it transitions to the background. The construction of an 15 // instance of this class marks the beginning of a task that needs background 16 // running time when the application is moved to the background and the 17 // destruction marks the end of such a task. 18 // 19 // Note there is no guarantee that the task will continue to finish when the 20 // application is moved to the background. 21 // 22 // This class should be used at times where leaving a task unfinished might be 23 // detrimental to user experience. For example, it should be used to ensure that 24 // the application has enough time to save important data or at least attempt to 25 // save such data. 26 class ScopedCriticalAction { 27 public: 28 ScopedCriticalAction(); 29 ~ScopedCriticalAction(); 30 31 private: 32 // Informs the OS that the background task has completed. 33 void EndBackgroundTask(); 34 35 // |UIBackgroundTaskIdentifier| returned by 36 // |beginBackgroundTaskWithExpirationHandler:| when marking the beginning of 37 // a long-running background task. It is defined as an |unsigned int| instead 38 // of a |UIBackgroundTaskIdentifier| so this class can be used in .cc files. 39 unsigned int background_task_id_; 40 Lock background_task_id_lock_; 41 42 DISALLOW_COPY_AND_ASSIGN(ScopedCriticalAction); 43 }; 44 45 } // namespace ios 46 } // namespace base 47 48 #endif // BASE_IOS_SCOPED_CRITICAL_ACTION_H_ 49