| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/application/public/cpp/app_lifetime_helper.h" | 5 #include "mojo/application/public/cpp/app_lifetime_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "mojo/application/public/cpp/application_impl.h" | 9 #include "mojo/application/public/cpp/application_impl.h" |
| 10 | 10 |
| 11 namespace mojo { | 11 namespace mojo { |
| 12 | 12 |
| 13 AppRefCount::AppRefCount( | 13 AppRefCount::AppRefCount( |
| 14 AppLifetimeHelper* app_lifetime_helper, | 14 AppLifetimeHelper* app_lifetime_helper, |
| 15 base::TimeDelta release_delay, |
| 15 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner) | 16 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner) |
| 16 : app_lifetime_helper_(app_lifetime_helper), | 17 : app_lifetime_helper_(app_lifetime_helper), |
| 18 release_delay_(release_delay), |
| 17 app_task_runner_(app_task_runner) { | 19 app_task_runner_(app_task_runner) { |
| 18 } | 20 } |
| 19 | 21 |
| 20 AppRefCount::~AppRefCount() { | 22 AppRefCount::~AppRefCount() { |
| 21 #ifndef NDEBUG | 23 #ifndef NDEBUG |
| 22 // Ensure that this object is used on only one thread at a time, or else there | 24 // Ensure that this object is used on only one thread at a time, or else there |
| 23 // could be races where the object is being reset on one thread and cloned on | 25 // could be races where the object is being reset on one thread and cloned on |
| 24 // another. | 26 // another. |
| 25 if (clone_task_runner_) { | 27 if (clone_task_runner_) { |
| 26 DCHECK(clone_task_runner_->BelongsToCurrentThread()); | 28 DCHECK(clone_task_runner_->BelongsToCurrentThread()); |
| 27 } | 29 } |
| 28 #endif | 30 #endif |
| 29 | 31 |
| 32 if (!release_delay_.is_zero()) { |
| 33 app_task_runner_->PostDelayedTask( |
| 34 FROM_HERE, base::Bind(&AppLifetimeHelper::Release, |
| 35 base::Unretained(app_lifetime_helper_)), |
| 36 release_delay_); |
| 37 return; |
| 38 } |
| 39 |
| 30 if (app_task_runner_->BelongsToCurrentThread()) { | 40 if (app_task_runner_->BelongsToCurrentThread()) { |
| 31 app_lifetime_helper_->Release(); | 41 app_lifetime_helper_->Release(); |
| 32 return; | 42 return; |
| 33 } | 43 } |
| 34 | 44 |
| 35 app_task_runner_->PostTask( | 45 app_task_runner_->PostTask( |
| 36 FROM_HERE, | 46 FROM_HERE, |
| 37 base::Bind(&AppLifetimeHelper::Release, | 47 base::Bind(&AppLifetimeHelper::Release, |
| 38 base::Unretained(app_lifetime_helper_))); | 48 base::Unretained(app_lifetime_helper_))); |
| 39 } | 49 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 52 // Ensure that this object is used on only one thread at a time, or else there | 62 // Ensure that this object is used on only one thread at a time, or else there |
| 53 // could be races where the object is being reset on one thread and cloned on | 63 // could be races where the object is being reset on one thread and cloned on |
| 54 // another. | 64 // another. |
| 55 if (clone_task_runner_) { | 65 if (clone_task_runner_) { |
| 56 DCHECK(clone_task_runner_->BelongsToCurrentThread()); | 66 DCHECK(clone_task_runner_->BelongsToCurrentThread()); |
| 57 } else { | 67 } else { |
| 58 clone_task_runner_ = base::MessageLoop::current()->task_runner(); | 68 clone_task_runner_ = base::MessageLoop::current()->task_runner(); |
| 59 } | 69 } |
| 60 #endif | 70 #endif |
| 61 | 71 |
| 62 return make_scoped_ptr(new AppRefCount( | 72 return make_scoped_ptr( |
| 63 app_lifetime_helper_, app_task_runner_)); | 73 new AppRefCount(app_lifetime_helper_, release_delay_, app_task_runner_)); |
| 64 } | 74 } |
| 65 | 75 |
| 66 AppLifetimeHelper::AppLifetimeHelper(ApplicationImpl* app) | 76 AppLifetimeHelper::AppLifetimeHelper(ApplicationImpl* app) |
| 67 : app_(app), ref_count_(0) { | 77 : app_(app), ref_count_(0) { |
| 68 } | 78 } |
| 69 | 79 |
| 70 AppLifetimeHelper::~AppLifetimeHelper() { | 80 AppLifetimeHelper::~AppLifetimeHelper() { |
| 71 } | 81 } |
| 72 | 82 |
| 73 scoped_ptr<AppRefCount> AppLifetimeHelper::CreateAppRefCount() { | 83 scoped_ptr<AppRefCount> AppLifetimeHelper::CreateAppRefCount() { |
| 84 return CreateAppRefCount(base::TimeDelta()); |
| 85 } |
| 86 |
| 87 scoped_ptr<AppRefCount> AppLifetimeHelper::CreateAppRefCount( |
| 88 base::TimeDelta release_delay) { |
| 74 AddRef(); | 89 AddRef(); |
| 75 return make_scoped_ptr(new AppRefCount( | 90 return make_scoped_ptr(new AppRefCount( |
| 76 this, base::MessageLoop::current()->task_runner())); | 91 this, release_delay, base::MessageLoop::current()->task_runner())); |
| 77 } | 92 } |
| 78 | 93 |
| 79 void AppLifetimeHelper::AddRef() { | 94 void AppLifetimeHelper::AddRef() { |
| 80 ref_count_++; | 95 ref_count_++; |
| 81 } | 96 } |
| 82 | 97 |
| 83 void AppLifetimeHelper::Release() { | 98 void AppLifetimeHelper::Release() { |
| 84 if (!--ref_count_) { | 99 if (!--ref_count_) { |
| 85 if (app_) | 100 if (app_) |
| 86 app_->Terminate(); | 101 app_->Terminate(); |
| 87 } | 102 } |
| 88 } | 103 } |
| 89 | 104 |
| 90 void AppLifetimeHelper::ApplicationTerminated() { | 105 void AppLifetimeHelper::ApplicationTerminated() { |
| 91 app_ = nullptr; | 106 app_ = nullptr; |
| 92 } | 107 } |
| 93 | 108 |
| 94 } // namespace mojo | 109 } // namespace mojo |
| OLD | NEW |