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
3 // found in the LICENSE file.
5 package org.chromium.content_shell_apk;
7 import android.app.Activity;
8 import android.content.Intent;
9 import android.os.Bundle;
10 import android.text.TextUtils;
11 import android.util.Log;
12 import android.view.KeyEvent;
13 import android.widget.Toast;
15 import org.chromium.base.BaseSwitches;
16 import org.chromium.base.CommandLine;
17 import org.chromium.base.MemoryPressureListener;
18 import org.chromium.base.library_loader.LibraryLoader;
19 import org.chromium.base.library_loader.ProcessInitException;
20 import org.chromium.content.browser.BrowserStartupController;
21 import org.chromium.content.browser.ContentViewCore;
22 import org.chromium.content.browser.DeviceUtils;
23 import org.chromium.content.common.ContentSwitches;
24 import org.chromium.content_shell.Shell;
25 import org.chromium.content_shell.ShellManager;
26 import org.chromium.ui.base.ActivityWindowAndroid;
27 import org.chromium.ui.base.WindowAndroid;
30 * Activity for managing the Content Shell.
32 public class ContentShellActivity extends Activity {
34 public static final String COMMAND_LINE_FILE = "/data/local/tmp/content-shell-command-line";
35 private static final String TAG = "ContentShellActivity";
37 private static final String ACTIVE_SHELL_URL_KEY = "activeUrl";
38 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
40 private ShellManager mShellManager;
41 private WindowAndroid mWindowAndroid;
44 protected void onCreate(final Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
47 // Initializing the command line must occur before loading the library.
48 if (!CommandLine.isInitialized()) {
49 CommandLine.initFromFile(COMMAND_LINE_FILE);
50 String[] commandLineParams = getCommandLineParamsFromIntent(getIntent());
51 if (commandLineParams != null) {
52 CommandLine.getInstance().appendSwitchesAndArguments(commandLineParams);
55 waitForDebuggerIfNeeded();
57 DeviceUtils.addDeviceSpecificUserAgentSwitch(this);
59 LibraryLoader.ensureInitialized();
60 } catch (ProcessInitException e) {
61 Log.e(TAG, "ContentView initialization failed.", e);
62 // Since the library failed to initialize nothing in the application
63 // can work, so kill the whole application not just the activity
68 setContentView(R.layout.content_shell_activity);
69 mShellManager = (ShellManager) findViewById(R.id.shell_container);
70 mWindowAndroid = new ActivityWindowAndroid(this);
71 mWindowAndroid.restoreInstanceState(savedInstanceState);
72 mShellManager.setWindow(mWindowAndroid);
74 String startupUrl = getUrlFromIntent(getIntent());
75 if (!TextUtils.isEmpty(startupUrl)) {
76 mShellManager.setStartupUrl(Shell.sanitizeUrl(startupUrl));
79 if (CommandLine.getInstance().hasSwitch(ContentSwitches.DUMP_RENDER_TREE)) {
81 BrowserStartupController.get(this).startBrowserProcessesSync(
82 BrowserStartupController.MAX_RENDERERS_LIMIT);
83 } catch (ProcessInitException e) {
84 Log.e(TAG, "Failed to load native library.", e);
89 BrowserStartupController.get(this).startBrowserProcessesAsync(
90 new BrowserStartupController.StartupCallback() {
92 public void onSuccess(boolean alreadyStarted) {
93 finishInitialization(savedInstanceState);
97 public void onFailure() {
98 initializationFailed();
101 } catch (ProcessInitException e) {
102 Log.e(TAG, "Unable to load native library.", e);
108 private void finishInitialization(Bundle savedInstanceState) {
109 String shellUrl = ShellManager.DEFAULT_SHELL_URL;
110 if (savedInstanceState != null
111 && savedInstanceState.containsKey(ACTIVE_SHELL_URL_KEY)) {
112 shellUrl = savedInstanceState.getString(ACTIVE_SHELL_URL_KEY);
114 mShellManager.launchShell(shellUrl);
117 private void initializationFailed() {
118 Log.e(TAG, "ContentView initialization failed.");
119 Toast.makeText(ContentShellActivity.this,
120 R.string.browser_process_initialization_failed,
121 Toast.LENGTH_SHORT).show();
126 protected void onSaveInstanceState(Bundle outState) {
127 super.onSaveInstanceState(outState);
128 ContentViewCore contentViewCore = getActiveContentViewCore();
129 if (contentViewCore != null) {
130 outState.putString(ACTIVE_SHELL_URL_KEY, contentViewCore.getUrl());
133 mWindowAndroid.saveInstanceState(outState);
136 private void waitForDebuggerIfNeeded() {
137 if (CommandLine.getInstance().hasSwitch(BaseSwitches.WAIT_FOR_JAVA_DEBUGGER)) {
138 Log.e(TAG, "Waiting for Java debugger to connect...");
139 android.os.Debug.waitForDebugger();
140 Log.e(TAG, "Java debugger connected. Resuming execution.");
145 public boolean onKeyUp(int keyCode, KeyEvent event) {
146 if (keyCode == KeyEvent.KEYCODE_BACK) {
147 ContentViewCore contentViewCore = getActiveContentViewCore();
148 if (contentViewCore != null && contentViewCore.canGoBack()) {
149 contentViewCore.goBack();
154 return super.onKeyUp(keyCode, event);
158 protected void onNewIntent(Intent intent) {
159 if (getCommandLineParamsFromIntent(intent) != null) {
160 Log.i(TAG, "Ignoring command line params: can only be set when creating the activity.");
163 if (MemoryPressureListener.handleDebugIntent(this, intent.getAction())) return;
165 String url = getUrlFromIntent(intent);
166 if (!TextUtils.isEmpty(url)) {
167 Shell activeView = getActiveShell();
168 if (activeView != null) {
169 activeView.loadUrl(url);
175 protected void onStop() {
178 ContentViewCore contentViewCore = getActiveContentViewCore();
179 if (contentViewCore != null) contentViewCore.onHide();
183 protected void onStart() {
186 ContentViewCore contentViewCore = getActiveContentViewCore();
187 if (contentViewCore != null) contentViewCore.onShow();
191 public void onActivityResult(int requestCode, int resultCode, Intent data) {
192 super.onActivityResult(requestCode, resultCode, data);
193 mWindowAndroid.onActivityResult(requestCode, resultCode, data);
196 private static String getUrlFromIntent(Intent intent) {
197 return intent != null ? intent.getDataString() : null;
200 private static String[] getCommandLineParamsFromIntent(Intent intent) {
201 return intent != null ? intent.getStringArrayExtra(COMMAND_LINE_ARGS_KEY) : null;
205 * @return The {@link ShellManager} configured for the activity or null if it has not been
208 public ShellManager getShellManager() {
209 return mShellManager;
213 * @return The currently visible {@link Shell} or null if one is not showing.
215 public Shell getActiveShell() {
216 return mShellManager != null ? mShellManager.getActiveShell() : null;
220 * @return The {@link ContentViewCore} owned by the currently visible {@link Shell} or null if
221 * one is not showing.
223 public ContentViewCore getActiveContentViewCore() {
224 Shell shell = getActiveShell();
225 return shell != null ? shell.getContentViewCore() : null;