blob: c02eb075658909b91acf62e28703e2636e013121 [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
John Reck847b5322011-04-14 17:02:18 -070018
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() {
Vivek Sekhar2ce13da2015-12-15 11:23:29 -0800112 // Only write if we are not already destroyed
113 if (mController.getTabControl() != null)
114 mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY);
John Reck847b5322011-04-14 17:02:18 -0700115 }
116
John Reck378a4102011-06-09 16:23:01 -0700117 private Runnable mCreateState = new Runnable() {
118
119 @Override
120 public void run() {
121 try {
George Mount3636d0a2011-11-21 09:08:21 -0800122 final Bundle state = mController.createSaveState();
Vivek Sekhar2ce13da2015-12-15 11:23:29 -0800123 // block write of null values
124 if (state != null && mController.getTabControl() != null) {
125 Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state)
126 .sendToTarget();
127 }
John Reck378a4102011-06-09 16:23:01 -0700128 // Remove any queued up saves
129 mForegroundHandler.removeCallbacks(mCreateState);
130 } catch (Throwable t) {
131 Log.w(LOGTAG, "Failed to save state", t);
132 return;
133 }
134 }
135
136 };
137
John Reckbc490d22011-07-22 15:04:59 -0700138 public void clearState() {
kaiyiza8b6dbb2013-07-29 18:11:22 +0800139 clearState(false);
140 }
141
142 /**
143 * Clear cached state files.
144 *
145 * @param block If block, clear state files in the caller thread, otherwise
146 * do it in a worker thread.
147 */
148 void clearState(boolean block) {
149 if (block) {
150 if (mContext != null) {
151 File state = new File(mContext.getCacheDir(), STATE_FILE);
152 if (state.exists()) {
153 state.delete();
154 }
155 }
156 } else {
157 mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE);
158 }
John Reck023124e2011-07-29 11:32:54 -0700159 updateLastRecovered(0);
John Reck847b5322011-04-14 17:02:18 -0700160 }
161
John Reck023124e2011-07-29 11:32:54 -0700162 private boolean shouldRestore() {
George Mount3636d0a2011-11-21 09:08:21 -0800163 BrowserSettings browserSettings = BrowserSettings.getInstance();
164 long lastRecovered = browserSettings.getLastRecovered();
John Reckcfeae6d2011-06-24 15:33:57 -0700165 long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered;
George Mount3636d0a2011-11-21 09:08:21 -0800166 return (timeSinceLastRecover > PROMPT_INTERVAL)
167 || browserSettings.wasLastRunPaused();
John Reckcfeae6d2011-06-24 15:33:57 -0700168 }
169
John Reck023124e2011-07-29 11:32:54 -0700170 private void updateLastRecovered(long time) {
George Mount3636d0a2011-11-21 09:08:21 -0800171 BrowserSettings browserSettings = BrowserSettings.getInstance();
172 browserSettings.setLastRecovered(time);
John Reckcfeae6d2011-06-24 15:33:57 -0700173 }
174
George Mount3636d0a2011-11-21 09:08:21 -0800175 synchronized private Bundle loadCrashState() {
John Reck023124e2011-07-29 11:32:54 -0700176 if (!shouldRestore()) {
177 return null;
178 }
George Mount3636d0a2011-11-21 09:08:21 -0800179 BrowserSettings browserSettings = BrowserSettings.getInstance();
180 browserSettings.setLastRunPaused(false);
John Reckcfeae6d2011-06-24 15:33:57 -0700181 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700182 Parcel parcel = Parcel.obtain();
Ben Murdoch679da252011-07-25 17:59:36 +0100183 FileInputStream fin = null;
John Reck847b5322011-04-14 17:02:18 -0700184 try {
John Reckbc490d22011-07-22 15:04:59 -0700185 File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
Ben Murdoch679da252011-07-25 17:59:36 +0100186 fin = new FileInputStream(stateFile);
John Reck847b5322011-04-14 17:02:18 -0700187 ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
188 byte[] buffer = new byte[BUFFER_SIZE];
189 int read;
190 while ((read = fin.read(buffer)) > 0) {
191 dataStream.write(buffer, 0, read);
192 }
193 byte[] data = dataStream.toByteArray();
194 parcel.unmarshall(data, 0, data.length);
195 parcel.setDataPosition(0);
John Reckcfeae6d2011-06-24 15:33:57 -0700196 state = parcel.readBundle();
John Recke659d7b2011-10-13 15:51:02 -0700197 if (state != null && !state.isEmpty()) {
198 return state;
199 }
John Reck847b5322011-04-14 17:02:18 -0700200 } catch (FileNotFoundException e) {
201 // No state to recover
John Reckbc490d22011-07-22 15:04:59 -0700202 } catch (Throwable e) {
John Reck847b5322011-04-14 17:02:18 -0700203 Log.w(LOGTAG, "Failed to recover state!", e);
John Reck847b5322011-04-14 17:02:18 -0700204 } finally {
205 parcel.recycle();
Ben Murdoch679da252011-07-25 17:59:36 +0100206 if (fin != null) {
207 try {
208 fin.close();
209 } catch (IOException e) { }
210 }
John Reck847b5322011-04-14 17:02:18 -0700211 }
John Reck023124e2011-07-29 11:32:54 -0700212 return null;
John Reck847b5322011-04-14 17:02:18 -0700213 }
John Reckbc490d22011-07-22 15:04:59 -0700214
215 public void startRecovery(Intent intent) {
216 synchronized (CrashRecoveryHandler.this) {
217 while (mIsPreloading) {
218 try {
219 CrashRecoveryHandler.this.wait();
220 } catch (InterruptedException e) {}
221 }
222 }
223 if (!mDidPreload) {
224 mRecoveryState = loadCrashState();
John Reckbc490d22011-07-22 15:04:59 -0700225 }
John Reck023124e2011-07-29 11:32:54 -0700226 updateLastRecovered(mRecoveryState != null
227 ? System.currentTimeMillis() : 0);
George Mount3636d0a2011-11-21 09:08:21 -0800228 mController.doStart(mRecoveryState, intent);
John Reckbc490d22011-07-22 15:04:59 -0700229 mRecoveryState = null;
230 }
231
232 public void preloadCrashState() {
233 synchronized (CrashRecoveryHandler.this) {
234 if (mIsPreloading) {
235 return;
236 }
237 mIsPreloading = true;
238 }
239 mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE);
240 }
241
George Mount3636d0a2011-11-21 09:08:21 -0800242 /**
243 * Writes the crash recovery state to a file synchronously.
244 * Errors are swallowed, but logged.
245 * @param state The state to write out
246 */
247 synchronized void writeState(Bundle state) {
248 if (LOGV_ENABLED) {
249 Log.v(LOGTAG, "Saving crash recovery state");
250 }
251 Parcel p = Parcel.obtain();
252 try {
253 state.writeToParcel(p, 0);
254 File stateJournal = new File(mContext.getCacheDir(),
255 STATE_FILE + ".journal");
256 FileOutputStream fout = new FileOutputStream(stateJournal);
257 fout.write(p.marshall());
258 fout.close();
259 File stateFile = new File(mContext.getCacheDir(),
260 STATE_FILE);
261 if (!stateJournal.renameTo(stateFile)) {
262 // Failed to rename, try deleting the existing
263 // file and try again
264 stateFile.delete();
265 stateJournal.renameTo(stateFile);
266 }
267 } catch (Throwable e) {
268 Log.i(LOGTAG, "Failed to save persistent state", e);
269 } finally {
270 p.recycle();
271 }
272 }
kaiyiza8b6dbb2013-07-29 18:11:22 +0800273}