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_APP_LIFETIME_HELPER_H_ |
| 6 #define MOJO_APPLICATION_APP_LIFETIME_HELPER_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 |
| 12 namespace mojo { |
| 13 |
| 14 class AppLifetimeHelper; |
| 15 |
| 16 // A service implementation should keep this object as a member variable to hold |
| 17 // a reference on the application. |
| 18 // Since services can live on different threads than the app, this class is |
| 19 // safe to use on any thread. |
| 20 class AppRefCount { |
| 21 public: |
| 22 ~AppRefCount(); |
| 23 |
| 24 // When a service creates another object that is held by the client, it should |
| 25 // also vend to it a refcount using this method. That way if the caller stops |
| 26 // holding on to the service but keeps the reference to the object, the app is |
| 27 // still alive. |
| 28 scoped_ptr<AppRefCount> Clone(); |
| 29 |
| 30 private: |
| 31 friend AppLifetimeHelper; |
| 32 |
| 33 AppRefCount(AppLifetimeHelper* app_lifetime_helper, |
| 34 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner); |
| 35 |
| 36 // Don't need to use weak ptr because if the app thread is alive that means |
| 37 // app is. |
| 38 AppLifetimeHelper* app_lifetime_helper_; |
| 39 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner_; |
| 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(AppRefCount); |
| 42 }; |
| 43 |
| 44 // This is a helper class for apps to manage their lifetime, specifically so |
| 45 // apps can quit when they're not used anymore. |
| 46 // An app can contain an object of this class as a member variable. Each time it |
| 47 // creates an instance of a service, it gives it a refcount using |
| 48 // CreateAppRefCount. The service implementation then keeps that object as a |
| 49 // member variable. When all the service implemenations go away, the app will be |
| 50 // quit with a call to mojo::ApplicationImpl::Terminate(). |
| 51 class AppLifetimeHelper { |
| 52 public: |
| 53 AppLifetimeHelper(); |
| 54 ~AppLifetimeHelper(); |
| 55 |
| 56 scoped_ptr<AppRefCount> CreateAppRefCount(); |
| 57 |
| 58 private: |
| 59 friend AppRefCount; |
| 60 void AddRef(); |
| 61 void Release(); |
| 62 |
| 63 int ref_count_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(AppLifetimeHelper); |
| 66 }; |
| 67 |
| 68 } // namespace mojo |
| 69 |
| 70 #endif // MOJO_APPLICATION_APP_LIFETIME_HELPER_H_ |
OLD | NEW |