Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3298)

Unified Diff: base/ios/scoped_critical_action.mm

Issue 10835032: Add APIs to protect critical tasks on iOS. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address review comments Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« base/ios/scoped_critical_action.h ('K') | « base/ios/scoped_critical_action.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..28f9df7b6ff836214fcbd5c53cadefde348b74c4
--- /dev/null
+++ b/base/ios/scoped_critical_action.mm
@@ -0,0 +1,49 @@
+// 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"
+
+#include "base/logging.h"
+#include "base/synchronization/lock.h"
+
+#import <UIKit/UIKit.h>
Mark Mentovai 2012/07/31 23:16:33 C system includes before project includes, with th
Chen Yu 2012/08/01 11:26:13 Done.
+
+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]
Mark Mentovai 2012/07/31 23:16:33 This function returns a UIBackgroundTaskIdentifier
stuartmorgan 2012/08/01 04:31:55 Except it isn't, which is why we did this. It make
Mark Mentovai 2012/08/01 13:50:42 stuartmorgan wrote:
+ 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();
+ }];
+ VLOG(3) << "Beginning background task with id " << background_task_id_;
Mark Mentovai 2012/07/31 23:16:33 The docs say something about UIBackgroundTaskInval
Chen Yu 2012/08/01 11:26:13 There is no much we can do in this case; a warning
+}
+
+ScopedCriticalAction::~ScopedCriticalAction() {
+ EndBackgroundTask();
+}
+
+void ScopedCriticalAction::EndBackgroundTask() {
+ UIBackgroundTaskIdentifier task_id;
+ {
+ AutoLock lockScope(id_lock_);
+ task_id = background_task_id_;
+ background_task_id_ = UIBackgroundTaskInvalid;
+ }
+ if (task_id == UIBackgroundTaskInvalid)
+ return;
+
+ VLOG(3) << "Ending background task with id " << task_id;
+ [[UIApplication sharedApplication] endBackgroundTask:task_id];
+}
+
+} // namespace ios
+} // namespace base
« base/ios/scoped_critical_action.h ('K') | « base/ios/scoped_critical_action.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698