Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / mojo / application / public / cpp / app_lifetime_helper.h
blob93005ef45b14437ada131bfb54a3e339a7adb81b
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"
12 namespace mojo {
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).
23 class AppRefCount {
24 public:
25 ~AppRefCount();
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
30 // still alive.
31 scoped_ptr<AppRefCount> Clone();
33 private:
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
40 // app is.
41 AppLifetimeHelper* app_lifetime_helper_;
42 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner_;
44 #ifndef NDEBUG
45 scoped_refptr<base::SingleThreadTaskRunner> clone_task_runner_;
46 #endif
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 {
59 public:
60 explicit AppLifetimeHelper(ApplicationImpl* app);
61 ~AppLifetimeHelper();
63 scoped_ptr<AppRefCount> CreateAppRefCount();
65 private:
66 friend AppRefCount;
67 void AddRef();
68 void Release();
70 friend ApplicationImpl;
71 void ApplicationTerminated();
73 ApplicationImpl* app_;
74 int ref_count_;
76 DISALLOW_COPY_AND_ASSIGN(AppLifetimeHelper);
79 } // namespace mojo
81 #endif // MOJO_APPLICATION_PUBLIC_CPP_APP_LIFETIME_HELPER_H_