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"; |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 40 | private static final String RECOVERY_PREFERENCES = "browser_recovery_prefs"; |
| 41 | private static final String KEY_LAST_RECOVERED = "last_recovered"; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 42 | private static final int BUFFER_SIZE = 4096; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 43 | private static final long BACKUP_DELAY = 500; // 500ms between writes |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 44 | /* 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 Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 49 | private static final long PROMPT_INTERVAL = 5 * 60 * 1000; // 5 minutes |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 50 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 51 | 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 Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 55 | private static CrashRecoveryHandler sInstance; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 56 | |
| 57 | private Controller mController; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 58 | private Context mContext; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 59 | private Handler mForegroundHandler; |
| 60 | private Handler mBackgroundHandler; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 61 | private boolean mIsPreloading = false; |
| 62 | private boolean mDidPreload = false; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 63 | private Bundle mRecoveryState = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 64 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 65 | 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 Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 79 | mController = controller; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 80 | mContext = mController.getActivity().getApplicationContext(); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 81 | mForegroundHandler = new Handler(); |
John Reck | cadae72 | 2011-07-25 11:36:17 -0700 | [diff] [blame] | 82 | mBackgroundHandler = new Handler(BackgroundHandler.getLooper()) { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 83 | |
| 84 | @Override |
| 85 | public void handleMessage(Message msg) { |
| 86 | switch (msg.what) { |
| 87 | case MSG_WRITE_STATE: |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 88 | if (LOGV_ENABLED) { |
| 89 | Log.v(LOGTAG, "Saving crash recovery state"); |
| 90 | } |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 91 | Parcel p = Parcel.obtain(); |
| 92 | try { |
| 93 | Bundle state = (Bundle) msg.obj; |
| 94 | state.writeToParcel(p, 0); |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 95 | File stateJournal = new File(mContext.getCacheDir(), |
| 96 | STATE_FILE + ".journal"); |
| 97 | FileOutputStream fout = new FileOutputStream(stateJournal); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 98 | fout.write(p.marshall()); |
| 99 | fout.close(); |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 100 | 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 Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 108 | } 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 Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 115 | if (LOGV_ENABLED) { |
| 116 | Log.v(LOGTAG, "Clearing crash recovery state"); |
| 117 | } |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 118 | 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 Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 125 | synchronized (CrashRecoveryHandler.this) { |
| 126 | mIsPreloading = false; |
| 127 | mDidPreload = true; |
| 128 | CrashRecoveryHandler.this.notifyAll(); |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | }; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | public void backupState() { |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 137 | mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 138 | } |
| 139 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 140 | private Runnable mCreateState = new Runnable() { |
| 141 | |
| 142 | @Override |
| 143 | public void run() { |
| 144 | try { |
| 145 | final Bundle state = new Bundle(); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 146 | mController.onSaveInstanceState(state); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 147 | Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state) |
| 148 | .sendToTarget(); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 149 | // 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 Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 159 | public void clearState() { |
| 160 | mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE); |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 161 | updateLastRecovered(0); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 162 | } |
| 163 | |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 164 | private boolean shouldRestore() { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 165 | SharedPreferences prefs = mContext.getSharedPreferences( |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 166 | RECOVERY_PREFERENCES, Context.MODE_PRIVATE); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 167 | long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, 0); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 168 | long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered; |
| 169 | if (timeSinceLastRecover > PROMPT_INTERVAL) { |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 170 | return true; |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 171 | } |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 172 | return false; |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 173 | } |
| 174 | |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 175 | private void updateLastRecovered(long time) { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 176 | SharedPreferences prefs = mContext.getSharedPreferences( |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 177 | RECOVERY_PREFERENCES, Context.MODE_PRIVATE); |
| 178 | prefs.edit() |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 179 | .putLong(KEY_LAST_RECOVERED, time) |
Ben Murdoch | fb72a9a | 2011-07-25 18:18:32 +0100 | [diff] [blame] | 180 | .apply(); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 181 | } |
| 182 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 183 | private Bundle loadCrashState() { |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 184 | if (!shouldRestore()) { |
| 185 | return null; |
| 186 | } |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 187 | Bundle state = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 188 | Parcel parcel = Parcel.obtain(); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 189 | FileInputStream fin = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 190 | try { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 191 | File stateFile = new File(mContext.getCacheDir(), STATE_FILE); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 192 | fin = new FileInputStream(stateFile); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 193 | 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 Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 202 | state = parcel.readBundle(); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 203 | } catch (FileNotFoundException e) { |
| 204 | // No state to recover |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 205 | state = null; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 206 | } catch (Throwable e) { |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 207 | Log.w(LOGTAG, "Failed to recover state!", e); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 208 | state = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 209 | } finally { |
| 210 | parcel.recycle(); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 211 | if (fin != null) { |
| 212 | try { |
| 213 | fin.close(); |
| 214 | } catch (IOException e) { } |
| 215 | } |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 216 | } |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 217 | if (state != null && !state.isEmpty()) { |
| 218 | return state; |
| 219 | } |
| 220 | return null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 221 | } |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 222 | |
| 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 Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 233 | } |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 234 | updateLastRecovered(mRecoveryState != null |
| 235 | ? System.currentTimeMillis() : 0); |
Michael Kolb | 938db80 | 2011-10-11 10:49:05 -0700 | [diff] [blame^] | 236 | mController.doStart(mRecoveryState, intent, true); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 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 Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 250 | } |