blob: 319ff89cf3277716ca68a366aeab890e2d4eb79a [file] [log] [blame]
John Reck847b5322011-04-14 17:02:18 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.DialogInterface;
John Reck0b3165d2011-06-22 10:44:33 -070022import android.content.DialogInterface.OnCancelListener;
John Reck847b5322011-04-14 17:02:18 -070023import android.content.DialogInterface.OnClickListener;
24import android.content.Intent;
John Reckcfeae6d2011-06-24 15:33:57 -070025import android.content.SharedPreferences;
John Reck847b5322011-04-14 17:02:18 -070026import android.os.Bundle;
John Reck378a4102011-06-09 16:23:01 -070027import android.os.Handler;
John Reckbc490d22011-07-22 15:04:59 -070028import android.os.Message;
John Reck847b5322011-04-14 17:02:18 -070029import android.os.Parcel;
30import android.util.Log;
31
32import java.io.ByteArrayOutputStream;
John Recka4816532011-06-29 14:41:59 -070033import java.io.File;
John Reck847b5322011-04-14 17:02:18 -070034import java.io.FileInputStream;
35import java.io.FileNotFoundException;
36import java.io.FileOutputStream;
Ben Murdoch679da252011-07-25 17:59:36 +010037import java.io.IOException;
John Reck847b5322011-04-14 17:02:18 -070038
39public class CrashRecoveryHandler {
40
41 private static final String LOGTAG = "BrowserCrashRecovery";
42 private static final String STATE_FILE = "browser_state.parcel";
John Reckcfeae6d2011-06-24 15:33:57 -070043 private static final String RECOVERY_PREFERENCES = "browser_recovery_prefs";
44 private static final String KEY_LAST_RECOVERED = "last_recovered";
John Reck847b5322011-04-14 17:02:18 -070045 private static final int BUFFER_SIZE = 4096;
John Reck378a4102011-06-09 16:23:01 -070046 private static final long BACKUP_DELAY = 500; // 500ms between writes
John Reckcfeae6d2011-06-24 15:33:57 -070047 /* This is the duration for which we will prompt to restore
48 * instead of automatically restoring. The first time the browser crashes,
49 * we will automatically restore. If we then crash again within XX minutes,
50 * we will prompt instead of automatically restoring.
51 */
52 private static final long PROMPT_INTERVAL = 30 * 60 * 1000; // 30 minutes
John Reck378a4102011-06-09 16:23:01 -070053
John Reckbc490d22011-07-22 15:04:59 -070054 private static final int MSG_WRITE_STATE = 1;
55 private static final int MSG_CLEAR_STATE = 2;
56 private static final int MSG_PRELOAD_STATE = 3;
57
John Reck378a4102011-06-09 16:23:01 -070058 private static CrashRecoveryHandler sInstance;
John Reck847b5322011-04-14 17:02:18 -070059
60 private Controller mController;
John Reckbc490d22011-07-22 15:04:59 -070061 private Context mContext;
John Reck378a4102011-06-09 16:23:01 -070062 private Handler mForegroundHandler;
63 private Handler mBackgroundHandler;
John Reckbc490d22011-07-22 15:04:59 -070064 private boolean mIsPreloading = false;
65 private boolean mDidPreload = false;
66 private boolean mShouldPrompt = false;
67 private Bundle mRecoveryState = null;
John Reck847b5322011-04-14 17:02:18 -070068
John Reck378a4102011-06-09 16:23:01 -070069 public static CrashRecoveryHandler initialize(Controller controller) {
70 if (sInstance == null) {
71 sInstance = new CrashRecoveryHandler(controller);
72 } else {
73 sInstance.mController = controller;
74 }
75 return sInstance;
76 }
77
78 public static CrashRecoveryHandler getInstance() {
79 return sInstance;
80 }
81
82 private CrashRecoveryHandler(Controller controller) {
John Reck847b5322011-04-14 17:02:18 -070083 mController = controller;
John Reckbc490d22011-07-22 15:04:59 -070084 mContext = mController.getActivity().getApplicationContext();
John Reck378a4102011-06-09 16:23:01 -070085 mForegroundHandler = new Handler();
John Reckcadae722011-07-25 11:36:17 -070086 mBackgroundHandler = new Handler(BackgroundHandler.getLooper()) {
John Reckbc490d22011-07-22 15:04:59 -070087
88 @Override
89 public void handleMessage(Message msg) {
90 switch (msg.what) {
91 case MSG_WRITE_STATE:
92 Parcel p = Parcel.obtain();
93 try {
94 Bundle state = (Bundle) msg.obj;
95 state.writeToParcel(p, 0);
96 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
97 FileOutputStream fout = new FileOutputStream(stateFile);
98 fout.write(p.marshall());
99 fout.close();
100 } catch (Throwable e) {
101 Log.i(LOGTAG, "Failed to save persistent state", e);
102 } finally {
103 p.recycle();
104 }
105 break;
106 case MSG_CLEAR_STATE:
107 File state = new File(mContext.getCacheDir(), STATE_FILE);
108 if (state.exists()) {
109 state.delete();
110 }
111 break;
112 case MSG_PRELOAD_STATE:
113 mRecoveryState = loadCrashState();
114 mShouldPrompt = shouldPrompt();
115 synchronized (CrashRecoveryHandler.this) {
116 mIsPreloading = false;
117 mDidPreload = true;
118 CrashRecoveryHandler.this.notifyAll();
119 }
120 break;
121 }
122 }
123 };
John Reck847b5322011-04-14 17:02:18 -0700124 }
125
126 public void backupState() {
John Reck378a4102011-06-09 16:23:01 -0700127 mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY);
John Reck847b5322011-04-14 17:02:18 -0700128 }
129
John Reck378a4102011-06-09 16:23:01 -0700130 private Runnable mCreateState = new Runnable() {
131
132 @Override
133 public void run() {
134 try {
135 final Bundle state = new Bundle();
John Reck1cf4b792011-07-26 10:22:22 -0700136 mController.onSaveInstanceState(state);
John Reckbc490d22011-07-22 15:04:59 -0700137 Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state)
138 .sendToTarget();
John Reck378a4102011-06-09 16:23:01 -0700139 // Remove any queued up saves
140 mForegroundHandler.removeCallbacks(mCreateState);
141 } catch (Throwable t) {
142 Log.w(LOGTAG, "Failed to save state", t);
143 return;
144 }
145 }
146
147 };
148
John Reckbc490d22011-07-22 15:04:59 -0700149 public void clearState() {
150 mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE);
John Reck847b5322011-04-14 17:02:18 -0700151 }
152
153 public void promptToRecover(final Bundle state, final Intent intent) {
154 new AlertDialog.Builder(mController.getActivity())
155 .setTitle(R.string.recover_title)
156 .setMessage(R.string.recover_prompt)
John Reck24f18262011-06-17 14:47:20 -0700157 .setIcon(R.mipmap.ic_launcher_browser)
John Reck847b5322011-04-14 17:02:18 -0700158 .setPositiveButton(R.string.recover_yes, new OnClickListener() {
159 @Override
160 public void onClick(DialogInterface dialog, int which) {
John Reckcfeae6d2011-06-24 15:33:57 -0700161 updateLastRecovered();
John Reck847b5322011-04-14 17:02:18 -0700162 mController.doStart(state, intent);
163 }
164 })
165 .setNegativeButton(R.string.recover_no, new OnClickListener() {
166 @Override
167 public void onClick(DialogInterface dialog, int which) {
John Reck0b3165d2011-06-22 10:44:33 -0700168 dialog.cancel();
169 }
170 })
171 .setOnCancelListener(new OnCancelListener() {
172 @Override
173 public void onCancel(DialogInterface dialog) {
John Reckbc490d22011-07-22 15:04:59 -0700174 clearState();
John Reck847b5322011-04-14 17:02:18 -0700175 mController.doStart(null, intent);
176 }
177 })
178 .show();
179 }
180
John Reckcfeae6d2011-06-24 15:33:57 -0700181 private boolean shouldPrompt() {
John Reckbc490d22011-07-22 15:04:59 -0700182 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700183 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
John Reckbc490d22011-07-22 15:04:59 -0700184 long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, 0);
John Reckcfeae6d2011-06-24 15:33:57 -0700185 long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered;
186 if (timeSinceLastRecover > PROMPT_INTERVAL) {
187 return false;
188 }
189 return true;
190 }
191
192 private void updateLastRecovered() {
John Reckbc490d22011-07-22 15:04:59 -0700193 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700194 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
195 prefs.edit()
196 .putLong(KEY_LAST_RECOVERED, System.currentTimeMillis())
Ben Murdochfb72a9a2011-07-25 18:18:32 +0100197 .apply();
John Reckcfeae6d2011-06-24 15:33:57 -0700198 }
199
John Reckbc490d22011-07-22 15:04:59 -0700200 private Bundle loadCrashState() {
John Reckcfeae6d2011-06-24 15:33:57 -0700201 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700202 Parcel parcel = Parcel.obtain();
Ben Murdoch679da252011-07-25 17:59:36 +0100203 FileInputStream fin = null;
John Reck847b5322011-04-14 17:02:18 -0700204 try {
John Reckbc490d22011-07-22 15:04:59 -0700205 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
Ben Murdoch679da252011-07-25 17:59:36 +0100206 fin = new FileInputStream(stateFile);
John Reck847b5322011-04-14 17:02:18 -0700207 ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
208 byte[] buffer = new byte[BUFFER_SIZE];
209 int read;
210 while ((read = fin.read(buffer)) > 0) {
211 dataStream.write(buffer, 0, read);
212 }
213 byte[] data = dataStream.toByteArray();
214 parcel.unmarshall(data, 0, data.length);
215 parcel.setDataPosition(0);
John Reckcfeae6d2011-06-24 15:33:57 -0700216 state = parcel.readBundle();
John Reck847b5322011-04-14 17:02:18 -0700217 } catch (FileNotFoundException e) {
218 // No state to recover
John Reckcfeae6d2011-06-24 15:33:57 -0700219 state = null;
John Reckbc490d22011-07-22 15:04:59 -0700220 } catch (Throwable e) {
John Reck847b5322011-04-14 17:02:18 -0700221 Log.w(LOGTAG, "Failed to recover state!", e);
John Reckcfeae6d2011-06-24 15:33:57 -0700222 state = null;
John Reck847b5322011-04-14 17:02:18 -0700223 } finally {
224 parcel.recycle();
Ben Murdoch679da252011-07-25 17:59:36 +0100225 if (fin != null) {
226 try {
227 fin.close();
228 } catch (IOException e) { }
229 }
John Reck847b5322011-04-14 17:02:18 -0700230 }
John Reckbc490d22011-07-22 15:04:59 -0700231 return state;
John Reck847b5322011-04-14 17:02:18 -0700232 }
John Reckbc490d22011-07-22 15:04:59 -0700233
234 public void startRecovery(Intent intent) {
235 synchronized (CrashRecoveryHandler.this) {
236 while (mIsPreloading) {
237 try {
238 CrashRecoveryHandler.this.wait();
239 } catch (InterruptedException e) {}
240 }
241 }
242 if (!mDidPreload) {
243 mRecoveryState = loadCrashState();
244 mShouldPrompt = shouldPrompt();
245 }
John Reckb89abd32011-07-29 10:13:53 -0700246 if (mRecoveryState != null && !mRecoveryState.isEmpty()) {
247 if (mShouldPrompt) {
248 promptToRecover(mRecoveryState, intent);
249 return;
250 } else {
251 updateLastRecovered();
252 }
John Reckbc490d22011-07-22 15:04:59 -0700253 }
254 mController.doStart(mRecoveryState, intent);
255 mRecoveryState = null;
256 }
257
258 public void preloadCrashState() {
259 synchronized (CrashRecoveryHandler.this) {
260 if (mIsPreloading) {
261 return;
262 }
263 mIsPreloading = true;
264 }
265 mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE);
266 }
267
John Reck847b5322011-04-14 17:02:18 -0700268}