scoped_critical_action.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #ifndef BASE_IOS_SCOPED_CRITICAL_ACTION_H_
  5. #define BASE_IOS_SCOPED_CRITICAL_ACTION_H_
  6. #include "base/macros.h"
  7. #include "base/memory/ref_counted.h"
  8. #include "base/synchronization/lock.h"
  9. namespace base {
  10. namespace ios {
  11. // This class attempts to allow the application to continue to run for a period
  12. // of time after it transitions to the background. The construction of an
  13. // instance of this class marks the beginning of a task that needs background
  14. // running time when the application is moved to the background and the
  15. // destruction marks the end of such a task.
  16. //
  17. // Note there is no guarantee that the task will continue to finish when the
  18. // application is moved to the background.
  19. //
  20. // This class should be used at times where leaving a task unfinished might be
  21. // detrimental to user experience. For example, it should be used to ensure that
  22. // the application has enough time to save important data or at least attempt to
  23. // save such data.
  24. class ScopedCriticalAction {
  25. public:
  26. ScopedCriticalAction(StringPiece task_name);
  27. ~ScopedCriticalAction();
  28. private:
  29. // Core logic; ScopedCriticalAction should not be reference counted so
  30. // that it follows the normal pattern of stack-allocating ScopedFoo objects,
  31. // but the expiration handler needs to have a reference counted object to
  32. // refer to.
  33. class Core : public base::RefCountedThreadSafe<Core> {
  34. public:
  35. Core();
  36. // Informs the OS that the background task has started. This is a
  37. // static method to ensure that the instance has a non-zero refcount.
  38. // |task_name| is used by the OS to log any leaked background tasks.
  39. static void StartBackgroundTask(scoped_refptr<Core> core,
  40. StringPiece task_name);
  41. // Informs the OS that the background task has completed. This is a
  42. // static method to ensure that the instance has a non-zero refcount.
  43. static void EndBackgroundTask(scoped_refptr<Core> core);
  44. private:
  45. friend base::RefCountedThreadSafe<Core>;
  46. ~Core();
  47. // |UIBackgroundTaskIdentifier| returned by
  48. // |beginBackgroundTaskWithName:expirationHandler:| when marking the
  49. // beginning of a long-running background task. It is defined as an
  50. // |unsigned int| instead of a |UIBackgroundTaskIdentifier| so this class
  51. // can be used in .cc files.
  52. unsigned int background_task_id_ GUARDED_BY(background_task_id_lock_);
  53. Lock background_task_id_lock_;
  54. DISALLOW_COPY_AND_ASSIGN(Core);
  55. };
  56. // The instance of the core that drives the background task.
  57. scoped_refptr<Core> core_;
  58. DISALLOW_COPY_AND_ASSIGN(ScopedCriticalAction);
  59. };
  60. } // namespace ios
  61. } // namespace base
  62. #endif // BASE_IOS_SCOPED_CRITICAL_ACTION_H_