| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 package org.chromium.base; | 5 package org.chromium.base; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.SharedPreferences; | 8 import android.content.SharedPreferences; |
| 9 import android.preference.PreferenceManager; | 9 import android.preference.PreferenceManager; |
| 10 | 10 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 public static void initApplicationContext(Context appContext) { | 55 public static void initApplicationContext(Context appContext) { |
| 56 // Conceding that occasionally in tests, native is loaded before the bro
wser process is | 56 // Conceding that occasionally in tests, native is loaded before the bro
wser process is |
| 57 // started, in which case the browser process re-sets the application co
ntext. | 57 // started, in which case the browser process re-sets the application co
ntext. |
| 58 if (sApplicationContext != null && sApplicationContext != appContext) { | 58 if (sApplicationContext != null && sApplicationContext != appContext) { |
| 59 throw new RuntimeException("Attempting to set multiple global applic
ation contexts."); | 59 throw new RuntimeException("Attempting to set multiple global applic
ation contexts."); |
| 60 } | 60 } |
| 61 initJavaSideApplicationContext(appContext); | 61 initJavaSideApplicationContext(appContext); |
| 62 } | 62 } |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * Initialize the native Android application context to be the same as the j
ava counter-part. | |
| 66 */ | |
| 67 public static void initApplicationContextForNative() { | |
| 68 if (sApplicationContext == null) { | |
| 69 throw new RuntimeException("Cannot have native global application co
ntext be null."); | |
| 70 } | |
| 71 nativeInitNativeSideApplicationContext(sApplicationContext); | |
| 72 } | |
| 73 | |
| 74 /** | |
| 75 * Only called by the static holder class and tests. | 65 * Only called by the static holder class and tests. |
| 76 * | 66 * |
| 77 * @return The application-wide shared preferences. | 67 * @return The application-wide shared preferences. |
| 78 */ | 68 */ |
| 79 private static SharedPreferences fetchAppSharedPreferences() { | 69 private static SharedPreferences fetchAppSharedPreferences() { |
| 80 return PreferenceManager.getDefaultSharedPreferences(sApplicationContext
); | 70 return PreferenceManager.getDefaultSharedPreferences(sApplicationContext
); |
| 81 } | 71 } |
| 82 | 72 |
| 83 /** | 73 /** |
| 84 * This is used to ensure that we always use the application context to fetc
h the default shared | 74 * This is used to ensure that we always use the application context to fetc
h the default shared |
| (...skipping 18 matching lines...) Expand all Loading... |
| 103 initJavaSideApplicationContext(appContext); | 93 initJavaSideApplicationContext(appContext); |
| 104 Holder.sSharedPreferences = fetchAppSharedPreferences(); | 94 Holder.sSharedPreferences = fetchAppSharedPreferences(); |
| 105 } | 95 } |
| 106 | 96 |
| 107 private static void initJavaSideApplicationContext(Context appContext) { | 97 private static void initJavaSideApplicationContext(Context appContext) { |
| 108 if (appContext == null) { | 98 if (appContext == null) { |
| 109 throw new RuntimeException("Global application context cannot be set
to null."); | 99 throw new RuntimeException("Global application context cannot be set
to null."); |
| 110 } | 100 } |
| 111 sApplicationContext = appContext; | 101 sApplicationContext = appContext; |
| 112 } | 102 } |
| 113 | |
| 114 private static native void nativeInitNativeSideApplicationContext(Context ap
pContext); | |
| 115 } | 103 } |
| OLD | NEW |