OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "mojo/application/app_lifetime_helper.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "mojo/application/public/cpp/application_impl.h" | |
10 | |
11 namespace mojo { | |
12 | |
13 AppRefCount::AppRefCount( | |
14 AppLifetimeHelper* app_lifetime_helper, | |
15 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner) | |
16 : app_lifetime_helper_(app_lifetime_helper), | |
17 app_task_runner_(app_task_runner) { | |
18 } | |
19 | |
20 AppRefCount::~AppRefCount() { | |
21 if (app_task_runner_->BelongsToCurrentThread()) { | |
22 app_lifetime_helper_->Release(); | |
23 return; | |
24 } | |
25 | |
26 app_task_runner_->PostTask( | |
27 FROM_HERE, | |
28 base::Bind(&AppLifetimeHelper::Release, | |
29 base::Unretained(app_lifetime_helper_))); | |
30 } | |
31 | |
32 scoped_ptr<AppRefCount> AppRefCount::Clone() { | |
33 if (app_task_runner_->BelongsToCurrentThread()) { | |
34 app_lifetime_helper_->AddRef(); | |
35 } else { | |
36 app_task_runner_->PostTask( | |
37 FROM_HERE, | |
38 base::Bind(&AppLifetimeHelper::AddRef, | |
yzshen1
2015/05/15 06:56:12
Is it possible to have races because of this AddRe
jam
2015/05/15 07:08:44
I had thought about this, but I convinced myself t
| |
39 base::Unretained(app_lifetime_helper_))); | |
40 } | |
41 | |
42 return make_scoped_ptr(new AppRefCount( | |
43 app_lifetime_helper_, app_task_runner_)); | |
44 } | |
45 | |
46 AppLifetimeHelper::AppLifetimeHelper() | |
47 : ref_count_(0) { | |
48 } | |
49 | |
50 AppLifetimeHelper::~AppLifetimeHelper() { | |
51 } | |
52 | |
53 scoped_ptr<AppRefCount> AppLifetimeHelper::CreateAppRefCount() { | |
54 AddRef(); | |
55 return make_scoped_ptr(new AppRefCount( | |
56 this, base::MessageLoop::current()->task_runner())); | |
57 } | |
58 | |
59 void AppLifetimeHelper::AddRef() { | |
60 ref_count_++; | |
61 } | |
62 | |
63 void AppLifetimeHelper::Release() { | |
64 if (!--ref_count_) | |
65 ApplicationImpl::Terminate(); | |
66 } | |
67 | |
68 } // namespace mojo | |
OLD | NEW |