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

Side by Side Diff: ios/chrome/app/UIApplication+ExitsOnSuspend.mm

Issue 1110133002: [iOS] Upstream code in //ios/chrome/app (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 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 #if !defined(NDEBUG)
6
7 #import "ios/chrome/app/UIApplication+ExitsOnSuspend.h"
8
9 #include "base/ios/block_types.h"
10 #include "base/logging.h"
11 #include "base/threading/thread_restrictions.h"
12
13 NSString* const kExitsOnSuspend = @"EnableExitsOnSuspend";
14
15 namespace {
16 int backgroundTasksCount = 0;
17 UIBackgroundTaskIdentifier countTaskIdentifier = UIBackgroundTaskInvalid;
18
19 // Perform a block on the main thread. Asynchronously if the thread is not the
20 // main thread, synchronously if the thread is the main thread.
21 void ExecuteBlockOnMainThread(ProceduralBlock block) {
22 if ([NSThread isMainThread])
23 block();
24 else
25 dispatch_async(dispatch_get_main_queue(), block);
26 }
27 } // namespace
28
29 // Category defining interposing methods. These methods keep a tally of the
30 // background tasks count.
31 @implementation UIApplication (BackgroundTasksCounter)
32
33 // Method to replace -beginBackgroundTaskWithExpirationHandler:. The original
34 // method is called within.
35 - (UIBackgroundTaskIdentifier)
36 cr_interpose_beginBackgroundTaskWithExpirationHandler:
37 (ProceduralBlock)handler {
38 UIBackgroundTaskIdentifier identifier =
39 [self cr_interpose_beginBackgroundTaskWithExpirationHandler:handler];
40 if (identifier != UIBackgroundTaskInvalid)
sdefresne 2015/04/28 14:57:25 nit: please use {} as the code in if is complex
41 ExecuteBlockOnMainThread(^{
42 backgroundTasksCount++;
43 });
44 return identifier;
45 }
46
47 // Method to replace -endBackgroundTask:. The original method is called within.
48 - (void)cr_interpose_endBackgroundTask:(UIBackgroundTaskIdentifier)identifier {
49 if (identifier != UIBackgroundTaskInvalid)
50 ExecuteBlockOnMainThread(^{
51 backgroundTasksCount--;
52 });
53 [self cr_interpose_endBackgroundTask:identifier];
54 }
55
56 @end
57
58 @interface UIApplication (ExitsOnSuspend_Private)
59 // Terminate the app immediately. exit(0) is used.
60 - (void)cr_terminateImmediately;
61 // Terminate the app via -cr_terminateImmediately when the background tasks
62 // count is one. The remaining task is the count observation task.
63 - (void)cr_terminateWhenCountIsOne;
64 @end
65
66 @implementation UIApplication (ExitsOnSuspend)
67
68 - (void)cr_terminateWhenDoneWithBackgroundTasks {
69 // Add a background task for the count observation.
70 DCHECK(countTaskIdentifier == UIBackgroundTaskInvalid);
71 countTaskIdentifier = [self beginBackgroundTaskWithExpirationHandler:^{
72 // If we get to the end of the 10 minutes, exit.
73 [self cr_terminateImmediately];
74 }];
75
76 [self cr_terminateWhenCountIsOne];
77 }
78
79 - (void)cr_cancelTermination {
80 [NSObject
81 cancelPreviousPerformRequestsWithTarget:self
82 selector:@selector(
83 cr_terminateWhenCountIsOne)
84 object:nil];
85
86 // Cancel the count observation background task.
87 [self endBackgroundTask:countTaskIdentifier];
88 countTaskIdentifier = UIBackgroundTaskInvalid;
89 }
90
91 #pragma mark - Private
92
93 - (void)cr_terminateImmediately {
94 DVLOG(1) << "App exited when suspended after running background tasks.";
95 // exit(0) will trigger at_exit handlers. Some need to be run on a IOAllowed
96 // thread, such as file_util::MemoryMappedFile::CloseHandles().
97 base::ThreadRestrictions::SetIOAllowed(true);
98 exit(0);
99 }
100
101 - (void)cr_terminateWhenCountIsOne {
102 if (backgroundTasksCount <= 1)
103 [self cr_terminateImmediately];
104 [self performSelector:_cmd withObject:nil afterDelay:1];
105 }
106
107 @end
108
109 #endif // !defined(NDEBUG)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698