OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.crash; | 5 package org.chromium.android_webview.crash; |
6 | 6 |
7 import org.chromium.android_webview.command_line.CommandLineUtil; | |
7 import org.chromium.base.CommandLine; | 8 import org.chromium.base.CommandLine; |
8 import org.chromium.base.annotations.SuppressFBWarnings; | 9 import org.chromium.base.annotations.SuppressFBWarnings; |
9 | 10 |
10 /** | 11 /** |
11 * Class for fetching command line switches for WebView in a thread-safe way. | 12 * Class for fetching command line switches for WebView in a thread-safe way. |
12 */ | 13 */ |
13 class SynchronizedWebViewCommandLine { | 14 class SynchronizedWebViewCommandLine { |
14 private static final Object sLock = new Object(); | 15 private static final Object sLock = new Object(); |
15 private static InitState sInitialized = InitState.NOT_STARTED; | 16 private static InitState sInitialized = InitState.NOT_STARTED; |
16 // TODO(gsennton): this value is used in WebViewChromiumFactoryProvider as w ell - set it | |
17 // somewhere where it can be read from both classes. | |
18 private static final String WEBVIEW_COMMAND_LINE_FILE = "/data/local/tmp/web view-command-line"; | |
19 | 17 |
20 private enum InitState { NOT_STARTED, STARTED, DONE } | 18 private enum InitState { NOT_STARTED, STARTED, DONE } |
21 | 19 |
20 private SynchronizedWebViewCommandLine() {} | |
21 | |
22 public static SynchronizedWebViewCommandLine getInstance() { | |
23 return new SynchronizedWebViewCommandLine(); | |
sgurun-gerrit only
2017/01/24 08:58:49
why is it necessary to be able to create different
gsennton
2017/01/24 10:41:48
To be able to mock the behaviour of SynchronizedWe
| |
24 } | |
25 | |
22 /** | 26 /** |
23 * Initialize the global CommandLine using the WebView command line file. | 27 * Initialize the global CommandLine using the WebView command line file. |
24 * This method includes IO operations - it shouldn't be performed on the mai n thread. | 28 * This method includes IO operations - it shouldn't be performed on the mai n thread. |
25 */ | 29 */ |
26 public static void initOnSeparateThread() { | 30 public void initOnSeparateThread() { |
27 synchronized (sLock) { | 31 synchronized (sLock) { |
28 if (sInitialized != InitState.NOT_STARTED) return; | 32 if (sInitialized != InitState.NOT_STARTED) return; |
29 sInitialized = InitState.STARTED; | 33 sInitialized = InitState.STARTED; |
30 } | 34 } |
31 new Thread(new Runnable() { | 35 new Thread(new Runnable() { |
32 @SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME") | 36 @SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME") |
33 @Override | 37 @Override |
34 public void run() { | 38 public void run() { |
35 CommandLine.initFromFile(WEBVIEW_COMMAND_LINE_FILE); | 39 // Only read command line flags if the device is debuggable. |
40 if (CommandLineUtil.isBuildDebuggable()) { | |
41 CommandLine.initFromFile(CommandLineUtil.WEBVIEW_COMMAND_LIN E_FILE); | |
42 } else { | |
43 CommandLine.init(null); | |
44 } | |
36 synchronized (sLock) { | 45 synchronized (sLock) { |
37 sInitialized = InitState.DONE; | 46 sInitialized = InitState.DONE; |
38 sLock.notifyAll(); | 47 sLock.notifyAll(); |
39 } | 48 } |
40 } | 49 } |
41 }, "WebView-command-line-init-thread").start(); | 50 }, "WebView-command-line-init-thread").start(); |
42 } | 51 } |
43 | 52 |
44 /** | 53 /** |
45 * Returns true if this command line contains the given switch. | 54 * Returns true if this command line contains the given switch. |
46 */ | 55 */ |
47 public static boolean hasSwitch(String switchString) { | 56 public boolean hasSwitch(String switchString) { |
48 synchronized (sLock) { | 57 synchronized (sLock) { |
49 while (sInitialized != InitState.DONE) { | 58 while (sInitialized != InitState.DONE) { |
50 try { | 59 try { |
51 sLock.wait(); | 60 sLock.wait(); |
52 } catch (InterruptedException e) { | 61 } catch (InterruptedException e) { |
53 throw new RuntimeException(e); | 62 throw new RuntimeException(e); |
54 } | 63 } |
55 } | 64 } |
56 return CommandLine.getInstance().hasSwitch(switchString); | 65 return CommandLine.getInstance().hasSwitch(switchString); |
57 } | 66 } |
58 } | 67 } |
59 } | 68 } |
OLD | NEW |