blob: c43de346f7770f3669833cc4fabe1135519204eb [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;
Ben Murdoch679da252011-07-25 17:59:36 +010038import java.io.IOException;
John Reck847b5322011-04-14 17:02:18 -070039
40public class CrashRecoveryHandler {
41
42 private static final String LOGTAG = "BrowserCrashRecovery";
43 private static final String STATE_FILE = "browser_state.parcel";
John Reckcfeae6d2011-06-24 15:33:57 -070044 private static final String RECOVERY_PREFERENCES = "browser_recovery_prefs";
45 private static final String KEY_LAST_RECOVERED = "last_recovered";
John Reck847b5322011-04-14 17:02:18 -070046 private static final int BUFFER_SIZE = 4096;
John Reck378a4102011-06-09 16:23:01 -070047 private static final long BACKUP_DELAY = 500; // 500ms between writes
John Reckcfeae6d2011-06-24 15:33:57 -070048 /* This is the duration for which we will prompt to restore
49 * instead of automatically restoring. The first time the browser crashes,
50 * we will automatically restore. If we then crash again within XX minutes,
51 * we will prompt instead of automatically restoring.
52 */
53 private static final long PROMPT_INTERVAL = 30 * 60 * 1000; // 30 minutes
John Reck378a4102011-06-09 16:23:01 -070054
John Reckbc490d22011-07-22 15:04:59 -070055 private static final int MSG_WRITE_STATE = 1;
56 private static final int MSG_CLEAR_STATE = 2;
57 private static final int MSG_PRELOAD_STATE = 3;
58
John Reck378a4102011-06-09 16:23:01 -070059 private static CrashRecoveryHandler sInstance;
John Reck847b5322011-04-14 17:02:18 -070060
61 private Controller mController;
John Reckbc490d22011-07-22 15:04:59 -070062 private Context mContext;
John Reck378a4102011-06-09 16:23:01 -070063 private Handler mForegroundHandler;
64 private Handler mBackgroundHandler;
John Reckbc490d22011-07-22 15:04:59 -070065 private boolean mIsPreloading = false;
66 private boolean mDidPreload = false;
67 private boolean mShouldPrompt = false;
68 private Bundle mRecoveryState = null;
John Reck847b5322011-04-14 17:02:18 -070069
John Reck378a4102011-06-09 16:23:01 -070070 public static CrashRecoveryHandler initialize(Controller controller) {
71 if (sInstance == null) {
72 sInstance = new CrashRecoveryHandler(controller);
73 } else {
74 sInstance.mController = controller;
75 }
76 return sInstance;
77 }
78
79 public static CrashRecoveryHandler getInstance() {
80 return sInstance;
81 }
82
83 private CrashRecoveryHandler(Controller controller) {
John Reck847b5322011-04-14 17:02:18 -070084 mController = controller;
John Reckbc490d22011-07-22 15:04:59 -070085 mContext = mController.getActivity().getApplicationContext();
John Reck378a4102011-06-09 16:23:01 -070086 mForegroundHandler = new Handler();
John Reckf57c0292011-07-21 18:15:39 -070087 Looper looper = BrowserSettings.getInstance().getBackgroundLooper();
John Reckbc490d22011-07-22 15:04:59 -070088 mBackgroundHandler = new Handler(looper) {
89
90 @Override
91 public void handleMessage(Message msg) {
92 switch (msg.what) {
93 case MSG_WRITE_STATE:
94 Parcel p = Parcel.obtain();
95 try {
96 Bundle state = (Bundle) msg.obj;
97 state.writeToParcel(p, 0);
98 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
99 FileOutputStream fout = new FileOutputStream(stateFile);
100 fout.write(p.marshall());
101 fout.close();
102 } catch (Throwable e) {
103 Log.i(LOGTAG, "Failed to save persistent state", e);
104 } finally {
105 p.recycle();
106 }
107 break;
108 case MSG_CLEAR_STATE:
109 File state = new File(mContext.getCacheDir(), STATE_FILE);
110 if (state.exists()) {
111 state.delete();
112 }
113 break;
114 case MSG_PRELOAD_STATE:
115 mRecoveryState = loadCrashState();
116 mShouldPrompt = shouldPrompt();
117 synchronized (CrashRecoveryHandler.this) {
118 mIsPreloading = false;
119 mDidPreload = true;
120 CrashRecoveryHandler.this.notifyAll();
121 }
122 break;
123 }
124 }
125 };
John Reck847b5322011-04-14 17:02:18 -0700126 }
127
128 public void backupState() {
John Reck378a4102011-06-09 16:23:01 -0700129 mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY);
John Reck847b5322011-04-14 17:02:18 -0700130 }
131
John Reck378a4102011-06-09 16:23:01 -0700132 private Runnable mCreateState = new Runnable() {
133
134 @Override
135 public void run() {
136 try {
137 final Bundle state = new Bundle();
138 mController.onSaveInstanceState(state, false);
John Reckbc490d22011-07-22 15:04:59 -0700139 Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state)
140 .sendToTarget();
John Reck378a4102011-06-09 16:23:01 -0700141 // Remove any queued up saves
142 mForegroundHandler.removeCallbacks(mCreateState);
143 } catch (Throwable t) {
144 Log.w(LOGTAG, "Failed to save state", t);
145 return;
146 }
147 }
148
149 };
150
John Reckbc490d22011-07-22 15:04:59 -0700151 public void clearState() {
152 mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE);
John Reck847b5322011-04-14 17:02:18 -0700153 }
154
155 public void promptToRecover(final Bundle state, final Intent intent) {
156 new AlertDialog.Builder(mController.getActivity())
157 .setTitle(R.string.recover_title)
158 .setMessage(R.string.recover_prompt)
John Reck24f18262011-06-17 14:47:20 -0700159 .setIcon(R.mipmap.ic_launcher_browser)
John Reck847b5322011-04-14 17:02:18 -0700160 .setPositiveButton(R.string.recover_yes, new OnClickListener() {
161 @Override
162 public void onClick(DialogInterface dialog, int which) {
John Reckcfeae6d2011-06-24 15:33:57 -0700163 updateLastRecovered();
John Reck847b5322011-04-14 17:02:18 -0700164 mController.doStart(state, intent);
165 }
166 })
167 .setNegativeButton(R.string.recover_no, new OnClickListener() {
168 @Override
169 public void onClick(DialogInterface dialog, int which) {
John Reck0b3165d2011-06-22 10:44:33 -0700170 dialog.cancel();
171 }
172 })
173 .setOnCancelListener(new OnCancelListener() {
174 @Override
175 public void onCancel(DialogInterface dialog) {
John Reckbc490d22011-07-22 15:04:59 -0700176 clearState();
John Reck847b5322011-04-14 17:02:18 -0700177 mController.doStart(null, intent);
178 }
179 })
180 .show();
181 }
182
John Reckcfeae6d2011-06-24 15:33:57 -0700183 private boolean shouldPrompt() {
John Reckbc490d22011-07-22 15:04:59 -0700184 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700185 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
John Reckbc490d22011-07-22 15:04:59 -0700186 long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, 0);
John Reckcfeae6d2011-06-24 15:33:57 -0700187 long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered;
188 if (timeSinceLastRecover > PROMPT_INTERVAL) {
189 return false;
190 }
191 return true;
192 }
193
194 private void updateLastRecovered() {
John Reckbc490d22011-07-22 15:04:59 -0700195 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700196 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
197 prefs.edit()
198 .putLong(KEY_LAST_RECOVERED, System.currentTimeMillis())
Ben Murdochfb72a9a2011-07-25 18:18:32 +0100199 .apply();
John Reckcfeae6d2011-06-24 15:33:57 -0700200 }
201
John Reckbc490d22011-07-22 15:04:59 -0700202 private Bundle loadCrashState() {
John Reckcfeae6d2011-06-24 15:33:57 -0700203 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700204 Parcel parcel = Parcel.obtain();
Ben Murdoch679da252011-07-25 17:59:36 +0100205 FileInputStream fin = null;
John Reck847b5322011-04-14 17:02:18 -0700206 try {
John Reckbc490d22011-07-22 15:04:59 -0700207 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
Ben Murdoch679da252011-07-25 17:59:36 +0100208 fin = new FileInputStream(stateFile);
John Reck847b5322011-04-14 17:02:18 -0700209 ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
210 byte[] buffer = new byte[BUFFER_SIZE];
211 int read;
212 while ((read = fin.read(buffer)) > 0) {
213 dataStream.write(buffer, 0, read);
214 }
215 byte[] data = dataStream.toByteArray();
216 parcel.unmarshall(data, 0, data.length);
217 parcel.setDataPosition(0);
John Reckcfeae6d2011-06-24 15:33:57 -0700218 state = parcel.readBundle();
John Reck847b5322011-04-14 17:02:18 -0700219 } catch (FileNotFoundException e) {
220 // No state to recover
John Reckcfeae6d2011-06-24 15:33:57 -0700221 state = null;
John Reckbc490d22011-07-22 15:04:59 -0700222 } catch (Throwable e) {
John Reck847b5322011-04-14 17:02:18 -0700223 Log.w(LOGTAG, "Failed to recover state!", e);
John Reckcfeae6d2011-06-24 15:33:57 -0700224 state = null;
John Reck847b5322011-04-14 17:02:18 -0700225 } finally {
226 parcel.recycle();
Ben Murdoch679da252011-07-25 17:59:36 +0100227 if (fin != null) {
228 try {
229 fin.close();
230 } catch (IOException e) { }
231 }
John Reck847b5322011-04-14 17:02:18 -0700232 }
John Reckbc490d22011-07-22 15:04:59 -0700233 return state;
John Reck847b5322011-04-14 17:02:18 -0700234 }
John Reckbc490d22011-07-22 15:04:59 -0700235
236 public void startRecovery(Intent intent) {
237 synchronized (CrashRecoveryHandler.this) {
238 while (mIsPreloading) {
239 try {
240 CrashRecoveryHandler.this.wait();
241 } catch (InterruptedException e) {}
242 }
243 }
244 if (!mDidPreload) {
245 mRecoveryState = loadCrashState();
246 mShouldPrompt = shouldPrompt();
247 }
248 if (mShouldPrompt) {
249 promptToRecover(mRecoveryState, intent);
250 return;
251 } else {
252 updateLastRecovered();
253 }
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}