Index: base/ios/scoped_critical_action.mm |
diff --git a/base/ios/scoped_critical_action.mm b/base/ios/scoped_critical_action.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..32f199bef408e619324facc95151aff148850e0f |
--- /dev/null |
+++ b/base/ios/scoped_critical_action.mm |
@@ -0,0 +1,51 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/ios/scoped_critical_action.h" |
+ |
+#import <UIKit/UIKit.h> |
stuartmorgan
2012/08/01 12:03:17
Blank line after this.
Chen Yu
2012/08/01 12:15:27
Done.
|
+#include "base/logging.h" |
+#include "base/synchronization/lock.h" |
+ |
+namespace base { |
+namespace ios { |
+ |
+// This implementation calls |beginBackgroundTaskWithExpirationHandler:| when |
+// instantiated and |endBackgroundTask:| when destroyed, creating a scope whose |
+// execution will continue (temporarily) even after the app is backgrounded. |
+ScopedCriticalAction::ScopedCriticalAction() { |
+ background_task_id_ = [[UIApplication sharedApplication] |
+ beginBackgroundTaskWithExpirationHandler:^{ |
+ DLOG(WARNING) << "Background task with id " << background_task_id_ |
+ << " expired."; |
+ // Note if |endBackgroundTask:| is not called for each task before time |
+ // expires, the system kills the application. |
+ EndBackgroundTask(); |
+ }]; |
+ if (background_task_id_ == UIBackgroundTaskInvalid) |
+ DLOG(WARNING) << "Running in the background is not possible."; |
stuartmorgan
2012/08/01 12:03:17
Be specific; "beginBackgroundTaskWithExpirationHan
Chen Yu
2012/08/01 12:15:27
Done.
|
+ else |
+ VLOG(3) << "Beginning background task with id " << background_task_id_; |
+} |
+ |
+ScopedCriticalAction::~ScopedCriticalAction() { |
+ EndBackgroundTask(); |
+} |
+ |
+void ScopedCriticalAction::EndBackgroundTask() { |
+ UIBackgroundTaskIdentifier task_id; |
+ { |
+ AutoLock lock_scope(background_task_id_lock_); |
+ if (background_task_id_ == UIBackgroundTaskInvalid) |
+ return; |
+ task_id = background_task_id_; |
+ background_task_id_ = UIBackgroundTaskInvalid; |
+ } |
+ |
+ VLOG(3) << "Ending background task with id " << task_id; |
+ [[UIApplication sharedApplication] endBackgroundTask:task_id]; |
+} |
+ |
+} // namespace ios |
+} // namespace base |