blob: 822e82a80f4382988973a0b60770c390c39b321a [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
John Reck847b5322011-04-14 17:02:18 -070019import android.content.Context;
John Reck847b5322011-04-14 17:02:18 -070020import android.content.Intent;
John Reckcfeae6d2011-06-24 15:33:57 -070021import android.content.SharedPreferences;
John Reck847b5322011-04-14 17:02:18 -070022import android.os.Bundle;
John Reck378a4102011-06-09 16:23:01 -070023import android.os.Handler;
John Reckbc490d22011-07-22 15:04:59 -070024import android.os.Message;
John Reck847b5322011-04-14 17:02:18 -070025import android.os.Parcel;
26import android.util.Log;
27
28import java.io.ByteArrayOutputStream;
John Recka4816532011-06-29 14:41:59 -070029import java.io.File;
John Reck847b5322011-04-14 17:02:18 -070030import java.io.FileInputStream;
31import java.io.FileNotFoundException;
32import java.io.FileOutputStream;
Ben Murdoch679da252011-07-25 17:59:36 +010033import java.io.IOException;
John Reck847b5322011-04-14 17:02:18 -070034
35public class CrashRecoveryHandler {
36
John Reck6c2e2f32011-08-22 13:41:23 -070037 private static final boolean LOGV_ENABLED = Browser.LOGV_ENABLED;
John Reck847b5322011-04-14 17:02:18 -070038 private static final String LOGTAG = "BrowserCrashRecovery";
39 private static final String STATE_FILE = "browser_state.parcel";
40 private static final int BUFFER_SIZE = 4096;
John Reck378a4102011-06-09 16:23:01 -070041 private static final long BACKUP_DELAY = 500; // 500ms between writes
John Reckcfeae6d2011-06-24 15:33:57 -070042 /* This is the duration for which we will prompt to restore
43 * instead of automatically restoring. The first time the browser crashes,
44 * we will automatically restore. If we then crash again within XX minutes,
45 * we will prompt instead of automatically restoring.
46 */
John Reck023124e2011-07-29 11:32:54 -070047 private static final long PROMPT_INTERVAL = 5 * 60 * 1000; // 5 minutes
John Reck378a4102011-06-09 16:23:01 -070048
John Reckbc490d22011-07-22 15:04:59 -070049 private static final int MSG_WRITE_STATE = 1;
50 private static final int MSG_CLEAR_STATE = 2;
51 private static final int MSG_PRELOAD_STATE = 3;
52
John Reck378a4102011-06-09 16:23:01 -070053 private static CrashRecoveryHandler sInstance;
John Reck847b5322011-04-14 17:02:18 -070054
55 private Controller mController;
John Reckbc490d22011-07-22 15:04:59 -070056 private Context mContext;
John Reck378a4102011-06-09 16:23:01 -070057 private Handler mForegroundHandler;
58 private Handler mBackgroundHandler;
John Reckbc490d22011-07-22 15:04:59 -070059 private boolean mIsPreloading = false;
60 private boolean mDidPreload = false;
John Reckbc490d22011-07-22 15:04:59 -070061 private Bundle mRecoveryState = null;
John Reck847b5322011-04-14 17:02:18 -070062
John Reck378a4102011-06-09 16:23:01 -070063 public static CrashRecoveryHandler initialize(Controller controller) {
64 if (sInstance == null) {
65 sInstance = new CrashRecoveryHandler(controller);
66 } else {
67 sInstance.mController = controller;
68 }
69 return sInstance;
70 }
71
72 public static CrashRecoveryHandler getInstance() {
73 return sInstance;
74 }
75
76 private CrashRecoveryHandler(Controller controller) {
John Reck847b5322011-04-14 17:02:18 -070077 mController = controller;
John Reckbc490d22011-07-22 15:04:59 -070078 mContext = mController.getActivity().getApplicationContext();
John Reck378a4102011-06-09 16:23:01 -070079 mForegroundHandler = new Handler();
John Reckcadae722011-07-25 11:36:17 -070080 mBackgroundHandler = new Handler(BackgroundHandler.getLooper()) {
John Reckbc490d22011-07-22 15:04:59 -070081
82 @Override
83 public void handleMessage(Message msg) {
84 switch (msg.what) {
85 case MSG_WRITE_STATE:
George Mount3636d0a2011-11-21 09:08:21 -080086 Bundle saveState = (Bundle) msg.obj;
87 writeState(saveState);
John Reckbc490d22011-07-22 15:04:59 -070088 break;
89 case MSG_CLEAR_STATE:
John Reck6c2e2f32011-08-22 13:41:23 -070090 if (LOGV_ENABLED) {
91 Log.v(LOGTAG, "Clearing crash recovery state");
92 }
John Reckbc490d22011-07-22 15:04:59 -070093 File state = new File(mContext.getCacheDir(), STATE_FILE);
94 if (state.exists()) {
95 state.delete();
96 }
97 break;
98 case MSG_PRELOAD_STATE:
99 mRecoveryState = loadCrashState();
John Reckbc490d22011-07-22 15:04:59 -0700100 synchronized (CrashRecoveryHandler.this) {
101 mIsPreloading = false;
102 mDidPreload = true;
103 CrashRecoveryHandler.this.notifyAll();
104 }
105 break;
106 }
107 }
108 };
John Reck847b5322011-04-14 17:02:18 -0700109 }
110
111 public void backupState() {
John Reck378a4102011-06-09 16:23:01 -0700112 mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY);
John Reck847b5322011-04-14 17:02:18 -0700113 }
114
John Reck378a4102011-06-09 16:23:01 -0700115 private Runnable mCreateState = new Runnable() {
116
117 @Override
118 public void run() {
119 try {
George Mount3636d0a2011-11-21 09:08:21 -0800120 final Bundle state = mController.createSaveState();
John Reckbc490d22011-07-22 15:04:59 -0700121 Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state)
122 .sendToTarget();
John Reck378a4102011-06-09 16:23:01 -0700123 // Remove any queued up saves
124 mForegroundHandler.removeCallbacks(mCreateState);
125 } catch (Throwable t) {
126 Log.w(LOGTAG, "Failed to save state", t);
127 return;
128 }
129 }
130
131 };
132
John Reckbc490d22011-07-22 15:04:59 -0700133 public void clearState() {
134 mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE);
John Reck023124e2011-07-29 11:32:54 -0700135 updateLastRecovered(0);
John Reck847b5322011-04-14 17:02:18 -0700136 }
137
John Reck023124e2011-07-29 11:32:54 -0700138 private boolean shouldRestore() {
George Mount3636d0a2011-11-21 09:08:21 -0800139 BrowserSettings browserSettings = BrowserSettings.getInstance();
140 long lastRecovered = browserSettings.getLastRecovered();
John Reckcfeae6d2011-06-24 15:33:57 -0700141 long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered;
George Mount3636d0a2011-11-21 09:08:21 -0800142 return (timeSinceLastRecover > PROMPT_INTERVAL)
143 || browserSettings.wasLastRunPaused();
John Reckcfeae6d2011-06-24 15:33:57 -0700144 }
145
John Reck023124e2011-07-29 11:32:54 -0700146 private void updateLastRecovered(long time) {
George Mount3636d0a2011-11-21 09:08:21 -0800147 BrowserSettings browserSettings = BrowserSettings.getInstance();
148 browserSettings.setLastRecovered(time);
John Reckcfeae6d2011-06-24 15:33:57 -0700149 }
150
George Mount3636d0a2011-11-21 09:08:21 -0800151 synchronized private Bundle loadCrashState() {
John Reck023124e2011-07-29 11:32:54 -0700152 if (!shouldRestore()) {
153 return null;
154 }
George Mount3636d0a2011-11-21 09:08:21 -0800155 BrowserSettings browserSettings = BrowserSettings.getInstance();
156 browserSettings.setLastRunPaused(false);
John Reckcfeae6d2011-06-24 15:33:57 -0700157 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700158 Parcel parcel = Parcel.obtain();
Ben Murdoch679da252011-07-25 17:59:36 +0100159 FileInputStream fin = null;
John Reck847b5322011-04-14 17:02:18 -0700160 try {
John Reckbc490d22011-07-22 15:04:59 -0700161 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
Ben Murdoch679da252011-07-25 17:59:36 +0100162 fin = new FileInputStream(stateFile);
John Reck847b5322011-04-14 17:02:18 -0700163 ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
164 byte[] buffer = new byte[BUFFER_SIZE];
165 int read;
166 while ((read = fin.read(buffer)) > 0) {
167 dataStream.write(buffer, 0, read);
168 }
169 byte[] data = dataStream.toByteArray();
170 parcel.unmarshall(data, 0, data.length);
171 parcel.setDataPosition(0);
John Reckcfeae6d2011-06-24 15:33:57 -0700172 state = parcel.readBundle();
John Recke659d7b2011-10-13 15:51:02 -0700173 if (state != null && !state.isEmpty()) {
174 return state;
175 }
John Reck847b5322011-04-14 17:02:18 -0700176 } catch (FileNotFoundException e) {
177 // No state to recover
John Reckbc490d22011-07-22 15:04:59 -0700178 } catch (Throwable e) {
John Reck847b5322011-04-14 17:02:18 -0700179 Log.w(LOGTAG, "Failed to recover state!", e);
John Reck847b5322011-04-14 17:02:18 -0700180 } finally {
181 parcel.recycle();
Ben Murdoch679da252011-07-25 17:59:36 +0100182 if (fin != null) {
183 try {
184 fin.close();
185 } catch (IOException e) { }
186 }
John Reck847b5322011-04-14 17:02:18 -0700187 }
John Reck023124e2011-07-29 11:32:54 -0700188 return null;
John Reck847b5322011-04-14 17:02:18 -0700189 }
John Reckbc490d22011-07-22 15:04:59 -0700190
191 public void startRecovery(Intent intent) {
192 synchronized (CrashRecoveryHandler.this) {
193 while (mIsPreloading) {
194 try {
195 CrashRecoveryHandler.this.wait();
196 } catch (InterruptedException e) {}
197 }
198 }
199 if (!mDidPreload) {
200 mRecoveryState = loadCrashState();
John Reckbc490d22011-07-22 15:04:59 -0700201 }
John Reck023124e2011-07-29 11:32:54 -0700202 updateLastRecovered(mRecoveryState != null
203 ? System.currentTimeMillis() : 0);
George Mount3636d0a2011-11-21 09:08:21 -0800204 mController.doStart(mRecoveryState, intent);
John Reckbc490d22011-07-22 15:04:59 -0700205 mRecoveryState = null;
206 }
207
208 public void preloadCrashState() {
209 synchronized (CrashRecoveryHandler.this) {
210 if (mIsPreloading) {
211 return;
212 }
213 mIsPreloading = true;
214 }
215 mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE);
216 }
217
George Mount3636d0a2011-11-21 09:08:21 -0800218 /**
219 * Writes the crash recovery state to a file synchronously.
220 * Errors are swallowed, but logged.
221 * @param state The state to write out
222 */
223 synchronized void writeState(Bundle state) {
224 if (LOGV_ENABLED) {
225 Log.v(LOGTAG, "Saving crash recovery state");
226 }
227 Parcel p = Parcel.obtain();
228 try {
229 state.writeToParcel(p, 0);
230 File stateJournal = new File(mContext.getCacheDir(),
231 STATE_FILE + ".journal");
232 FileOutputStream fout = new FileOutputStream(stateJournal);
233 fout.write(p.marshall());
234 fout.close();
235 File stateFile = new File(mContext.getCacheDir(),
236 STATE_FILE);
237 if (!stateJournal.renameTo(stateFile)) {
238 // Failed to rename, try deleting the existing
239 // file and try again
240 stateFile.delete();
241 stateJournal.renameTo(stateFile);
242 }
243 } catch (Throwable e) {
244 Log.i(LOGTAG, "Failed to save persistent state", e);
245 } finally {
246 p.recycle();
247 }
248 }
249}