blob: c2fbbd2f4e6597985803e0f3e8f0e4d7600d8e7f [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";
John Reckcfeae6d2011-06-24 15:33:57 -070040 private static final String RECOVERY_PREFERENCES = "browser_recovery_prefs";
41 private static final String KEY_LAST_RECOVERED = "last_recovered";
John Reck847b5322011-04-14 17:02:18 -070042 private static final int BUFFER_SIZE = 4096;
John Reck378a4102011-06-09 16:23:01 -070043 private static final long BACKUP_DELAY = 500; // 500ms between writes
John Reckcfeae6d2011-06-24 15:33:57 -070044 /* This is the duration for which we will prompt to restore
45 * instead of automatically restoring. The first time the browser crashes,
46 * we will automatically restore. If we then crash again within XX minutes,
47 * we will prompt instead of automatically restoring.
48 */
John Reck023124e2011-07-29 11:32:54 -070049 private static final long PROMPT_INTERVAL = 5 * 60 * 1000; // 5 minutes
John Reck378a4102011-06-09 16:23:01 -070050
John Reckbc490d22011-07-22 15:04:59 -070051 private static final int MSG_WRITE_STATE = 1;
52 private static final int MSG_CLEAR_STATE = 2;
53 private static final int MSG_PRELOAD_STATE = 3;
54
John Reck378a4102011-06-09 16:23:01 -070055 private static CrashRecoveryHandler sInstance;
John Reck847b5322011-04-14 17:02:18 -070056
57 private Controller mController;
John Reckbc490d22011-07-22 15:04:59 -070058 private Context mContext;
John Reck378a4102011-06-09 16:23:01 -070059 private Handler mForegroundHandler;
60 private Handler mBackgroundHandler;
John Reckbc490d22011-07-22 15:04:59 -070061 private boolean mIsPreloading = false;
62 private boolean mDidPreload = false;
John Reckbc490d22011-07-22 15:04:59 -070063 private Bundle mRecoveryState = null;
John Reck847b5322011-04-14 17:02:18 -070064
John Reck378a4102011-06-09 16:23:01 -070065 public static CrashRecoveryHandler initialize(Controller controller) {
66 if (sInstance == null) {
67 sInstance = new CrashRecoveryHandler(controller);
68 } else {
69 sInstance.mController = controller;
70 }
71 return sInstance;
72 }
73
74 public static CrashRecoveryHandler getInstance() {
75 return sInstance;
76 }
77
78 private CrashRecoveryHandler(Controller controller) {
John Reck847b5322011-04-14 17:02:18 -070079 mController = controller;
John Reckbc490d22011-07-22 15:04:59 -070080 mContext = mController.getActivity().getApplicationContext();
John Reck378a4102011-06-09 16:23:01 -070081 mForegroundHandler = new Handler();
John Reckcadae722011-07-25 11:36:17 -070082 mBackgroundHandler = new Handler(BackgroundHandler.getLooper()) {
John Reckbc490d22011-07-22 15:04:59 -070083
84 @Override
85 public void handleMessage(Message msg) {
86 switch (msg.what) {
87 case MSG_WRITE_STATE:
John Reck6c2e2f32011-08-22 13:41:23 -070088 if (LOGV_ENABLED) {
89 Log.v(LOGTAG, "Saving crash recovery state");
90 }
John Reckbc490d22011-07-22 15:04:59 -070091 Parcel p = Parcel.obtain();
92 try {
93 Bundle state = (Bundle) msg.obj;
94 state.writeToParcel(p, 0);
John Reck6c2e2f32011-08-22 13:41:23 -070095 File stateJournal = new File(mContext.getCacheDir(),
96 STATE_FILE + ".journal");
97 FileOutputStream fout = new FileOutputStream(stateJournal);
John Reckbc490d22011-07-22 15:04:59 -070098 fout.write(p.marshall());
99 fout.close();
John Reck6c2e2f32011-08-22 13:41:23 -0700100 File stateFile = new File(mContext.getCacheDir(),
101 STATE_FILE);
102 if (!stateJournal.renameTo(stateFile)) {
103 // Failed to rename, try deleting the existing
104 // file and try again
105 stateFile.delete();
106 stateJournal.renameTo(stateFile);
107 }
John Reckbc490d22011-07-22 15:04:59 -0700108 } catch (Throwable e) {
109 Log.i(LOGTAG, "Failed to save persistent state", e);
110 } finally {
111 p.recycle();
112 }
113 break;
114 case MSG_CLEAR_STATE:
John Reck6c2e2f32011-08-22 13:41:23 -0700115 if (LOGV_ENABLED) {
116 Log.v(LOGTAG, "Clearing crash recovery state");
117 }
John Reckbc490d22011-07-22 15:04:59 -0700118 File state = new File(mContext.getCacheDir(), STATE_FILE);
119 if (state.exists()) {
120 state.delete();
121 }
122 break;
123 case MSG_PRELOAD_STATE:
124 mRecoveryState = loadCrashState();
John Reckbc490d22011-07-22 15:04:59 -0700125 synchronized (CrashRecoveryHandler.this) {
126 mIsPreloading = false;
127 mDidPreload = true;
128 CrashRecoveryHandler.this.notifyAll();
129 }
130 break;
131 }
132 }
133 };
John Reck847b5322011-04-14 17:02:18 -0700134 }
135
136 public void backupState() {
John Reck378a4102011-06-09 16:23:01 -0700137 mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY);
John Reck847b5322011-04-14 17:02:18 -0700138 }
139
John Reck378a4102011-06-09 16:23:01 -0700140 private Runnable mCreateState = new Runnable() {
141
142 @Override
143 public void run() {
144 try {
145 final Bundle state = new Bundle();
John Reck1cf4b792011-07-26 10:22:22 -0700146 mController.onSaveInstanceState(state);
John Reckbc490d22011-07-22 15:04:59 -0700147 Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state)
148 .sendToTarget();
John Reck378a4102011-06-09 16:23:01 -0700149 // Remove any queued up saves
150 mForegroundHandler.removeCallbacks(mCreateState);
151 } catch (Throwable t) {
152 Log.w(LOGTAG, "Failed to save state", t);
153 return;
154 }
155 }
156
157 };
158
John Reckbc490d22011-07-22 15:04:59 -0700159 public void clearState() {
160 mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE);
John Reck023124e2011-07-29 11:32:54 -0700161 updateLastRecovered(0);
John Reck847b5322011-04-14 17:02:18 -0700162 }
163
John Reck023124e2011-07-29 11:32:54 -0700164 private boolean shouldRestore() {
John Reckbc490d22011-07-22 15:04:59 -0700165 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700166 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
John Reckbc490d22011-07-22 15:04:59 -0700167 long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, 0);
John Reckcfeae6d2011-06-24 15:33:57 -0700168 long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered;
169 if (timeSinceLastRecover > PROMPT_INTERVAL) {
John Reck023124e2011-07-29 11:32:54 -0700170 return true;
John Reckcfeae6d2011-06-24 15:33:57 -0700171 }
John Reck023124e2011-07-29 11:32:54 -0700172 return false;
John Reckcfeae6d2011-06-24 15:33:57 -0700173 }
174
John Reck023124e2011-07-29 11:32:54 -0700175 private void updateLastRecovered(long time) {
John Reckbc490d22011-07-22 15:04:59 -0700176 SharedPreferences prefs = mContext.getSharedPreferences(
John Reckcfeae6d2011-06-24 15:33:57 -0700177 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
178 prefs.edit()
John Reck023124e2011-07-29 11:32:54 -0700179 .putLong(KEY_LAST_RECOVERED, time)
Ben Murdochfb72a9a2011-07-25 18:18:32 +0100180 .apply();
John Reckcfeae6d2011-06-24 15:33:57 -0700181 }
182
John Reckbc490d22011-07-22 15:04:59 -0700183 private Bundle loadCrashState() {
John Reck023124e2011-07-29 11:32:54 -0700184 if (!shouldRestore()) {
185 return null;
186 }
John Reckcfeae6d2011-06-24 15:33:57 -0700187 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700188 Parcel parcel = Parcel.obtain();
Ben Murdoch679da252011-07-25 17:59:36 +0100189 FileInputStream fin = null;
John Reck847b5322011-04-14 17:02:18 -0700190 try {
John Reckbc490d22011-07-22 15:04:59 -0700191 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
Ben Murdoch679da252011-07-25 17:59:36 +0100192 fin = new FileInputStream(stateFile);
John Reck847b5322011-04-14 17:02:18 -0700193 ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
194 byte[] buffer = new byte[BUFFER_SIZE];
195 int read;
196 while ((read = fin.read(buffer)) > 0) {
197 dataStream.write(buffer, 0, read);
198 }
199 byte[] data = dataStream.toByteArray();
200 parcel.unmarshall(data, 0, data.length);
201 parcel.setDataPosition(0);
John Reckcfeae6d2011-06-24 15:33:57 -0700202 state = parcel.readBundle();
John Reck847b5322011-04-14 17:02:18 -0700203 } catch (FileNotFoundException e) {
204 // No state to recover
John Reckcfeae6d2011-06-24 15:33:57 -0700205 state = null;
John Reckbc490d22011-07-22 15:04:59 -0700206 } catch (Throwable e) {
John Reck847b5322011-04-14 17:02:18 -0700207 Log.w(LOGTAG, "Failed to recover state!", e);
John Reckcfeae6d2011-06-24 15:33:57 -0700208 state = null;
John Reck847b5322011-04-14 17:02:18 -0700209 } finally {
210 parcel.recycle();
Ben Murdoch679da252011-07-25 17:59:36 +0100211 if (fin != null) {
212 try {
213 fin.close();
214 } catch (IOException e) { }
215 }
John Reck847b5322011-04-14 17:02:18 -0700216 }
John Reck023124e2011-07-29 11:32:54 -0700217 if (state != null && !state.isEmpty()) {
218 return state;
219 }
220 return null;
John Reck847b5322011-04-14 17:02:18 -0700221 }
John Reckbc490d22011-07-22 15:04:59 -0700222
223 public void startRecovery(Intent intent) {
224 synchronized (CrashRecoveryHandler.this) {
225 while (mIsPreloading) {
226 try {
227 CrashRecoveryHandler.this.wait();
228 } catch (InterruptedException e) {}
229 }
230 }
231 if (!mDidPreload) {
232 mRecoveryState = loadCrashState();
John Reckbc490d22011-07-22 15:04:59 -0700233 }
John Reck023124e2011-07-29 11:32:54 -0700234 updateLastRecovered(mRecoveryState != null
235 ? System.currentTimeMillis() : 0);
John Reckbc490d22011-07-22 15:04:59 -0700236 mController.doStart(mRecoveryState, intent);
237 mRecoveryState = null;
238 }
239
240 public void preloadCrashState() {
241 synchronized (CrashRecoveryHandler.this) {
242 if (mIsPreloading) {
243 return;
244 }
245 mIsPreloading = true;
246 }
247 mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE);
248 }
249
John Reck847b5322011-04-14 17:02:18 -0700250}