Roll src/third_party/WebKit eb1578b:b559582 (svn 193425:193437)
[chromium-blink-merge.git] / chrome / browser / metrics / thread_watcher_android.cc
blob74aab6a4f99a1e8f285e7c24bd805c154232ecb6
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 "chrome/browser/metrics/thread_watcher_android.h"
7 #include "base/android/application_status_listener.h"
8 #include "base/command_line.h"
9 #include "base/lazy_instance.h"
10 #include "chrome/browser/metrics/thread_watcher.h"
12 namespace {
14 // For most of the activities, the C++ side is initialized asynchronously
15 // and the very first APPLICATION_STATE_HAS_RUNNING_ACTIVITIES is never received
16 // whilst the ThreadWatcherList is initiated higher up in the stack.
17 // However, some activities are initialized synchronously, and it'll receive
18 // an APPLICATION_STATE_HAS_RUNNING_ACTIVITIES here as well.
19 // Protect against this case, and only let
20 // APPLICATION_STATE_HAS_RUNNING_ACTIVITIES turn on the
21 // watchdog if it was previously handled by an
22 // APPLICATION_STATE_HAS_STOPPED_ACTIVITIES (which is always handled here).
23 bool g_application_has_stopped = false;
25 void OnApplicationStateChange(
26 base::android::ApplicationState application_state) {
27 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
28 if (application_state ==
29 base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES) {
30 g_application_has_stopped = true;
31 ThreadWatcherList::StopWatchingAll();
32 } else if (application_state ==
33 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES &&
34 g_application_has_stopped) {
35 g_application_has_stopped = false;
36 ThreadWatcherList::StartWatchingAll(
37 *base::CommandLine::ForCurrentProcess());
41 struct LeakyApplicationStatusListenerTraits {
42 static const bool kRegisterOnExit = false;
43 #ifndef NDEBUG
44 static const bool kAllowedToAccessOnNonjoinableThread = true;
45 #endif
47 static base::android::ApplicationStatusListener* New(void* instance) {
48 ANNOTATE_SCOPED_MEMORY_LEAK;
49 return new (instance) base::android::ApplicationStatusListener(
50 base::Bind(&OnApplicationStateChange));
53 static void Delete(base::android::ApplicationStatusListener* instance) {
57 base::LazyInstance<base::android::ApplicationStatusListener,
58 LeakyApplicationStatusListenerTraits>
59 g_application_status_listener = LAZY_INSTANCE_INITIALIZER;
61 } // namespace
63 void ThreadWatcherAndroid::RegisterApplicationStatusListener() {
64 // Leaky.
65 g_application_status_listener.Get();