blob: 8cb5929ef03393941d60eb8e182caba771afcba9 [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
37 private static final String LOGTAG = "BrowserCrashRecovery";
38 private static final String STATE_FILE = "browser_state.parcel";
John Reckcfeae6d2011-06-24 15:33:57 -070039 private static final String RECOVERY_PREFERENCES = "browser_recovery_prefs";
40 private static final String KEY_LAST_RECOVERED = "last_recovered";
John Reck847b5322011-04-14 17:02:18 -070041 private static final int BUFFER_SIZE = 4096;
John Reck378a4102011-06-09 16:23:01 -070042 private static final long BACKUP_DELAY = 500; // 500ms between writes
John Reckcfeae6d2011-06-24 15:33:57 -070043 /* This is the duration for which we will prompt to restore
44 * instead of automatically restoring. The first time the browser crashes,
45 * we will automatically restore. If we then crash again within XX minutes,
46 * we will prompt instead of automatically restoring.
47 */
John Reck023124e2011-07-29 11:32:54 -070048 private static final long PROMPT_INTERVAL = 5 * 60 * 1000; // 5 minutes
John Reck378a4102011-06-09 16:23:01 -070049
John Reckbc490d22011-07-22 15:04:59 -070050 private static final int MSG_WRITE_STATE = 1;
51 private static final int MSG_CLEAR_STATE = 2;
52 private static final int MSG_PRELOAD_STATE = 3;
53
John Reck378a4102011-06-09 16:23:01 -070054 private static CrashRecoveryHandler sInstance;
John Reck847b5322011-04-14 17:02:18 -070055
56 private Controller mController;
John Reckbc490d22011-07-22 15:04:59 -070057 private Context mContext;
John Reck378a4102011-06-09 16:23:01 -070058 private Handler mForegroundHandler;
59 private Handler mBackgroundHandler;
John Reckbc490d22011-07-22 15:04:59 -070060 private boolean mIsPreloading = false;
61 private boolean mDidPreload = false;
John Reckbc490d22011-07-22 15:04:59 -070062 private Bundle mRecoveryState = null;
John Reck847b5322011-04-14 17:02:18 -070063
John Reck378a4102011-06-09 16:23:01 -070064 public static CrashRecoveryHandler initialize(Controller controller) {
65 if (sInstance == null) {
66 sInstance = new CrashRecoveryHandler(controller);
67 } else {
68 sInstance.mController = controller;
69 }
70 return sInstance;
71 }
72
73 public static CrashRecoveryHandler getInstance() {
74 return sInstance;
75 }
76
77 private CrashRecoveryHandler(Controller controller) {
John Reck847b5322011-04-14 17:02:18 -070078 mController = controller;
John Reckbc490d22011-07-22 15:04:59 -070079 mContext = mController.getActivity().getApplicationContext();
John Reck378a4102011-06-09 16:23:01 -070080 mForegroundHandler = new Handler();
John Reckcadae722011-07-25 11:36:17 -070081 mBackgroundHandler = new Handler(BackgroundHandler.getLooper()) {
John Reckbc490d22011-07-22 15:04:59 -070082
83 @Override
84 public void handleMessage(Message msg) {
85 switch (msg.what) {
86 case MSG_WRITE_STATE:
87 Parcel p = Parcel.obtain();
88 try {
89 Bundle state = (Bundle) msg.obj;
90 state.writeToParcel(p, 0);
91 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
92 FileOutputStream fout = new FileOutputStream(stateFile);
93 fout.write(p.marshall());
94 fout.close();
95 } catch (Throwable e) {
96 Log.i(LOGTAG, "Failed to save persistent state", e);
97 } finally {
98 p.recycle();
99 }
100 break;
101 case MSG_CLEAR_STATE:
102 File state = new File(mContext.getCacheDir(), STATE_FILE);
103 if (state.exists()) {
104 state.delete();
105 }
106 break;
107 case MSG_PRELOAD_STATE:
108 mRecoveryState = loadCrashState();
John Reckbc490d22011-07-22 15:04:59 -0700109 synchronized (CrashRecoveryHandler.this) {
110 mIsPreloading = false;
111 mDidPreload = true;
112 CrashRecoveryHandler.this.notifyAll();
113 }
114 break;
115 }
116 }
117 };
John Reck847b5322011-04-14 17:02:18 -0700118 }
119
120 public void backupState() {
John Reck378a4102011-06-09 16:23:01 -0700121 mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY);
John Reck847b5322011-04-14 17:02:18 -0700122 }
123
John Reck378a4102011-06-09 16:23:01 -0700124 private Runnable mCreateState = new Runnable() {
125
126 @Override
127 public void run() {
128 try {
129 final Bundle state = new Bundle();
John Reck1cf4b792011-07-26 10:22:22 -0700130 mController.onSaveInstanceState(state);
John Reckbc490d22011-07-22 15:04:59 -0700131 Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state)
132 .sendToTarget();
John Reck378a4102011-06-09 16:23:01 -0700133 // Remove any queued up saves
134 mForegroundHandler.removeCallbacks(mCreateState);
135 } catch (Throwable t) {
136 Log.w(LOGTAG, "Failed to save state", t);
137 return;
138 }
139 }
140
141 };
142
John Reckbc490d22011-07-22 15:04:59 -0700143 public void clearState() {
144 mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE);
John Reck023124e2011-07-29 11:32:54 -0700145 updateLastRecovered(0);
John Reck847b5322011-04-14 17:02:18 -0700146 }
147
John Reck023124e2011-07-29 11:32:54 -0700148 private boolean shouldRestore() {
John Reckbc490d22011-07-22 15:04:59 -0700149 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700150 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
John Reckbc490d22011-07-22 15:04:59 -0700151 long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, 0);
John Reckcfeae6d2011-06-24 15:33:57 -0700152 long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered;
153 if (timeSinceLastRecover > PROMPT_INTERVAL) {
John Reck023124e2011-07-29 11:32:54 -0700154 return true;
John Reckcfeae6d2011-06-24 15:33:57 -0700155 }
John Reck023124e2011-07-29 11:32:54 -0700156 return false;
John Reckcfeae6d2011-06-24 15:33:57 -0700157 }
158
John Reck023124e2011-07-29 11:32:54 -0700159 private void updateLastRecovered(long time) {
John Reckbc490d22011-07-22 15:04:59 -0700160 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700161 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
162 prefs.edit()
John Reck023124e2011-07-29 11:32:54 -0700163 .putLong(KEY_LAST_RECOVERED, time)
Ben Murdochfb72a9a2011-07-25 18:18:32 +0100164 .apply();
John Reckcfeae6d2011-06-24 15:33:57 -0700165 }
166
John Reckbc490d22011-07-22 15:04:59 -0700167 private Bundle loadCrashState() {
John Reck023124e2011-07-29 11:32:54 -0700168 if (!shouldRestore()) {
169 return null;
170 }
John Reckcfeae6d2011-06-24 15:33:57 -0700171 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700172 Parcel parcel = Parcel.obtain();
Ben Murdoch679da252011-07-25 17:59:36 +0100173 FileInputStream fin = null;
John Reck847b5322011-04-14 17:02:18 -0700174 try {
John Reckbc490d22011-07-22 15:04:59 -0700175 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
Ben Murdoch679da252011-07-25 17:59:36 +0100176 fin = new FileInputStream(stateFile);
John Reck847b5322011-04-14 17:02:18 -0700177 ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
178 byte[] buffer = new byte[BUFFER_SIZE];
179 int read;
180 while ((read = fin.read(buffer)) > 0) {
181 dataStream.write(buffer, 0, read);
182 }
183 byte[] data = dataStream.toByteArray();
184 parcel.unmarshall(data, 0, data.length);
185 parcel.setDataPosition(0);
John Reckcfeae6d2011-06-24 15:33:57 -0700186 state = parcel.readBundle();
John Reck847b5322011-04-14 17:02:18 -0700187 } catch (FileNotFoundException e) {
188 // No state to recover
John Reckcfeae6d2011-06-24 15:33:57 -0700189 state = null;
John Reckbc490d22011-07-22 15:04:59 -0700190 } catch (Throwable e) {
John Reck847b5322011-04-14 17:02:18 -0700191 Log.w(LOGTAG, "Failed to recover state!", e);
John Reckcfeae6d2011-06-24 15:33:57 -0700192 state = null;
John Reck847b5322011-04-14 17:02:18 -0700193 } finally {
194 parcel.recycle();
Ben Murdoch679da252011-07-25 17:59:36 +0100195 if (fin != null) {
196 try {
197 fin.close();
198 } catch (IOException e) { }
199 }
John Reck847b5322011-04-14 17:02:18 -0700200 }
John Reck023124e2011-07-29 11:32:54 -0700201 if (state != null && !state.isEmpty()) {
202 return state;
203 }
204 return null;
John Reck847b5322011-04-14 17:02:18 -0700205 }
John Reckbc490d22011-07-22 15:04:59 -0700206
207 public void startRecovery(Intent intent) {
208 synchronized (CrashRecoveryHandler.this) {
209 while (mIsPreloading) {
210 try {
211 CrashRecoveryHandler.this.wait();
212 } catch (InterruptedException e) {}
213 }
214 }
215 if (!mDidPreload) {
216 mRecoveryState = loadCrashState();
John Reckbc490d22011-07-22 15:04:59 -0700217 }
John Reck023124e2011-07-29 11:32:54 -0700218 updateLastRecovered(mRecoveryState != null
219 ? System.currentTimeMillis() : 0);
John Reckbc490d22011-07-22 15:04:59 -0700220 mController.doStart(mRecoveryState, intent);
221 mRecoveryState = null;
222 }
223
224 public void preloadCrashState() {
225 synchronized (CrashRecoveryHandler.this) {
226 if (mIsPreloading) {
227 return;
228 }
229 mIsPreloading = true;
230 }
231 mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE);
232 }
233
John Reck847b5322011-04-14 17:02:18 -0700234}