Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / content / public / android / java / src / org / chromium / content / app / ContentApplication.java
1 // Copyright 2013 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.
4
5 package org.chromium.content.app;
6
7 import android.os.Looper;
8 import android.os.MessageQueue;
9
10 import org.chromium.base.BaseChromiumApplication;
11 import org.chromium.content.browser.TracingControllerAndroid;
12
13 /**
14  * Basic application functionality that should be shared among all browser applications
15  * based on the content layer.
16  */
17 public class ContentApplication extends BaseChromiumApplication {
18     private TracingControllerAndroid mTracingController;
19
20     TracingControllerAndroid getTracingController() {
21         if (mTracingController == null) {
22             mTracingController = new TracingControllerAndroid(this);
23         }
24         return mTracingController;
25     }
26
27     @Override
28     public void onCreate() {
29         super.onCreate();
30
31         // Delay TracingControllerAndroid.registerReceiver() until the main loop is idle.
32         Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
33             @Override
34             public boolean queueIdle() {
35                 // Will retry if the native library has not been initialized.
36                 if (!LibraryLoader.isInitialized()) return true;
37
38                 try {
39                     getTracingController().registerReceiver(ContentApplication.this);
40                 } catch (SecurityException e) {
41                     // Happens if the process is isolated. Ignore.
42                 }
43                 // Remove the idle handler.
44                 return false;
45             }
46         });
47     }
48
49     /**
50      * For emulated process environment only. On a production device, the application process is
51      * simply killed without calling this method. We don't need to unregister the broadcast
52      * receiver in the latter case.
53      */
54     @Override
55     public void onTerminate() {
56         try {
57             getTracingController().unregisterReceiver(this);
58         } catch (SecurityException e) {
59             // Happens if the process is isolated. Ignore.
60         }
61
62         super.onTerminate();
63     }
64
65 }