Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: android_webview/java/src/org/chromium/android_webview/AwBrowserProcess.java

Issue 2076623002: 🍳 Fix and clean up application context in webview. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix tests. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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.android_webview; 5 package org.chromium.android_webview;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.os.StrictMode; 8 import android.os.StrictMode;
9 9
10 import org.chromium.android_webview.policy.AwPolicyProvider; 10 import org.chromium.android_webview.policy.AwPolicyProvider;
(...skipping 22 matching lines...) Expand all
33 33
34 private static final String TAG = "AwBrowserProcess"; 34 private static final String TAG = "AwBrowserProcess";
35 private static final String EXCLUSIVE_LOCK_FILE = "webview_data.lock"; 35 private static final String EXCLUSIVE_LOCK_FILE = "webview_data.lock";
36 private static FileLock sExclusiveFileLock; 36 private static FileLock sExclusiveFileLock;
37 37
38 /** 38 /**
39 * Loads the native library, and performs basic static construction of objec ts needed 39 * Loads the native library, and performs basic static construction of objec ts needed
40 * to run webview in this process. Does not create threads; safe to call fro m zygote. 40 * to run webview in this process. Does not create threads; safe to call fro m zygote.
41 * Note: it is up to the caller to ensure this is only called once. 41 * Note: it is up to the caller to ensure this is only called once.
42 */ 42 */
43 public static void loadLibrary(Context context) { 43 public static void loadLibrary() {
44 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX, c ontext); 44 Context appContext = ContextUtils.getApplicationContext();
45 ContextUtils.initApplicationContext(context.getApplicationContext()); 45 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX, a ppContext);
46 try { 46 try {
47 LibraryLoader libraryLoader = LibraryLoader.get(LibraryProcessType.P ROCESS_WEBVIEW); 47 LibraryLoader libraryLoader = LibraryLoader.get(LibraryProcessType.P ROCESS_WEBVIEW);
48 libraryLoader.loadNow(context); 48 libraryLoader.loadNow(appContext);
49 // Switch the command line implementation from Java to native. 49 // Switch the command line implementation from Java to native.
50 // It's okay for the WebView to do this before initialization becaus e we have 50 // It's okay for the WebView to do this before initialization becaus e we have
51 // setup the JNI bindings by this point. 51 // setup the JNI bindings by this point.
52 libraryLoader.switchCommandLineForWebView(); 52 libraryLoader.switchCommandLineForWebView();
53 } catch (ProcessInitException e) { 53 } catch (ProcessInitException e) {
54 throw new RuntimeException("Cannot load WebView", e); 54 throw new RuntimeException("Cannot load WebView", e);
55 } 55 }
56 } 56 }
57 57
58 /** 58 /**
59 * Configures child process launcher. This is required only if child service s are used in 59 * Configures child process launcher. This is required only if child service s are used in
60 * WebView. 60 * WebView.
61 */ 61 */
62 public static void configureChildProcessLauncher(String packageName, int ext raBindFlags) { 62 public static void configureChildProcessLauncher(String packageName, int ext raBindFlags) {
63 ChildProcessCreationParams.set( 63 ChildProcessCreationParams.set(
64 new ChildProcessCreationParams(packageName, extraBindFlags, 64 new ChildProcessCreationParams(packageName, extraBindFlags,
65 LibraryProcessType.PROCESS_WEBVIEW_CHILD)); 65 LibraryProcessType.PROCESS_WEBVIEW_CHILD));
66 } 66 }
67 67
68 /** 68 /**
69 * Starts the chromium browser process running within this process. Creates threads 69 * Starts the chromium browser process running within this process. Creates threads
70 * and performs other per-app resource allocations; must not be called from zygote. 70 * and performs other per-app resource allocations; must not be called from zygote.
71 * Note: it is up to the caller to ensure this is only called once. 71 * Note: it is up to the caller to ensure this is only called once.
72 * @param context The Android application context
73 */ 72 */
74 public static void start(final Context context) { 73 public static void start() {
75 tryObtainingDataDirLockOrDie(context); 74 tryObtainingDataDirLockOrDie();
76 // We must post to the UI thread to cover the case that the user 75 // We must post to the UI thread to cover the case that the user
77 // has invoked Chromium startup by using the (thread-safe) 76 // has invoked Chromium startup by using the (thread-safe)
78 // CookieManager rather than creating a WebView. 77 // CookieManager rather than creating a WebView.
79 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 78 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
80 @Override 79 @Override
81 public void run() { 80 public void run() {
81 Context appContext = ContextUtils.getApplicationContext();
82 // The policies are used by browser startup, so we need to regis ter the policy 82 // The policies are used by browser startup, so we need to regis ter the policy
83 // providers before starting the browser process. This only regi sters java objects 83 // providers before starting the browser process. This only regi sters java objects
84 // and doesn't need the native library. 84 // and doesn't need the native library.
85 CombinedPolicyProvider.get().registerProvider(new AwPolicyProvid er(context)); 85 CombinedPolicyProvider.get().registerProvider(new AwPolicyProvid er(appContext));
86 86
87 try { 87 try {
88 BrowserStartupController.get(context, LibraryProcessType.PRO CESS_WEBVIEW) 88 BrowserStartupController.get(appContext, LibraryProcessType. PROCESS_WEBVIEW)
89 .startBrowserProcessesSync(!CommandLine.getInstance( ).hasSwitch( 89 .startBrowserProcessesSync(!CommandLine.getInstance( ).hasSwitch(
90 AwSwitches.WEBVIEW_SANDBOXED_RENDERE R)); 90 AwSwitches.WEBVIEW_SANDBOXED_RENDERE R));
91 } catch (ProcessInitException e) { 91 } catch (ProcessInitException e) {
92 throw new RuntimeException("Cannot initialize WebView", e); 92 throw new RuntimeException("Cannot initialize WebView", e);
93 } 93 }
94 } 94 }
95 }); 95 });
96 } 96 }
97 97
98 private static void tryObtainingDataDirLockOrDie(Context context) { 98 private static void tryObtainingDataDirLockOrDie() {
99 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); 99 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
100 StrictMode.allowThreadDiskWrites(); 100 StrictMode.allowThreadDiskWrites();
101 try { 101 try {
102 String dataPath = PathUtils.getDataDirectory(context); 102 String dataPath = PathUtils.getDataDirectory(ContextUtils.getApplica tionContext());
103 File lockFile = new File(dataPath, EXCLUSIVE_LOCK_FILE); 103 File lockFile = new File(dataPath, EXCLUSIVE_LOCK_FILE);
104 boolean success = false; 104 boolean success = false;
105 try { 105 try {
106 // Note that the file is not closed intentionally. 106 // Note that the file is not closed intentionally.
107 RandomAccessFile file = new RandomAccessFile(lockFile, "rw"); 107 RandomAccessFile file = new RandomAccessFile(lockFile, "rw");
108 sExclusiveFileLock = file.getChannel().tryLock(); 108 sExclusiveFileLock = file.getChannel().tryLock();
109 success = sExclusiveFileLock != null; 109 success = sExclusiveFileLock != null;
110 } catch (IOException e) { 110 } catch (IOException e) {
111 Log.w(TAG, "Failed to create lock file " + lockFile, e); 111 Log.w(TAG, "Failed to create lock file " + lockFile, e);
112 } 112 }
113 if (!success) { 113 if (!success) {
114 Log.w(TAG, "The app may have another WebView opened in a separat e process. " 114 Log.w(TAG, "The app may have another WebView opened in a separat e process. "
115 + "This is not recommended and may stop working in futur e versions."); 115 + "This is not recommended and may stop working in futur e versions.");
116 } 116 }
117 } finally { 117 } finally {
118 StrictMode.setThreadPolicy(oldPolicy); 118 StrictMode.setThreadPolicy(oldPolicy);
119 } 119 }
120 } 120 }
121 } 121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698