blob: aa48aacd7344b0f17e7b72337875758d7bcf9acd [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;
37
38public class CrashRecoveryHandler {
39
40 private static final String LOGTAG = "BrowserCrashRecovery";
41 private static final String STATE_FILE = "browser_state.parcel";
John Reckcfeae6d2011-06-24 15:33:57 -070042 private static final String RECOVERY_PREFERENCES = "browser_recovery_prefs";
43 private static final String KEY_LAST_RECOVERED = "last_recovered";
John Reck847b5322011-04-14 17:02:18 -070044 private static final int BUFFER_SIZE = 4096;
John Reck378a4102011-06-09 16:23:01 -070045 private static final long BACKUP_DELAY = 500; // 500ms between writes
John Reckcfeae6d2011-06-24 15:33:57 -070046 /* This is the duration for which we will prompt to restore
47 * instead of automatically restoring. The first time the browser crashes,
48 * we will automatically restore. If we then crash again within XX minutes,
49 * we will prompt instead of automatically restoring.
50 */
51 private static final long PROMPT_INTERVAL = 30 * 60 * 1000; // 30 minutes
John Reck378a4102011-06-09 16:23:01 -070052
John Reckbc490d22011-07-22 15:04:59 -070053 private static final int MSG_WRITE_STATE = 1;
54 private static final int MSG_CLEAR_STATE = 2;
55 private static final int MSG_PRELOAD_STATE = 3;
56
John Reck378a4102011-06-09 16:23:01 -070057 private static CrashRecoveryHandler sInstance;
John Reck847b5322011-04-14 17:02:18 -070058
59 private Controller mController;
John Reckbc490d22011-07-22 15:04:59 -070060 private Context mContext;
John Reck378a4102011-06-09 16:23:01 -070061 private Handler mForegroundHandler;
62 private Handler mBackgroundHandler;
John Reckbc490d22011-07-22 15:04:59 -070063 private boolean mIsPreloading = false;
64 private boolean mDidPreload = false;
65 private boolean mShouldPrompt = false;
66 private Bundle mRecoveryState = null;
John Reck847b5322011-04-14 17:02:18 -070067
John Reck378a4102011-06-09 16:23:01 -070068 public static CrashRecoveryHandler initialize(Controller controller) {
69 if (sInstance == null) {
70 sInstance = new CrashRecoveryHandler(controller);
71 } else {
72 sInstance.mController = controller;
73 }
74 return sInstance;
75 }
76
77 public static CrashRecoveryHandler getInstance() {
78 return sInstance;
79 }
80
81 private CrashRecoveryHandler(Controller controller) {
John Reck847b5322011-04-14 17:02:18 -070082 mController = controller;
John Reckbc490d22011-07-22 15:04:59 -070083 mContext = mController.getActivity().getApplicationContext();
John Reck378a4102011-06-09 16:23:01 -070084 mForegroundHandler = new Handler();
John Reckcadae722011-07-25 11:36:17 -070085 mBackgroundHandler = new Handler(BackgroundHandler.getLooper()) {
John Reckbc490d22011-07-22 15:04:59 -070086
87 @Override
88 public void handleMessage(Message msg) {
89 switch (msg.what) {
90 case MSG_WRITE_STATE:
91 Parcel p = Parcel.obtain();
92 try {
93 Bundle state = (Bundle) msg.obj;
94 state.writeToParcel(p, 0);
95 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
96 FileOutputStream fout = new FileOutputStream(stateFile);
97 fout.write(p.marshall());
98 fout.close();
99 } catch (Throwable e) {
100 Log.i(LOGTAG, "Failed to save persistent state", e);
101 } finally {
102 p.recycle();
103 }
104 break;
105 case MSG_CLEAR_STATE:
106 File state = new File(mContext.getCacheDir(), STATE_FILE);
107 if (state.exists()) {
108 state.delete();
109 }
110 break;
111 case MSG_PRELOAD_STATE:
112 mRecoveryState = loadCrashState();
113 mShouldPrompt = shouldPrompt();
114 synchronized (CrashRecoveryHandler.this) {
115 mIsPreloading = false;
116 mDidPreload = true;
117 CrashRecoveryHandler.this.notifyAll();
118 }
119 break;
120 }
121 }
122 };
John Reck847b5322011-04-14 17:02:18 -0700123 }
124
125 public void backupState() {
John Reck378a4102011-06-09 16:23:01 -0700126 mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY);
John Reck847b5322011-04-14 17:02:18 -0700127 }
128
John Reck378a4102011-06-09 16:23:01 -0700129 private Runnable mCreateState = new Runnable() {
130
131 @Override
132 public void run() {
133 try {
134 final Bundle state = new Bundle();
135 mController.onSaveInstanceState(state, false);
John Reckbc490d22011-07-22 15:04:59 -0700136 Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state)
137 .sendToTarget();
John Reck378a4102011-06-09 16:23:01 -0700138 // Remove any queued up saves
139 mForegroundHandler.removeCallbacks(mCreateState);
140 } catch (Throwable t) {
141 Log.w(LOGTAG, "Failed to save state", t);
142 return;
143 }
144 }
145
146 };
147
John Reckbc490d22011-07-22 15:04:59 -0700148 public void clearState() {
149 mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE);
John Reck847b5322011-04-14 17:02:18 -0700150 }
151
152 public void promptToRecover(final Bundle state, final Intent intent) {
153 new AlertDialog.Builder(mController.getActivity())
154 .setTitle(R.string.recover_title)
155 .setMessage(R.string.recover_prompt)
John Reck24f18262011-06-17 14:47:20 -0700156 .setIcon(R.mipmap.ic_launcher_browser)
John Reck847b5322011-04-14 17:02:18 -0700157 .setPositiveButton(R.string.recover_yes, new OnClickListener() {
158 @Override
159 public void onClick(DialogInterface dialog, int which) {
John Reckcfeae6d2011-06-24 15:33:57 -0700160 updateLastRecovered();
John Reck847b5322011-04-14 17:02:18 -0700161 mController.doStart(state, intent);
162 }
163 })
164 .setNegativeButton(R.string.recover_no, new OnClickListener() {
165 @Override
166 public void onClick(DialogInterface dialog, int which) {
John Reck0b3165d2011-06-22 10:44:33 -0700167 dialog.cancel();
168 }
169 })
170 .setOnCancelListener(new OnCancelListener() {
171 @Override
172 public void onCancel(DialogInterface dialog) {
John Reckbc490d22011-07-22 15:04:59 -0700173 clearState();
John Reck847b5322011-04-14 17:02:18 -0700174 mController.doStart(null, intent);
175 }
176 })
177 .show();
178 }
179
John Reckcfeae6d2011-06-24 15:33:57 -0700180 private boolean shouldPrompt() {
John Reckbc490d22011-07-22 15:04:59 -0700181 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700182 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
John Reckbc490d22011-07-22 15:04:59 -0700183 long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, 0);
John Reckcfeae6d2011-06-24 15:33:57 -0700184 long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered;
185 if (timeSinceLastRecover > PROMPT_INTERVAL) {
186 return false;
187 }
188 return true;
189 }
190
191 private void updateLastRecovered() {
John Reckbc490d22011-07-22 15:04:59 -0700192 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700193 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
194 prefs.edit()
195 .putLong(KEY_LAST_RECOVERED, System.currentTimeMillis())
196 .commit();
197 }
198
John Reckbc490d22011-07-22 15:04:59 -0700199 private Bundle loadCrashState() {
John Reckcfeae6d2011-06-24 15:33:57 -0700200 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700201 Parcel parcel = Parcel.obtain();
202 try {
John Reckbc490d22011-07-22 15:04:59 -0700203 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
John Recka4816532011-06-29 14:41:59 -0700204 FileInputStream fin = new FileInputStream(stateFile);
John Reck847b5322011-04-14 17:02:18 -0700205 ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
206 byte[] buffer = new byte[BUFFER_SIZE];
207 int read;
208 while ((read = fin.read(buffer)) > 0) {
209 dataStream.write(buffer, 0, read);
210 }
211 byte[] data = dataStream.toByteArray();
212 parcel.unmarshall(data, 0, data.length);
213 parcel.setDataPosition(0);
John Reckcfeae6d2011-06-24 15:33:57 -0700214 state = parcel.readBundle();
John Reck847b5322011-04-14 17:02:18 -0700215 } catch (FileNotFoundException e) {
216 // No state to recover
John Reckcfeae6d2011-06-24 15:33:57 -0700217 state = null;
John Reckbc490d22011-07-22 15:04:59 -0700218 } catch (Throwable e) {
John Reck847b5322011-04-14 17:02:18 -0700219 Log.w(LOGTAG, "Failed to recover state!", e);
John Reckcfeae6d2011-06-24 15:33:57 -0700220 state = null;
John Reck847b5322011-04-14 17:02:18 -0700221 } finally {
222 parcel.recycle();
223 }
John Reckbc490d22011-07-22 15:04:59 -0700224 return state;
John Reck847b5322011-04-14 17:02:18 -0700225 }
John Reckbc490d22011-07-22 15:04:59 -0700226
227 public void startRecovery(Intent intent) {
228 synchronized (CrashRecoveryHandler.this) {
229 while (mIsPreloading) {
230 try {
231 CrashRecoveryHandler.this.wait();
232 } catch (InterruptedException e) {}
233 }
234 }
235 if (!mDidPreload) {
236 mRecoveryState = loadCrashState();
237 mShouldPrompt = shouldPrompt();
238 }
239 if (mShouldPrompt) {
240 promptToRecover(mRecoveryState, intent);
241 return;
242 } else {
243 updateLastRecovered();
244 }
245 mController.doStart(mRecoveryState, intent);
246 mRecoveryState = null;
247 }
248
249 public void preloadCrashState() {
250 synchronized (CrashRecoveryHandler.this) {
251 if (mIsPreloading) {
252 return;
253 }
254 mIsPreloading = true;
255 }
256 mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE);
257 }
258
John Reck847b5322011-04-14 17:02:18 -0700259}