scoped_critical_action.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/memory/ref_counted.h"
  7. #include "base/strings/string_piece_forward.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(const ScopedCriticalAction&) = delete;
  28. ScopedCriticalAction& operator=(const ScopedCriticalAction&) = delete;
  29. ~ScopedCriticalAction();
  30. private:
  31. // Core logic; ScopedCriticalAction should not be reference counted so
  32. // that it follows the normal pattern of stack-allocating ScopedFoo objects,
  33. // but the expiration handler needs to have a reference counted object to
  34. // refer to.
  35. class Core : public base::RefCountedThreadSafe<Core> {
  36. public:
  37. Core();
  38. Core(const Core&) = delete;
  39. Core& operator=(const Core&) = delete;
  40. // Informs the OS that the background task has started. This is a
  41. // static method to ensure that the instance has a non-zero refcount.
  42. // |task_name| is used by the OS to log any leaked background tasks.
  43. static void StartBackgroundTask(scoped_refptr<Core> core,
  44. StringPiece task_name);
  45. // Informs the OS that the background task has completed. This is a
  46. // static method to ensure that the instance has a non-zero refcount.
  47. static void EndBackgroundTask(scoped_refptr<Core> core);
  48. private:
  49. friend base::RefCountedThreadSafe<Core>;
  50. ~Core();
  51. // |UIBackgroundTaskIdentifier| returned by
  52. // |beginBackgroundTaskWithName:expirationHandler:| when marking the
  53. // beginning of a long-running background task. It is defined as a uint64_t
  54. // instead of a |UIBackgroundTaskIdentifier| so this class can be used in
  55. // .cc files.
  56. uint64_t background_task_id_ GUARDED_BY(background_task_id_lock_);
  57. Lock background_task_id_lock_;
  58. };
  59. // The instance of the core that drives the background task.
  60. scoped_refptr<Core> core_;
  61. };
  62. } // namespace ios
  63. } // namespace base
  64. #endif // BASE_IOS_SCOPED_CRITICAL_ACTION_H_