1 | // Copyright (c) 2012 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_shell_apk; |
6 | |
7 | import android.content.BroadcastReceiver; |
8 | import android.content.Context; |
9 | import android.content.Intent; |
10 | import android.content.IntentFilter; |
11 | import android.os.Bundle; |
12 | import android.text.TextUtils; |
13 | import android.util.Log; |
14 | import android.view.KeyEvent; |
15 | |
16 | import org.chromium.base.ChromiumActivity; |
17 | import org.chromium.base.MemoryPressureListener; |
18 | import org.chromium.content.app.LibraryLoader; |
19 | import org.chromium.content.browser.ActivityContentVideoViewClient; |
20 | import org.chromium.content.browser.AndroidBrowserProcess; |
21 | import org.chromium.content.browser.BrowserStartupConfig; |
22 | import org.chromium.content.browser.ContentVideoViewClient; |
23 | import org.chromium.content.browser.ContentView; |
24 | import org.chromium.content.browser.ContentViewClient; |
25 | import org.chromium.content.browser.DeviceUtils; |
26 | import org.chromium.content.browser.TracingIntentHandler; |
27 | import org.chromium.content.common.CommandLine; |
28 | import org.chromium.content.common.ProcessInitException; |
29 | import org.chromium.content_shell.Shell; |
30 | import org.chromium.content_shell.ShellManager; |
31 | import org.chromium.ui.WindowAndroid; |
32 | |
33 | /** |
34 | * Activity for managing the Content Shell. |
35 | */ |
36 | public class ContentShellActivity extends ChromiumActivity { |
37 | |
38 | public static final String COMMAND_LINE_FILE = "/data/local/tmp/content-shell-command-line"; |
39 | private static final String TAG = "ContentShellActivity"; |
40 | |
41 | private static final String ACTIVE_SHELL_URL_KEY = "activeUrl"; |
42 | private static final String ACTION_START_TRACE = |
43 | "org.chromium.content_shell.action.PROFILE_START"; |
44 | private static final String ACTION_STOP_TRACE = |
45 | "org.chromium.content_shell.action.PROFILE_STOP"; |
46 | public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs"; |
47 | |
48 | /** |
49 | * Sending an intent with this action will simulate a memory pressure signal |
50 | * at a critical level. |
51 | */ |
52 | private static final String ACTION_LOW_MEMORY = |
53 | "org.chromium.content_shell.action.ACTION_LOW_MEMORY"; |
54 | |
55 | /** |
56 | * Sending an intent with this action will simulate a memory pressure signal |
57 | * at a moderate level. |
58 | */ |
59 | private static final String ACTION_TRIM_MEMORY_MODERATE = |
60 | "org.chromium.content_shell.action.ACTION_TRIM_MEMORY_MODERATE"; |
61 | |
62 | |
63 | private ShellManager mShellManager; |
64 | private WindowAndroid mWindowAndroid; |
65 | private BroadcastReceiver mReceiver; |
66 | |
67 | @Override |
68 | protected void onCreate(Bundle savedInstanceState) { |
69 | super.onCreate(savedInstanceState); |
70 | |
71 | // Initializing the command line must occur before loading the library. |
72 | if (!CommandLine.isInitialized()) { |
73 | CommandLine.initFromFile(COMMAND_LINE_FILE); |
74 | String[] commandLineParams = getCommandLineParamsFromIntent(getIntent()); |
75 | if (commandLineParams != null) { |
76 | CommandLine.getInstance().appendSwitchesAndArguments(commandLineParams); |
77 | } |
78 | } |
79 | waitForDebuggerIfNeeded(); |
80 | |
81 | DeviceUtils.addDeviceSpecificUserAgentSwitch(this); |
82 | try { |
83 | LibraryLoader.ensureInitialized(); |
84 | |
85 | setContentView(R.layout.content_shell_activity); |
86 | mShellManager = (ShellManager) findViewById(R.id.shell_container); |
87 | mWindowAndroid = new WindowAndroid(this); |
88 | mWindowAndroid.restoreInstanceState(savedInstanceState); |
89 | mShellManager.setWindow(mWindowAndroid); |
90 | |
91 | String startupUrl = getUrlFromIntent(getIntent()); |
92 | if (!TextUtils.isEmpty(startupUrl)) { |
93 | mShellManager.setStartupUrl(Shell.sanitizeUrl(startupUrl)); |
94 | } |
95 | |
96 | if (!CommandLine.getInstance().hasSwitch(CommandLine.DUMP_RENDER_TREE)) { |
97 | BrowserStartupConfig.setAsync(new BrowserStartupConfig.StartupCallback() { |
98 | |
99 | @Override |
100 | public void run(int startupResult) { |
101 | if (startupResult > 0) { |
102 | // TODO: Show error message. |
103 | Log.e(TAG, "ContentView initialization failed."); |
104 | finish(); |
105 | } else { |
106 | finishInitialization(); |
107 | } |
108 | } |
109 | }); |
110 | } |
111 | |
112 | if (!AndroidBrowserProcess.init(this, AndroidBrowserProcess.MAX_RENDERERS_LIMIT)) { |
113 | String shellUrl = ShellManager.DEFAULT_SHELL_URL; |
114 | if (savedInstanceState != null |
115 | && savedInstanceState.containsKey(ACTIVE_SHELL_URL_KEY)) { |
116 | shellUrl = savedInstanceState.getString(ACTIVE_SHELL_URL_KEY); |
117 | } |
118 | mShellManager.launchShell(shellUrl); |
119 | finishInitialization(); |
120 | } |
121 | } catch (ProcessInitException e) { |
122 | Log.e(TAG, "ContentView initialization failed.", e); |
123 | finish(); |
124 | } |
125 | } |
126 | |
127 | private void finishInitialization() { |
128 | getActiveContentView().setContentViewClient(new ContentViewClient() { |
129 | @Override |
130 | public ContentVideoViewClient getContentVideoViewClient() { |
131 | return new ActivityContentVideoViewClient(ContentShellActivity.this); |
132 | } |
133 | }); |
134 | } |
135 | |
136 | @Override |
137 | protected void onSaveInstanceState(Bundle outState) { |
138 | super.onSaveInstanceState(outState); |
139 | Shell activeShell = getActiveShell(); |
140 | if (activeShell != null) { |
141 | outState.putString(ACTIVE_SHELL_URL_KEY, activeShell.getContentView().getUrl()); |
142 | } |
143 | |
144 | mWindowAndroid.saveInstanceState(outState); |
145 | } |
146 | |
147 | private void waitForDebuggerIfNeeded() { |
148 | if (CommandLine.getInstance().hasSwitch(CommandLine.WAIT_FOR_JAVA_DEBUGGER)) { |
149 | Log.e(TAG, "Waiting for Java debugger to connect..."); |
150 | android.os.Debug.waitForDebugger(); |
151 | Log.e(TAG, "Java debugger connected. Resuming execution."); |
152 | } |
153 | } |
154 | |
155 | @Override |
156 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
157 | if (keyCode != KeyEvent.KEYCODE_BACK) return super.onKeyUp(keyCode, event); |
158 | |
159 | Shell activeView = getActiveShell(); |
160 | if (activeView != null && activeView.getContentView().canGoBack()) { |
161 | activeView.getContentView().goBack(); |
162 | return true; |
163 | } |
164 | |
165 | return super.onKeyUp(keyCode, event); |
166 | } |
167 | |
168 | @Override |
169 | protected void onNewIntent(Intent intent) { |
170 | if (getCommandLineParamsFromIntent(intent) != null) { |
171 | Log.i(TAG, "Ignoring command line params: can only be set when creating the activity."); |
172 | } |
173 | |
174 | if (ACTION_LOW_MEMORY.equals(intent.getAction())) { |
175 | MemoryPressureListener.simulateMemoryPressureSignal(TRIM_MEMORY_COMPLETE); |
176 | return; |
177 | } else if (ACTION_TRIM_MEMORY_MODERATE.equals(intent.getAction())) { |
178 | MemoryPressureListener.simulateMemoryPressureSignal(TRIM_MEMORY_MODERATE); |
179 | return; |
180 | } |
181 | |
182 | String url = getUrlFromIntent(intent); |
183 | if (!TextUtils.isEmpty(url)) { |
184 | Shell activeView = getActiveShell(); |
185 | if (activeView != null) { |
186 | activeView.loadUrl(url); |
187 | } |
188 | } |
189 | } |
190 | |
191 | @Override |
192 | protected void onPause() { |
193 | ContentView view = getActiveContentView(); |
194 | if (view != null) view.onActivityPause(); |
195 | |
196 | super.onPause(); |
197 | unregisterReceiver(mReceiver); |
198 | } |
199 | |
200 | @Override |
201 | protected void onResume() { |
202 | super.onResume(); |
203 | |
204 | ContentView view = getActiveContentView(); |
205 | if (view != null) view.onActivityResume(); |
206 | IntentFilter intentFilter = new IntentFilter(ACTION_START_TRACE); |
207 | intentFilter.addAction(ACTION_STOP_TRACE); |
208 | mReceiver = new BroadcastReceiver() { |
209 | @Override |
210 | public void onReceive(Context context, Intent intent) { |
211 | String action = intent.getAction(); |
212 | String extra = intent.getStringExtra("file"); |
213 | if (ACTION_START_TRACE.equals(action)) { |
214 | if (extra.isEmpty()) { |
215 | Log.e(TAG, "Can not start tracing without specifing saving location"); |
216 | } else { |
217 | TracingIntentHandler.beginTracing(extra); |
218 | Log.i(TAG, "start tracing"); |
219 | } |
220 | } else if (ACTION_STOP_TRACE.equals(action)) { |
221 | Log.i(TAG, "stop tracing"); |
222 | TracingIntentHandler.endTracing(); |
223 | } |
224 | } |
225 | }; |
226 | registerReceiver(mReceiver, intentFilter); |
227 | } |
228 | |
229 | @Override |
230 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
231 | super.onActivityResult(requestCode, resultCode, data); |
232 | mWindowAndroid.onActivityResult(requestCode, resultCode, data); |
233 | } |
234 | |
235 | private static String getUrlFromIntent(Intent intent) { |
236 | return intent != null ? intent.getDataString() : null; |
237 | } |
238 | |
239 | private static String[] getCommandLineParamsFromIntent(Intent intent) { |
240 | return intent != null ? intent.getStringArrayExtra(COMMAND_LINE_ARGS_KEY) : null; |
241 | } |
242 | |
243 | /** |
244 | * @return The {@link ShellManager} configured for the activity or null if it has not been |
245 | * created yet. |
246 | */ |
247 | public ShellManager getShellManager() { |
248 | return mShellManager; |
249 | } |
250 | |
251 | /** |
252 | * @return The currently visible {@link Shell} or null if one is not showing. |
253 | */ |
254 | public Shell getActiveShell() { |
255 | return mShellManager != null ? mShellManager.getActiveShell() : null; |
256 | } |
257 | |
258 | /** |
259 | * @return The {@link ContentView} owned by the currently visible {@link Shell} or null if one |
260 | * is not showing. |
261 | */ |
262 | public ContentView getActiveContentView() { |
263 | Shell shell = getActiveShell(); |
264 | return shell != null ? shell.getContentView() : null; |
265 | } |
266 | } |