blob: bcdf8b03a91804b969f603a723ebec7312197363 [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() {
kaiyiza8b6dbb2013-07-29 18:11:22 +0800134 clearState(false);
135 }
136
137 /**
138 * Clear cached state files.
139 *
140 * @param block If block, clear state files in the caller thread, otherwise
141 * do it in a worker thread.
142 */
143 void clearState(boolean block) {
144 if (block) {
145 if (mContext != null) {
146 File state = new File(mContext.getCacheDir(), STATE_FILE);
147 if (state.exists()) {
148 state.delete();
149 }
150 }
151 } else {
152 mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE);
153 }
John Reck023124e2011-07-29 11:32:54 -0700154 updateLastRecovered(0);
John Reck847b5322011-04-14 17:02:18 -0700155 }
156
John Reck023124e2011-07-29 11:32:54 -0700157 private boolean shouldRestore() {
George Mount3636d0a2011-11-21 09:08:21 -0800158 BrowserSettings browserSettings = BrowserSettings.getInstance();
159 long lastRecovered = browserSettings.getLastRecovered();
John Reckcfeae6d2011-06-24 15:33:57 -0700160 long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered;
George Mount3636d0a2011-11-21 09:08:21 -0800161 return (timeSinceLastRecover > PROMPT_INTERVAL)
162 || browserSettings.wasLastRunPaused();
John Reckcfeae6d2011-06-24 15:33:57 -0700163 }
164
John Reck023124e2011-07-29 11:32:54 -0700165 private void updateLastRecovered(long time) {
George Mount3636d0a2011-11-21 09:08:21 -0800166 BrowserSettings browserSettings = BrowserSettings.getInstance();
167 browserSettings.setLastRecovered(time);
John Reckcfeae6d2011-06-24 15:33:57 -0700168 }
169
George Mount3636d0a2011-11-21 09:08:21 -0800170 synchronized private Bundle loadCrashState() {
John Reck023124e2011-07-29 11:32:54 -0700171 if (!shouldRestore()) {
172 return null;
173 }
George Mount3636d0a2011-11-21 09:08:21 -0800174 BrowserSettings browserSettings = BrowserSettings.getInstance();
175 browserSettings.setLastRunPaused(false);
John Reckcfeae6d2011-06-24 15:33:57 -0700176 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700177 Parcel parcel = Parcel.obtain();
Ben Murdoch679da252011-07-25 17:59:36 +0100178 FileInputStream fin = null;
John Reck847b5322011-04-14 17:02:18 -0700179 try {
John Reckbc490d22011-07-22 15:04:59 -0700180 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
Ben Murdoch679da252011-07-25 17:59:36 +0100181 fin = new FileInputStream(stateFile);
John Reck847b5322011-04-14 17:02:18 -0700182 ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
183 byte[] buffer = new byte[BUFFER_SIZE];
184 int read;
185 while ((read = fin.read(buffer)) > 0) {
186 dataStream.write(buffer, 0, read);
187 }
188 byte[] data = dataStream.toByteArray();
189 parcel.unmarshall(data, 0, data.length);
190 parcel.setDataPosition(0);
John Reckcfeae6d2011-06-24 15:33:57 -0700191 state = parcel.readBundle();
John Recke659d7b2011-10-13 15:51:02 -0700192 if (state != null && !state.isEmpty()) {
193 return state;
194 }
John Reck847b5322011-04-14 17:02:18 -0700195 } catch (FileNotFoundException e) {
196 // No state to recover
John Reckbc490d22011-07-22 15:04:59 -0700197 } catch (Throwable e) {
John Reck847b5322011-04-14 17:02:18 -0700198 Log.w(LOGTAG, "Failed to recover state!", e);
John Reck847b5322011-04-14 17:02:18 -0700199 } finally {
200 parcel.recycle();
Ben Murdoch679da252011-07-25 17:59:36 +0100201 if (fin != null) {
202 try {
203 fin.close();
204 } catch (IOException e) { }
205 }
John Reck847b5322011-04-14 17:02:18 -0700206 }
John Reck023124e2011-07-29 11:32:54 -0700207 return null;
John Reck847b5322011-04-14 17:02:18 -0700208 }
John Reckbc490d22011-07-22 15:04:59 -0700209
210 public void startRecovery(Intent intent) {
211 synchronized (CrashRecoveryHandler.this) {
212 while (mIsPreloading) {
213 try {
214 CrashRecoveryHandler.this.wait();
215 } catch (InterruptedException e) {}
216 }
217 }
218 if (!mDidPreload) {
219 mRecoveryState = loadCrashState();
John Reckbc490d22011-07-22 15:04:59 -0700220 }
John Reck023124e2011-07-29 11:32:54 -0700221 updateLastRecovered(mRecoveryState != null
222 ? System.currentTimeMillis() : 0);
George Mount3636d0a2011-11-21 09:08:21 -0800223 mController.doStart(mRecoveryState, intent);
John Reckbc490d22011-07-22 15:04:59 -0700224 mRecoveryState = null;
225 }
226
227 public void preloadCrashState() {
228 synchronized (CrashRecoveryHandler.this) {
229 if (mIsPreloading) {
230 return;
231 }
232 mIsPreloading = true;
233 }
234 mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE);
235 }
236
George Mount3636d0a2011-11-21 09:08:21 -0800237 /**
238 * Writes the crash recovery state to a file synchronously.
239 * Errors are swallowed, but logged.
240 * @param state The state to write out
241 */
242 synchronized void writeState(Bundle state) {
243 if (LOGV_ENABLED) {
244 Log.v(LOGTAG, "Saving crash recovery state");
245 }
246 Parcel p = Parcel.obtain();
247 try {
248 state.writeToParcel(p, 0);
249 File stateJournal = new File(mContext.getCacheDir(),
250 STATE_FILE + ".journal");
251 FileOutputStream fout = new FileOutputStream(stateJournal);
252 fout.write(p.marshall());
253 fout.close();
254 File stateFile = new File(mContext.getCacheDir(),
255 STATE_FILE);
256 if (!stateJournal.renameTo(stateFile)) {
257 // Failed to rename, try deleting the existing
258 // file and try again
259 stateFile.delete();
260 stateJournal.renameTo(stateFile);
261 }
262 } catch (Throwable e) {
263 Log.i(LOGTAG, "Failed to save persistent state", e);
264 } finally {
265 p.recycle();
266 }
267 }
kaiyiza8b6dbb2013-07-29 18:11:22 +0800268}