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 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 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() { |
kaiyiz | a8b6dbb | 2013-07-29 18:11:22 +0800 | [diff] [blame] | 134 | clearState(false); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Clear cached state files. |
| 139 | * |
| 140 | * @param block If block, clear state files in the caller thread, otherwise |
| 141 | * do it in a worker thread. |
| 142 | */ |
| 143 | void clearState(boolean block) { |
| 144 | if (block) { |
| 145 | if (mContext != null) { |
| 146 | File state = new File(mContext.getCacheDir(), STATE_FILE); |
| 147 | if (state.exists()) { |
| 148 | state.delete(); |
| 149 | } |
| 150 | } |
| 151 | } else { |
| 152 | mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE); |
| 153 | } |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 154 | updateLastRecovered(0); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 155 | } |
| 156 | |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 157 | private boolean shouldRestore() { |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 158 | BrowserSettings browserSettings = BrowserSettings.getInstance(); |
| 159 | long lastRecovered = browserSettings.getLastRecovered(); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 160 | long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered; |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 161 | return (timeSinceLastRecover > PROMPT_INTERVAL) |
| 162 | || browserSettings.wasLastRunPaused(); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 163 | } |
| 164 | |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 165 | private void updateLastRecovered(long time) { |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 166 | BrowserSettings browserSettings = BrowserSettings.getInstance(); |
| 167 | browserSettings.setLastRecovered(time); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 168 | } |
| 169 | |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 170 | synchronized private Bundle loadCrashState() { |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 171 | if (!shouldRestore()) { |
| 172 | return null; |
| 173 | } |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 174 | BrowserSettings browserSettings = BrowserSettings.getInstance(); |
| 175 | browserSettings.setLastRunPaused(false); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 176 | Bundle state = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 177 | Parcel parcel = Parcel.obtain(); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 178 | FileInputStream fin = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 179 | try { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 180 | File stateFile = new File(mContext.getCacheDir(), STATE_FILE); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 181 | fin = new FileInputStream(stateFile); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 182 | ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); |
| 183 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 184 | int read; |
| 185 | while ((read = fin.read(buffer)) > 0) { |
| 186 | dataStream.write(buffer, 0, read); |
| 187 | } |
| 188 | byte[] data = dataStream.toByteArray(); |
| 189 | parcel.unmarshall(data, 0, data.length); |
| 190 | parcel.setDataPosition(0); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 191 | state = parcel.readBundle(); |
John Reck | e659d7b | 2011-10-13 15:51:02 -0700 | [diff] [blame] | 192 | if (state != null && !state.isEmpty()) { |
| 193 | return state; |
| 194 | } |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 195 | } catch (FileNotFoundException e) { |
| 196 | // No state to recover |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 197 | } catch (Throwable e) { |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 198 | Log.w(LOGTAG, "Failed to recover state!", e); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 199 | } finally { |
| 200 | parcel.recycle(); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 201 | if (fin != null) { |
| 202 | try { |
| 203 | fin.close(); |
| 204 | } catch (IOException e) { } |
| 205 | } |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 206 | } |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 207 | return null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 208 | } |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 209 | |
| 210 | public void startRecovery(Intent intent) { |
| 211 | synchronized (CrashRecoveryHandler.this) { |
| 212 | while (mIsPreloading) { |
| 213 | try { |
| 214 | CrashRecoveryHandler.this.wait(); |
| 215 | } catch (InterruptedException e) {} |
| 216 | } |
| 217 | } |
| 218 | if (!mDidPreload) { |
| 219 | mRecoveryState = loadCrashState(); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 220 | } |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame] | 221 | updateLastRecovered(mRecoveryState != null |
| 222 | ? System.currentTimeMillis() : 0); |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 223 | mController.doStart(mRecoveryState, intent); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 224 | mRecoveryState = null; |
| 225 | } |
| 226 | |
| 227 | public void preloadCrashState() { |
| 228 | synchronized (CrashRecoveryHandler.this) { |
| 229 | if (mIsPreloading) { |
| 230 | return; |
| 231 | } |
| 232 | mIsPreloading = true; |
| 233 | } |
| 234 | mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE); |
| 235 | } |
| 236 | |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 237 | /** |
| 238 | * Writes the crash recovery state to a file synchronously. |
| 239 | * Errors are swallowed, but logged. |
| 240 | * @param state The state to write out |
| 241 | */ |
| 242 | synchronized void writeState(Bundle state) { |
| 243 | if (LOGV_ENABLED) { |
| 244 | Log.v(LOGTAG, "Saving crash recovery state"); |
| 245 | } |
| 246 | Parcel p = Parcel.obtain(); |
| 247 | try { |
| 248 | state.writeToParcel(p, 0); |
| 249 | File stateJournal = new File(mContext.getCacheDir(), |
| 250 | STATE_FILE + ".journal"); |
| 251 | FileOutputStream fout = new FileOutputStream(stateJournal); |
| 252 | fout.write(p.marshall()); |
| 253 | fout.close(); |
| 254 | File stateFile = new File(mContext.getCacheDir(), |
| 255 | STATE_FILE); |
| 256 | if (!stateJournal.renameTo(stateFile)) { |
| 257 | // Failed to rename, try deleting the existing |
| 258 | // file and try again |
| 259 | stateFile.delete(); |
| 260 | stateJournal.renameTo(stateFile); |
| 261 | } |
| 262 | } catch (Throwable e) { |
| 263 | Log.i(LOGTAG, "Failed to save persistent state", e); |
| 264 | } finally { |
| 265 | p.recycle(); |
| 266 | } |
| 267 | } |
kaiyiz | a8b6dbb | 2013-07-29 18:11:22 +0800 | [diff] [blame] | 268 | } |