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.
5 #ifndef MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_
6 #define MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/single_thread_task_runner.h"
14 class ApplicationImpl
;
15 class AppLifetimeHelper
;
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).
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
31 scoped_ptr
<AppRefCount
> Clone();
34 friend AppLifetimeHelper
;
36 AppRefCount(AppLifetimeHelper
* app_lifetime_helper
,
37 scoped_refptr
<base::SingleThreadTaskRunner
> app_task_runner
);
39 // Don't need to use weak ptr because if the app thread is alive that means
41 AppLifetimeHelper
* app_lifetime_helper_
;
42 scoped_refptr
<base::SingleThreadTaskRunner
> app_task_runner_
;
45 scoped_refptr
<base::SingleThreadTaskRunner
> clone_task_runner_
;
48 DISALLOW_COPY_AND_ASSIGN(AppRefCount
);
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
{
60 explicit AppLifetimeHelper(ApplicationImpl
* app
);
63 scoped_ptr
<AppRefCount
> CreateAppRefCount();
70 friend ApplicationImpl
;
71 void ApplicationTerminated();
73 ApplicationImpl
* app_
;
76 DISALLOW_COPY_AND_ASSIGN(AppLifetimeHelper
);
81 #endif // MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_