Chromium Code Reviews| Index: mojo/application/app_lifetime_helper.cc |
| diff --git a/mojo/application/app_lifetime_helper.cc b/mojo/application/app_lifetime_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bce4738a47aafe9fb978c3c3e1f0ee2d81750261 |
| --- /dev/null |
| +++ b/mojo/application/app_lifetime_helper.cc |
| @@ -0,0 +1,68 @@ |
| +// Copyright 2015 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 "mojo/application/app_lifetime_helper.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "mojo/application/public/cpp/application_impl.h" |
| + |
| +namespace mojo { |
| + |
| +AppRefCount::AppRefCount( |
| + AppLifetimeHelper* app_lifetime_helper, |
| + scoped_refptr<base::SingleThreadTaskRunner> app_task_runner) |
| + : app_lifetime_helper_(app_lifetime_helper), |
| + app_task_runner_(app_task_runner) { |
| +} |
| + |
| +AppRefCount::~AppRefCount() { |
| + if (app_task_runner_->BelongsToCurrentThread()) { |
| + app_lifetime_helper_->Release(); |
| + return; |
| + } |
| + |
| + app_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&AppLifetimeHelper::Release, |
| + base::Unretained(app_lifetime_helper_))); |
| +} |
| + |
| +scoped_ptr<AppRefCount> AppRefCount::Clone() { |
| + if (app_task_runner_->BelongsToCurrentThread()) { |
| + app_lifetime_helper_->AddRef(); |
| + } else { |
| + app_task_runner_->PostTask( |
| + FROM_HERE, |
| + 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
|
| + base::Unretained(app_lifetime_helper_))); |
| + } |
| + |
| + return make_scoped_ptr(new AppRefCount( |
| + app_lifetime_helper_, app_task_runner_)); |
| +} |
| + |
| +AppLifetimeHelper::AppLifetimeHelper() |
| + : ref_count_(0) { |
| +} |
| + |
| +AppLifetimeHelper::~AppLifetimeHelper() { |
| +} |
| + |
| +scoped_ptr<AppRefCount> AppLifetimeHelper::CreateAppRefCount() { |
| + AddRef(); |
| + return make_scoped_ptr(new AppRefCount( |
| + this, base::MessageLoop::current()->task_runner())); |
| +} |
| + |
| +void AppLifetimeHelper::AddRef() { |
| + ref_count_++; |
| +} |
| + |
| +void AppLifetimeHelper::Release() { |
| + if (!--ref_count_) |
| + ApplicationImpl::Terminate(); |
| +} |
| + |
| +} // namespace mojo |