John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 19 | import android.content.Context; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 21 | import android.content.SharedPreferences; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 22 | import android.os.Bundle; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 23 | import android.os.Handler; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 24 | import android.os.Message; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 25 | import android.os.Parcel; |
| 26 | import android.util.Log; |
| 27 | |
| 28 | import java.io.ByteArrayOutputStream; |
John Reck | a481653 | 2011-06-29 14:41:59 -0700 | [diff] [blame] | 29 | import java.io.File; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 30 | import java.io.FileInputStream; |
| 31 | import java.io.FileNotFoundException; |
| 32 | import java.io.FileOutputStream; |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 33 | import java.io.IOException; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 34 | |
| 35 | public class CrashRecoveryHandler { |
| 36 | |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 37 | private static final boolean LOGV_ENABLED = Browser.LOGV_ENABLED; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 38 | 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 Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 41 | private static final long BACKUP_DELAY = 500; // 500ms between writes |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 42 | /* 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 Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 47 | private static final long PROMPT_INTERVAL = 5 * 60 * 1000; // 5 minutes |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 48 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 49 | 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 Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 53 | private static CrashRecoveryHandler sInstance; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 54 | |
| 55 | private Controller mController; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 56 | private Context mContext; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 57 | private Handler mForegroundHandler; |
| 58 | private Handler mBackgroundHandler; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 59 | private boolean mIsPreloading = false; |
| 60 | private boolean mDidPreload = false; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 61 | private Bundle mRecoveryState = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 62 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 63 | 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 Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 77 | mController = controller; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 78 | mContext = mController.getActivity().getApplicationContext(); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 79 | mForegroundHandler = new Handler(); |
John Reck | cadae72 | 2011-07-25 11:36:17 -0700 | [diff] [blame] | 80 | mBackgroundHandler = new Handler(BackgroundHandler.getLooper()) { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 81 | |
| 82 | @Override |
| 83 | public void handleMessage(Message msg) { |
| 84 | switch (msg.what) { |
| 85 | case MSG_WRITE_STATE: |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 86 | Bundle saveState = (Bundle) msg.obj; |
| 87 | writeState(saveState); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 88 | break; |
| 89 | case MSG_CLEAR_STATE: |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 90 | if (LOGV_ENABLED) { |
| 91 | Log.v(LOGTAG, "Clearing crash recovery state"); |
| 92 | } |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 93 | 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 Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 100 | synchronized (CrashRecoveryHandler.this) { |
| 101 | mIsPreloading = false; |
| 102 | mDidPreload = true; |
| 103 | CrashRecoveryHandler.this.notifyAll(); |
| 104 | } |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | }; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | public void backupState() { |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 112 | mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 113 | } |
| 114 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 115 | private Runnable mCreateState = new Runnable() { |
| 116 | |
| 117 | @Override |
| 118 | public void run() { |
| 119 | try { |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 120 | final Bundle state = mController.createSaveState(); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 121 | Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state) |
| 122 | .sendToTarget(); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 123 | // 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 Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 133 | public void clearState() { |
| 134 | mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE); |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 135 | updateLastRecovered(0); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 136 | } |
| 137 | |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 138 | private boolean shouldRestore() { |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 139 | BrowserSettings browserSettings = BrowserSettings.getInstance(); |
| 140 | long lastRecovered = browserSettings.getLastRecovered(); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 141 | long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered; |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 142 | return (timeSinceLastRecover > PROMPT_INTERVAL) |
| 143 | || browserSettings.wasLastRunPaused(); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 144 | } |
| 145 | |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 146 | private void updateLastRecovered(long time) { |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 147 | BrowserSettings browserSettings = BrowserSettings.getInstance(); |
| 148 | browserSettings.setLastRecovered(time); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 149 | } |
| 150 | |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 151 | synchronized private Bundle loadCrashState() { |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 152 | if (!shouldRestore()) { |
| 153 | return null; |
| 154 | } |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 155 | BrowserSettings browserSettings = BrowserSettings.getInstance(); |
| 156 | browserSettings.setLastRunPaused(false); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 157 | Bundle state = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 158 | Parcel parcel = Parcel.obtain(); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 159 | FileInputStream fin = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 160 | try { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 161 | File stateFile = new File(mContext.getCacheDir(), STATE_FILE); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 162 | fin = new FileInputStream(stateFile); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 163 | 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 Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 172 | state = parcel.readBundle(); |
John Reck | e659d7b | 2011-10-13 15:51:02 -0700 | [diff] [blame] | 173 | if (state != null && !state.isEmpty()) { |
| 174 | return state; |
| 175 | } |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 176 | } catch (FileNotFoundException e) { |
| 177 | // No state to recover |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 178 | } catch (Throwable e) { |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 179 | Log.w(LOGTAG, "Failed to recover state!", e); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 180 | } finally { |
| 181 | parcel.recycle(); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 182 | if (fin != null) { |
| 183 | try { |
| 184 | fin.close(); |
| 185 | } catch (IOException e) { } |
| 186 | } |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 187 | } |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 188 | return null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 189 | } |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 190 | |
| 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 Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 201 | } |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 202 | updateLastRecovered(mRecoveryState != null |
| 203 | ? System.currentTimeMillis() : 0); |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 204 | mController.doStart(mRecoveryState, intent); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 205 | 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 Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 218 | /** |
| 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 | } |