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 #ifndef MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ | |
6 #define MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 | |
12 namespace mojo { | |
13 | |
14 class ApplicationImpl; | |
15 class AppLifetimeHelper; | |
16 | |
17 // A service implementation should keep this object as a member variable to hold | |
18 // a reference on the application. | |
19 // Since services can live on different threads than the app, this class is | |
20 // safe to use on any thread. However, each instance should only be used on one | |
21 // thread at a time (otherwise there'll be races between the addref resulting | |
22 // from cloning and destruction). | |
23 class AppRefCount { | |
24 public: | |
25 ~AppRefCount(); | |
26 | |
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 | |
29 // holding on to the service but keeps the reference to the object, the app is | |
30 // still alive. | |
31 scoped_ptr<AppRefCount> Clone(); | |
32 | |
33 private: | |
34 friend AppLifetimeHelper; | |
35 | |
36 AppRefCount(AppLifetimeHelper* app_lifetime_helper, | |
37 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner); | |
38 | |
39 // Don't need to use weak ptr because if the app thread is alive that means | |
40 // app is. | |
41 AppLifetimeHelper* app_lifetime_helper_; | |
42 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner_; | |
43 | |
44 #ifndef NDEBUG | |
45 scoped_refptr<base::SingleThreadTaskRunner> clone_task_runner_; | |
46 #endif | |
47 | |
48 DISALLOW_COPY_AND_ASSIGN(AppRefCount); | |
49 }; | |
50 | |
51 // This is a helper class for apps to manage their lifetime, specifically so | |
52 // 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 | |
54 // creates an instance of a service, it gives it a refcount using | |
55 // CreateAppRefCount. The service implementation then keeps that object as a | |
56 // member variable. When all the service implemenations go away, the app will be | |
57 // quit with a call to mojo::ApplicationImpl::Terminate(). | |
58 class AppLifetimeHelper { | |
59 public: | |
60 explicit AppLifetimeHelper(ApplicationImpl* app); | |
61 ~AppLifetimeHelper(); | |
62 | |
63 scoped_ptr<AppRefCount> CreateAppRefCount(); | |
64 | |
65 private: | |
66 friend AppRefCount; | |
67 void AddRef(); | |
68 void Release(); | |
69 | |
70 friend ApplicationImpl; | |
71 void OnQuit(); | |
72 | |
73 ApplicationImpl* app_; | |
74 int ref_count_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(AppLifetimeHelper); | |
77 }; | |
78 | |
79 } // namespace mojo | |
80 | |
81 #endif // MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ | |
OLD | NEW |