blob: 995d1197d8267359a2ea7b78308abae3e0e9171e [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 Reckf57c0292011-07-21 18:15:39 -070028import android.os.Looper;
John Reckbc490d22011-07-22 15:04:59 -070029import android.os.Message;
John Reck847b5322011-04-14 17:02:18 -070030import android.os.Parcel;
31import android.util.Log;
32
33import java.io.ByteArrayOutputStream;
John Recka4816532011-06-29 14:41:59 -070034import java.io.File;
John Reck847b5322011-04-14 17:02:18 -070035import java.io.FileInputStream;
36import java.io.FileNotFoundException;
37import java.io.FileOutputStream;
38
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 Reckf57c0292011-07-21 18:15:39 -070086 Looper looper = BrowserSettings.getInstance().getBackgroundLooper();
John Reckbc490d22011-07-22 15:04:59 -070087 mBackgroundHandler = new Handler(looper) {
88
89 @Override
90 public void handleMessage(Message msg) {
91 switch (msg.what) {
92 case MSG_WRITE_STATE:
93 Parcel p = Parcel.obtain();
94 try {
95 Bundle state = (Bundle) msg.obj;
96 state.writeToParcel(p, 0);
97 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
98 FileOutputStream fout = new FileOutputStream(stateFile);
99 fout.write(p.marshall());
100 fout.close();
101 } catch (Throwable e) {
102 Log.i(LOGTAG, "Failed to save persistent state", e);
103 } finally {
104 p.recycle();
105 }
106 break;
107 case MSG_CLEAR_STATE:
108 File state = new File(mContext.getCacheDir(), STATE_FILE);
109 if (state.exists()) {
110 state.delete();
111 }
112 break;
113 case MSG_PRELOAD_STATE:
114 mRecoveryState = loadCrashState();
115 mShouldPrompt = shouldPrompt();
116 synchronized (CrashRecoveryHandler.this) {
117 mIsPreloading = false;
118 mDidPreload = true;
119 CrashRecoveryHandler.this.notifyAll();
120 }
121 break;
122 }
123 }
124 };
John Reck847b5322011-04-14 17:02:18 -0700125 }
126
127 public void backupState() {
John Reck378a4102011-06-09 16:23:01 -0700128 mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY);
John Reck847b5322011-04-14 17:02:18 -0700129 }
130
John Reck378a4102011-06-09 16:23:01 -0700131 private Runnable mCreateState = new Runnable() {
132
133 @Override
134 public void run() {
135 try {
136 final Bundle state = new Bundle();
137 mController.onSaveInstanceState(state, false);
John Reckbc490d22011-07-22 15:04:59 -0700138 Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state)
139 .sendToTarget();
John Reck378a4102011-06-09 16:23:01 -0700140 // Remove any queued up saves
141 mForegroundHandler.removeCallbacks(mCreateState);
142 } catch (Throwable t) {
143 Log.w(LOGTAG, "Failed to save state", t);
144 return;
145 }
146 }
147
148 };
149
John Reckbc490d22011-07-22 15:04:59 -0700150 public void clearState() {
151 mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE);
John Reck847b5322011-04-14 17:02:18 -0700152 }
153
154 public void promptToRecover(final Bundle state, final Intent intent) {
155 new AlertDialog.Builder(mController.getActivity())
156 .setTitle(R.string.recover_title)
157 .setMessage(R.string.recover_prompt)
John Reck24f18262011-06-17 14:47:20 -0700158 .setIcon(R.mipmap.ic_launcher_browser)
John Reck847b5322011-04-14 17:02:18 -0700159 .setPositiveButton(R.string.recover_yes, new OnClickListener() {
160 @Override
161 public void onClick(DialogInterface dialog, int which) {
John Reckcfeae6d2011-06-24 15:33:57 -0700162 updateLastRecovered();
John Reck847b5322011-04-14 17:02:18 -0700163 mController.doStart(state, intent);
164 }
165 })
166 .setNegativeButton(R.string.recover_no, new OnClickListener() {
167 @Override
168 public void onClick(DialogInterface dialog, int which) {
John Reck0b3165d2011-06-22 10:44:33 -0700169 dialog.cancel();
170 }
171 })
172 .setOnCancelListener(new OnCancelListener() {
173 @Override
174 public void onCancel(DialogInterface dialog) {
John Reckbc490d22011-07-22 15:04:59 -0700175 clearState();
John Reck847b5322011-04-14 17:02:18 -0700176 mController.doStart(null, intent);
177 }
178 })
179 .show();
180 }
181
John Reckcfeae6d2011-06-24 15:33:57 -0700182 private boolean shouldPrompt() {
John Reckbc490d22011-07-22 15:04:59 -0700183 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700184 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
John Reckbc490d22011-07-22 15:04:59 -0700185 long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, 0);
John Reckcfeae6d2011-06-24 15:33:57 -0700186 long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered;
187 if (timeSinceLastRecover > PROMPT_INTERVAL) {
188 return false;
189 }
190 return true;
191 }
192
193 private void updateLastRecovered() {
John Reckbc490d22011-07-22 15:04:59 -0700194 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700195 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
196 prefs.edit()
197 .putLong(KEY_LAST_RECOVERED, System.currentTimeMillis())
198 .commit();
199 }
200
John Reckbc490d22011-07-22 15:04:59 -0700201 private Bundle loadCrashState() {
John Reckcfeae6d2011-06-24 15:33:57 -0700202 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700203 Parcel parcel = Parcel.obtain();
204 try {
John Reckbc490d22011-07-22 15:04:59 -0700205 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
John Recka4816532011-06-29 14:41:59 -0700206 FileInputStream 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();
225 }
John Reckbc490d22011-07-22 15:04:59 -0700226 return state;
John Reck847b5322011-04-14 17:02:18 -0700227 }
John Reckbc490d22011-07-22 15:04:59 -0700228
229 public void startRecovery(Intent intent) {
230 synchronized (CrashRecoveryHandler.this) {
231 while (mIsPreloading) {
232 try {
233 CrashRecoveryHandler.this.wait();
234 } catch (InterruptedException e) {}
235 }
236 }
237 if (!mDidPreload) {
238 mRecoveryState = loadCrashState();
239 mShouldPrompt = shouldPrompt();
240 }
241 if (mShouldPrompt) {
242 promptToRecover(mRecoveryState, intent);
243 return;
244 } else {
245 updateLastRecovered();
246 }
247 mController.doStart(mRecoveryState, intent);
248 mRecoveryState = null;
249 }
250
251 public void preloadCrashState() {
252 synchronized (CrashRecoveryHandler.this) {
253 if (mIsPreloading) {
254 return;
255 }
256 mIsPreloading = true;
257 }
258 mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE);
259 }
260
John Reck847b5322011-04-14 17:02:18 -0700261}