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 #ifndef MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ | 5 #ifndef MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ |
6 #define MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ | 6 #define MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 // When a service creates another object that is held by the client, it should | 27 // When a service creates another object that is held by the client, it should |
28 // also vend to it a refcount using this method. That way if the caller stops | 28 // also vend to it a refcount using this method. That way if the caller stops |
29 // holding on to the service but keeps the reference to the object, the app is | 29 // holding on to the service but keeps the reference to the object, the app is |
30 // still alive. | 30 // still alive. |
31 scoped_ptr<AppRefCount> Clone(); | 31 scoped_ptr<AppRefCount> Clone(); |
32 | 32 |
33 private: | 33 private: |
34 friend AppLifetimeHelper; | 34 friend AppLifetimeHelper; |
35 | 35 |
36 AppRefCount(AppLifetimeHelper* app_lifetime_helper, | 36 AppRefCount(AppLifetimeHelper* app_lifetime_helper, |
| 37 base::TimeDelta release_delay_, |
37 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner); | 38 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner); |
38 | 39 |
39 // Don't need to use weak ptr because if the app thread is alive that means | 40 // Don't need to use weak ptr because if the app thread is alive that means |
40 // app is. | 41 // app is. |
41 AppLifetimeHelper* app_lifetime_helper_; | 42 AppLifetimeHelper* app_lifetime_helper_; |
| 43 |
| 44 // Delay before releasing the ref-count. |
| 45 base::TimeDelta release_delay_; |
| 46 |
42 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner_; | 47 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner_; |
43 | 48 |
44 #ifndef NDEBUG | 49 #ifndef NDEBUG |
45 scoped_refptr<base::SingleThreadTaskRunner> clone_task_runner_; | 50 scoped_refptr<base::SingleThreadTaskRunner> clone_task_runner_; |
46 #endif | 51 #endif |
47 | 52 |
48 DISALLOW_COPY_AND_ASSIGN(AppRefCount); | 53 DISALLOW_COPY_AND_ASSIGN(AppRefCount); |
49 }; | 54 }; |
50 | 55 |
51 // This is a helper class for apps to manage their lifetime, specifically so | 56 // This is a helper class for apps to manage their lifetime, specifically so |
52 // apps can quit when they're not used anymore. | 57 // apps can quit when they're not used anymore. |
53 // An app can contain an object of this class as a member variable. Each time it | 58 // An app can contain an object of this class as a member variable. Each time it |
54 // creates an instance of a service, it gives it a refcount using | 59 // creates an instance of a service, it gives it a refcount using |
55 // CreateAppRefCount. The service implementation then keeps that object as a | 60 // CreateAppRefCount. The service implementation then keeps that object as a |
56 // member variable. When all the service implemenations go away, the app will be | 61 // member variable. When all the service implemenations go away, the app will be |
57 // quit with a call to mojo::ApplicationImpl::Terminate(). | 62 // quit with a call to mojo::ApplicationImpl::Terminate(). |
58 class AppLifetimeHelper { | 63 class AppLifetimeHelper { |
59 public: | 64 public: |
60 explicit AppLifetimeHelper(ApplicationImpl* app); | 65 explicit AppLifetimeHelper(ApplicationImpl* app); |
61 ~AppLifetimeHelper(); | 66 ~AppLifetimeHelper(); |
62 | 67 |
63 scoped_ptr<AppRefCount> CreateAppRefCount(); | 68 scoped_ptr<AppRefCount> CreateAppRefCount(); |
| 69 scoped_ptr<AppRefCount> CreateAppRefCount(base::TimeDelta release_delay); |
64 | 70 |
65 private: | 71 private: |
66 friend AppRefCount; | 72 friend AppRefCount; |
67 void AddRef(); | 73 void AddRef(); |
68 void Release(); | 74 void Release(); |
69 | 75 |
70 friend ApplicationImpl; | 76 friend ApplicationImpl; |
71 void ApplicationTerminated(); | 77 void ApplicationTerminated(); |
72 | 78 |
73 ApplicationImpl* app_; | 79 ApplicationImpl* app_; |
74 int ref_count_; | 80 int ref_count_; |
75 | 81 |
76 DISALLOW_COPY_AND_ASSIGN(AppLifetimeHelper); | 82 DISALLOW_COPY_AND_ASSIGN(AppLifetimeHelper); |
77 }; | 83 }; |
78 | 84 |
79 } // namespace mojo | 85 } // namespace mojo |
80 | 86 |
81 #endif // MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ | 87 #endif // MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ |
OLD | NEW |