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"
14 struct LeakyLazyObserverListTraits
:
15 base::internal::LeakyLazyInstanceTraits
<
16 ObserverListThreadSafe
<base::android::ApplicationStatusListener
> > {
17 static ObserverListThreadSafe
<base::android::ApplicationStatusListener
>*
19 ObserverListThreadSafe
<base::android::ApplicationStatusListener
>* ret
=
20 base::internal::LeakyLazyInstanceTraits
<ObserverListThreadSafe
<
21 base::android::ApplicationStatusListener
> >::New(instance
);
28 base::LazyInstance
<ObserverListThreadSafe
<
29 base::android::ApplicationStatusListener
>,
30 LeakyLazyObserverListTraits
> g_observers
= LAZY_INSTANCE_INITIALIZER
;
37 ApplicationStatusListener::ApplicationStatusListener(
38 const ApplicationStatusListener::ApplicationStateChangeCallback
& callback
)
39 : callback_(callback
) {
40 DCHECK(!callback_
.is_null());
41 g_observers
.Get().AddObserver(this);
43 Java_ApplicationStatus_registerThreadSafeNativeApplicationStateListener(
44 base::android::AttachCurrentThread());
47 ApplicationStatusListener::~ApplicationStatusListener() {
48 g_observers
.Get().RemoveObserver(this);
51 void ApplicationStatusListener::Notify(ApplicationState state
) {
56 bool ApplicationStatusListener::RegisterBindings(JNIEnv
* env
) {
57 return RegisterNativesImpl(env
);
61 void ApplicationStatusListener::NotifyApplicationStateChange(
62 ApplicationState state
) {
63 g_observers
.Get().Notify(&ApplicationStatusListener::Notify
, state
);
66 static void OnApplicationStateChange(JNIEnv
* env
,
69 ApplicationState application_state
= static_cast<ApplicationState
>(new_state
);
70 ApplicationStatusListener::NotifyApplicationStateChange(application_state
);
73 } // namespace android