blob: 9dad70ed6ba7ba14577ea867ddf6a6ac9fe1e6d6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
// 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>
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
namespace base {
namespace ios {
ScopedCriticalAction::ScopedCriticalAction()
: core_(new ScopedCriticalAction::Core()) {
}
ScopedCriticalAction::~ScopedCriticalAction() {
core_->EndBackgroundTask();
}
// 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::Core::Core() {
scoped_refptr<ScopedCriticalAction::Core> core = this;
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.
core->EndBackgroundTask();
}];
if (background_task_id_ == UIBackgroundTaskInvalid) {
DLOG(WARNING) <<
"beginBackgroundTaskWithExpirationHandler: returned an invalid ID";
} else {
VLOG(3) << "Beginning background task with id " << background_task_id_;
}
}
ScopedCriticalAction::Core::~Core() {
DCHECK_EQ(background_task_id_, UIBackgroundTaskInvalid);
}
void ScopedCriticalAction::Core::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
|