| 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_SHELL_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ | 5 #ifndef MOJO_SHELL_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ |
| 6 #define MOJO_SHELL_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ | 6 #define MOJO_SHELL_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.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" |
| 11 | 11 |
| 12 namespace mojo { | 12 namespace mojo { |
| 13 | 13 |
| 14 class Shell; |
| 14 class ShellConnection; | 15 class ShellConnection; |
| 15 class AppLifetimeHelper; | 16 class AppLifetimeHelper; |
| 16 | 17 |
| 17 // A service implementation should keep this object as a member variable to hold | 18 // A service implementation should keep this object as a member variable to hold |
| 18 // a reference on the application. | 19 // a reference on the application. |
| 19 // Since services can live on different threads than the app, this class is | 20 // 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 // 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 // thread at a time (otherwise there'll be races between the addref resulting |
| 22 // from cloning and destruction). | 23 // from cloning and destruction). |
| 23 class AppRefCount { | 24 class AppRefCount { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 50 | 51 |
| 51 // This is a helper class for apps to manage their lifetime, specifically so | 52 // This is a helper class for apps to manage their lifetime, specifically so |
| 52 // apps can quit when they're not used anymore. | 53 // 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 // 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 // creates an instance of a service, it gives it a refcount using |
| 55 // CreateAppRefCount. The service implementation then keeps that object as a | 56 // 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 // member variable. When all the service implemenations go away, the app will be |
| 57 // quit with a call to mojo::ShellConnection::Terminate(). | 58 // quit with a call to mojo::ShellConnection::Terminate(). |
| 58 class AppLifetimeHelper { | 59 class AppLifetimeHelper { |
| 59 public: | 60 public: |
| 60 explicit AppLifetimeHelper(ShellConnection* app); | 61 explicit AppLifetimeHelper(Shell* shell); |
| 61 ~AppLifetimeHelper(); | 62 ~AppLifetimeHelper(); |
| 62 | 63 |
| 63 scoped_ptr<AppRefCount> CreateAppRefCount(); | 64 scoped_ptr<AppRefCount> CreateAppRefCount(); |
| 64 | 65 |
| 65 private: | 66 private: |
| 66 friend AppRefCount; | 67 friend AppRefCount; |
| 67 void AddRef(); | 68 void AddRef(); |
| 68 void Release(); | 69 void Release(); |
| 69 | 70 |
| 70 friend ShellConnection; | 71 friend ShellConnection; |
| 71 void OnQuit(); | 72 void OnQuit(); |
| 72 | 73 |
| 73 ShellConnection* app_; | 74 Shell* shell_; |
| 74 int ref_count_; | 75 int ref_count_; |
| 75 | 76 |
| 76 DISALLOW_COPY_AND_ASSIGN(AppLifetimeHelper); | 77 DISALLOW_COPY_AND_ASSIGN(AppLifetimeHelper); |
| 77 }; | 78 }; |
| 78 | 79 |
| 79 } // namespace mojo | 80 } // namespace mojo |
| 80 | 81 |
| 81 #endif // MOJO_SHELL_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ | 82 #endif // MOJO_SHELL_PUBLIC_CPP_APP_LIFETIME_HELPER_H_ |
| OLD | NEW |