Chromium Code Reviews| Index: mojo/application/app_lifetime_helper.h |
| diff --git a/mojo/application/app_lifetime_helper.h b/mojo/application/app_lifetime_helper.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8ba12d945e24a2fd7436fab172fd7dcecdf563b7 |
| --- /dev/null |
| +++ b/mojo/application/app_lifetime_helper.h |
| @@ -0,0 +1,63 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef MOJO_APPLICATION_APP_LIFETIME_HELPER_H_ |
| +#define MOJO_APPLICATION_APP_LIFETIME_HELPER_H_ |
| + |
| +#include "base/basictypes.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| + |
| +namespace mojo { |
| + |
| +class AppLifetimeHelper; |
| + |
| +// A service implementation should keep this object as a member variable to hold |
| +// a reference on the application. |
| +class AppRefCount { |
| + public: |
| + explicit AppRefCount( |
| + const base::WeakPtr<AppLifetimeHelper>& app_lifetime_helper); |
| + ~AppRefCount(); |
| + |
| + // When a service creates another object that is held by the client, it should |
| + // also vend to it a refcount using this method. That way if the caller stops |
| + // holding on to the service but keeps the reference to the object, the app is |
| + // still alive. |
| + scoped_ptr<AppRefCount> Clone(); |
| + |
| + private: |
| + base::WeakPtr<AppLifetimeHelper> app_lifetime_helper_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AppRefCount); |
| +}; |
| + |
| +// This is a helper class for apps to manage their lifetime, specifically so |
| +// apps can quit when they're not used anymore. |
| +// An app can contain an object of this class as a member variable. Each time it |
| +// creates an instance of a service, it gives it a refcount using |
| +// CreateAppRefCount. The service implementation then keeps that object as a |
| +// member variable. When all the service implemenations go away, the app will be |
| +// quit with a call to mojo::ApplicationImpl::Terminate(). |
| +class AppLifetimeHelper { |
| + public: |
| + AppLifetimeHelper(); |
| + ~AppLifetimeHelper(); |
| + |
| + scoped_ptr<AppRefCount> CreateAppRefCount(); |
| + |
| + private: |
| + friend AppRefCount; |
| + void Release(); |
| + |
| + int ref_count_; |
| + |
| + 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
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(AppLifetimeHelper); |
| +}; |
| + |
| +} // namespace mojo |
| + |
| +#endif // MOJO_APPLICATION_APP_LIFETIME_HELPER_H_ |