OLD | NEW |
---|---|
(Empty) | |
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 #include "base/ios/scoped_critical_action.h" | |
6 | |
7 #import <UIKit/UIKit.h> | |
stuartmorgan
2012/08/01 12:03:17
Blank line after this.
Chen Yu
2012/08/01 12:15:27
Done.
| |
8 #include "base/logging.h" | |
9 #include "base/synchronization/lock.h" | |
10 | |
11 namespace base { | |
12 namespace ios { | |
13 | |
14 // This implementation calls |beginBackgroundTaskWithExpirationHandler:| when | |
15 // instantiated and |endBackgroundTask:| when destroyed, creating a scope whose | |
16 // execution will continue (temporarily) even after the app is backgrounded. | |
17 ScopedCriticalAction::ScopedCriticalAction() { | |
18 background_task_id_ = [[UIApplication sharedApplication] | |
19 beginBackgroundTaskWithExpirationHandler:^{ | |
20 DLOG(WARNING) << "Background task with id " << background_task_id_ | |
21 << " expired."; | |
22 // Note if |endBackgroundTask:| is not called for each task before time | |
23 // expires, the system kills the application. | |
24 EndBackgroundTask(); | |
25 }]; | |
26 if (background_task_id_ == UIBackgroundTaskInvalid) | |
27 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.
| |
28 else | |
29 VLOG(3) << "Beginning background task with id " << background_task_id_; | |
30 } | |
31 | |
32 ScopedCriticalAction::~ScopedCriticalAction() { | |
33 EndBackgroundTask(); | |
34 } | |
35 | |
36 void ScopedCriticalAction::EndBackgroundTask() { | |
37 UIBackgroundTaskIdentifier task_id; | |
38 { | |
39 AutoLock lock_scope(background_task_id_lock_); | |
40 if (background_task_id_ == UIBackgroundTaskInvalid) | |
41 return; | |
42 task_id = background_task_id_; | |
43 background_task_id_ = UIBackgroundTaskInvalid; | |
44 } | |
45 | |
46 VLOG(3) << "Ending background task with id " << task_id; | |
47 [[UIApplication sharedApplication] endBackgroundTask:task_id]; | |
48 } | |
49 | |
50 } // namespace ios | |
51 } // namespace base | |
OLD | NEW |