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