Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / base / android / application_status_listener.h
blob30048b2b5158ac81542621b1ad370d8e1b5b17f1
1 // Copyright 2014 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 BASE_ANDROID_APPLICATION_STATUS_LISTENER_H_
6 #define BASE_ANDROID_APPLICATION_STATUS_LISTENER_H_
8 #include <jni.h>
10 #include "base/android/jni_android.h"
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/singleton.h"
15 #include "base/observer_list_threadsafe.h"
17 namespace base {
18 namespace android {
20 // Define application state values like APPLICATION_STATE_VISIBLE in a
21 // way that ensures they're always the same than their Java counterpart.
23 // Note that these states represent the most visible Activity state.
24 // If there are activities with states paused and stopped, only
25 // HAS_PAUSED_ACTIVITIES should be returned.
27 // A Java counterpart will be generated for this enum.
28 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base
29 enum ApplicationState {
30 APPLICATION_STATE_HAS_RUNNING_ACTIVITIES = 1,
31 APPLICATION_STATE_HAS_PAUSED_ACTIVITIES = 2,
32 APPLICATION_STATE_HAS_STOPPED_ACTIVITIES = 3,
33 APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES = 4
36 // A native helper class to listen to state changes of the Android
37 // Application. This mirrors org.chromium.base.ApplicationStatus.
38 // any thread.
40 // To start listening, create a new instance, passing a callback to a
41 // function that takes an ApplicationState parameter. To stop listening,
42 // simply delete the listener object. The implementation guarantees
43 // that the callback will always be called on the thread that created
44 // the listener.
46 // Example:
48 // void OnApplicationStateChange(ApplicationState state) {
49 // ...
50 // }
52 // // Start listening.
53 // ApplicationStatusListener* my_listener =
54 // new ApplicationStatusListener(
55 // base::Bind(&OnApplicationStateChange));
57 // ...
59 // // Stop listening.
60 // delete my_listener
62 class BASE_EXPORT ApplicationStatusListener {
63 public:
64 typedef base::Callback<void(ApplicationState)> ApplicationStateChangeCallback;
66 explicit ApplicationStatusListener(
67 const ApplicationStateChangeCallback& callback);
68 ~ApplicationStatusListener();
70 // Internal use: must be public to be called from base_jni_registrar.cc
71 static bool RegisterBindings(JNIEnv* env);
73 // Internal use only: must be public to be called from JNI and unit tests.
74 static void NotifyApplicationStateChange(ApplicationState state);
76 private:
77 void Notify(ApplicationState state);
79 ApplicationStateChangeCallback callback_;
81 DISALLOW_COPY_AND_ASSIGN(ApplicationStatusListener);
84 } // namespace android
85 } // namespace base
87 #endif // BASE_ANDROID_APPLICATION_STATUS_LISTENER_H_