Chromium Code Reviews| 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/memory/weak_ptr.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 class AppRefCount { | |
| 19 public: | |
| 20 explicit AppRefCount( | |
| 21 const base::WeakPtr<AppLifetimeHelper>& app_lifetime_helper); | |
| 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 base::WeakPtr<AppLifetimeHelper> app_lifetime_helper_; | |
| 32 | |
| 33 DISALLOW_COPY_AND_ASSIGN(AppRefCount); | |
| 34 }; | |
| 35 | |
| 36 // This is a helper class for apps to manage their lifetime, specifically so | |
| 37 // apps can quit when they're not used anymore. | |
| 38 // An app can contain an object of this class as a member variable. Each time it | |
| 39 // creates an instance of a service, it gives it a refcount using | |
| 40 // CreateAppRefCount. The service implementation then keeps that object as a | |
| 41 // member variable. When all the service implemenations go away, the app will be | |
| 42 // quit with a call to mojo::ApplicationImpl::Terminate(). | |
| 43 class AppLifetimeHelper { | |
| 44 public: | |
| 45 AppLifetimeHelper(); | |
| 46 ~AppLifetimeHelper(); | |
| 47 | |
| 48 scoped_ptr<AppRefCount> CreateAppRefCount(); | |
| 49 | |
| 50 private: | |
| 51 friend AppRefCount; | |
| 52 void Release(); | |
| 53 | |
| 54 int ref_count_; | |
| 55 | |
| 56 base::WeakPtrFactory<AppLifetimeHelper> weak_factory_; | |
|
yzshen1
2015/05/14 16:16:38
According to our offline discussion:
A service may
jam
2015/05/15 02:15:31
Thanks, nice catch. I've switched to use thread ho
| |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(AppLifetimeHelper); | |
| 59 }; | |
| 60 | |
| 61 } // namespace mojo | |
| 62 | |
| 63 #endif // MOJO_APPLICATION_APP_LIFETIME_HELPER_H_ | |
| OLD | NEW |