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 #include "base/android/application_status_listener.h"
9 #include "base/lazy_instance.h"
10 #include "base/observer_list_threadsafe.h"
11 #include "jni/ApplicationStatus_jni.h"
18 struct LeakyLazyObserverListTraits
:
19 base::internal::LeakyLazyInstanceTraits
<
20 ObserverListThreadSafe
<ApplicationStatusListener
> > {
21 static ObserverListThreadSafe
<ApplicationStatusListener
>*
23 ObserverListThreadSafe
<ApplicationStatusListener
>* ret
=
24 base::internal::LeakyLazyInstanceTraits
<ObserverListThreadSafe
<
25 ApplicationStatusListener
>>::New(instance
);
32 LazyInstance
<ObserverListThreadSafe
<ApplicationStatusListener
>,
33 LeakyLazyObserverListTraits
> g_observers
=
34 LAZY_INSTANCE_INITIALIZER
;
38 ApplicationStatusListener::ApplicationStatusListener(
39 const ApplicationStatusListener::ApplicationStateChangeCallback
& callback
)
40 : callback_(callback
) {
41 DCHECK(!callback_
.is_null());
42 g_observers
.Get().AddObserver(this);
44 Java_ApplicationStatus_registerThreadSafeNativeApplicationStateListener(
45 AttachCurrentThread());
48 ApplicationStatusListener::~ApplicationStatusListener() {
49 g_observers
.Get().RemoveObserver(this);
52 void ApplicationStatusListener::Notify(ApplicationState state
) {
57 bool ApplicationStatusListener::RegisterBindings(JNIEnv
* env
) {
58 return RegisterNativesImpl(env
);
62 void ApplicationStatusListener::NotifyApplicationStateChange(
63 ApplicationState state
) {
64 g_observers
.Get().Notify(FROM_HERE
, &ApplicationStatusListener::Notify
,
68 static void OnApplicationStateChange(JNIEnv
* env
,
71 ApplicationState application_state
= static_cast<ApplicationState
>(new_state
);
72 ApplicationStatusListener::NotifyApplicationStateChange(application_state
);
75 } // namespace android