| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/android/application_status_listener.h" | 5 #include "base/android/application_status_listener.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/observer_list_threadsafe.h" | 10 #include "base/observer_list_threadsafe.h" |
| 11 #include "jni/ApplicationStatus_jni.h" | 11 #include "jni/ApplicationStatus_jni.h" |
| 12 | 12 |
| 13 namespace base { |
| 14 namespace android { |
| 15 |
| 13 namespace { | 16 namespace { |
| 17 |
| 14 struct LeakyLazyObserverListTraits : | 18 struct LeakyLazyObserverListTraits : |
| 15 base::internal::LeakyLazyInstanceTraits< | 19 base::internal::LeakyLazyInstanceTraits< |
| 16 ObserverListThreadSafe<base::android::ApplicationStatusListener> > { | 20 ObserverListThreadSafe<ApplicationStatusListener> > { |
| 17 static ObserverListThreadSafe<base::android::ApplicationStatusListener>* | 21 static ObserverListThreadSafe<ApplicationStatusListener>* |
| 18 New(void* instance) { | 22 New(void* instance) { |
| 19 ObserverListThreadSafe<base::android::ApplicationStatusListener>* ret = | 23 ObserverListThreadSafe<ApplicationStatusListener>* ret = |
| 20 base::internal::LeakyLazyInstanceTraits<ObserverListThreadSafe< | 24 base::internal::LeakyLazyInstanceTraits<ObserverListThreadSafe< |
| 21 base::android::ApplicationStatusListener> >::New(instance); | 25 ApplicationStatusListener>>::New(instance); |
| 22 // Leaky. | 26 // Leaky. |
| 23 ret->AddRef(); | 27 ret->AddRef(); |
| 24 return ret; | 28 return ret; |
| 25 } | 29 } |
| 26 }; | 30 }; |
| 27 | 31 |
| 28 base::LazyInstance<ObserverListThreadSafe< | 32 LazyInstance<ObserverListThreadSafe<ApplicationStatusListener>, |
| 29 base::android::ApplicationStatusListener>, | 33 LeakyLazyObserverListTraits> g_observers = |
| 30 LeakyLazyObserverListTraits> g_observers = LAZY_INSTANCE_INITIALIZER; | 34 LAZY_INSTANCE_INITIALIZER; |
| 31 | 35 |
| 32 } // namespace | 36 } // namespace |
| 33 | |
| 34 namespace base { | |
| 35 namespace android { | |
| 36 | 37 |
| 37 ApplicationStatusListener::ApplicationStatusListener( | 38 ApplicationStatusListener::ApplicationStatusListener( |
| 38 const ApplicationStatusListener::ApplicationStateChangeCallback& callback) | 39 const ApplicationStatusListener::ApplicationStateChangeCallback& callback) |
| 39 : callback_(callback) { | 40 : callback_(callback) { |
| 40 DCHECK(!callback_.is_null()); | 41 DCHECK(!callback_.is_null()); |
| 41 g_observers.Get().AddObserver(this); | 42 g_observers.Get().AddObserver(this); |
| 42 | 43 |
| 43 Java_ApplicationStatus_registerThreadSafeNativeApplicationStateListener( | 44 Java_ApplicationStatus_registerThreadSafeNativeApplicationStateListener( |
| 44 base::android::AttachCurrentThread()); | 45 AttachCurrentThread()); |
| 45 } | 46 } |
| 46 | 47 |
| 47 ApplicationStatusListener::~ApplicationStatusListener() { | 48 ApplicationStatusListener::~ApplicationStatusListener() { |
| 48 g_observers.Get().RemoveObserver(this); | 49 g_observers.Get().RemoveObserver(this); |
| 49 } | 50 } |
| 50 | 51 |
| 51 void ApplicationStatusListener::Notify(ApplicationState state) { | 52 void ApplicationStatusListener::Notify(ApplicationState state) { |
| 52 callback_.Run(state); | 53 callback_.Run(state); |
| 53 } | 54 } |
| 54 | 55 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 66 | 67 |
| 67 static void OnApplicationStateChange(JNIEnv* env, | 68 static void OnApplicationStateChange(JNIEnv* env, |
| 68 jclass clazz, | 69 jclass clazz, |
| 69 jint new_state) { | 70 jint new_state) { |
| 70 ApplicationState application_state = static_cast<ApplicationState>(new_state); | 71 ApplicationState application_state = static_cast<ApplicationState>(new_state); |
| 71 ApplicationStatusListener::NotifyApplicationStateChange(application_state); | 72 ApplicationStatusListener::NotifyApplicationStateChange(application_state); |
| 72 } | 73 } |
| 73 | 74 |
| 74 } // namespace android | 75 } // namespace android |
| 75 } // namespace base | 76 } // namespace base |
| OLD | NEW |