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 | |
| 37 | private static final String LOGTAG = "BrowserCrashRecovery"; |
| 38 | private static final String STATE_FILE = "browser_state.parcel"; |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 39 | private static final String RECOVERY_PREFERENCES = "browser_recovery_prefs"; |
| 40 | private static final String KEY_LAST_RECOVERED = "last_recovered"; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 41 | private static final int BUFFER_SIZE = 4096; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 42 | private static final long BACKUP_DELAY = 500; // 500ms between writes |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 43 | /* This is the duration for which we will prompt to restore |
| 44 | * instead of automatically restoring. The first time the browser crashes, |
| 45 | * we will automatically restore. If we then crash again within XX minutes, |
| 46 | * we will prompt instead of automatically restoring. |
| 47 | */ |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame^] | 48 | private static final long PROMPT_INTERVAL = 5 * 60 * 1000; // 5 minutes |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 49 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 50 | private static final int MSG_WRITE_STATE = 1; |
| 51 | private static final int MSG_CLEAR_STATE = 2; |
| 52 | private static final int MSG_PRELOAD_STATE = 3; |
| 53 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 54 | private static CrashRecoveryHandler sInstance; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 55 | |
| 56 | private Controller mController; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 57 | private Context mContext; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 58 | private Handler mForegroundHandler; |
| 59 | private Handler mBackgroundHandler; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 60 | private boolean mIsPreloading = false; |
| 61 | private boolean mDidPreload = false; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 62 | private Bundle mRecoveryState = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 63 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 64 | public static CrashRecoveryHandler initialize(Controller controller) { |
| 65 | if (sInstance == null) { |
| 66 | sInstance = new CrashRecoveryHandler(controller); |
| 67 | } else { |
| 68 | sInstance.mController = controller; |
| 69 | } |
| 70 | return sInstance; |
| 71 | } |
| 72 | |
| 73 | public static CrashRecoveryHandler getInstance() { |
| 74 | return sInstance; |
| 75 | } |
| 76 | |
| 77 | private CrashRecoveryHandler(Controller controller) { |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 78 | mController = controller; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 79 | mContext = mController.getActivity().getApplicationContext(); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 80 | mForegroundHandler = new Handler(); |
John Reck | cadae72 | 2011-07-25 11:36:17 -0700 | [diff] [blame] | 81 | mBackgroundHandler = new Handler(BackgroundHandler.getLooper()) { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 82 | |
| 83 | @Override |
| 84 | public void handleMessage(Message msg) { |
| 85 | switch (msg.what) { |
| 86 | case MSG_WRITE_STATE: |
| 87 | Parcel p = Parcel.obtain(); |
| 88 | try { |
| 89 | Bundle state = (Bundle) msg.obj; |
| 90 | state.writeToParcel(p, 0); |
| 91 | File stateFile = new File(mContext.getCacheDir(), STATE_FILE); |
| 92 | FileOutputStream fout = new FileOutputStream(stateFile); |
| 93 | fout.write(p.marshall()); |
| 94 | fout.close(); |
| 95 | } catch (Throwable e) { |
| 96 | Log.i(LOGTAG, "Failed to save persistent state", e); |
| 97 | } finally { |
| 98 | p.recycle(); |
| 99 | } |
| 100 | break; |
| 101 | case MSG_CLEAR_STATE: |
| 102 | File state = new File(mContext.getCacheDir(), STATE_FILE); |
| 103 | if (state.exists()) { |
| 104 | state.delete(); |
| 105 | } |
| 106 | break; |
| 107 | case MSG_PRELOAD_STATE: |
| 108 | mRecoveryState = loadCrashState(); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 109 | synchronized (CrashRecoveryHandler.this) { |
| 110 | mIsPreloading = false; |
| 111 | mDidPreload = true; |
| 112 | CrashRecoveryHandler.this.notifyAll(); |
| 113 | } |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | }; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | public void backupState() { |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 121 | mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 122 | } |
| 123 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 124 | private Runnable mCreateState = new Runnable() { |
| 125 | |
| 126 | @Override |
| 127 | public void run() { |
| 128 | try { |
| 129 | final Bundle state = new Bundle(); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 130 | mController.onSaveInstanceState(state); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 131 | Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state) |
| 132 | .sendToTarget(); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 133 | // Remove any queued up saves |
| 134 | mForegroundHandler.removeCallbacks(mCreateState); |
| 135 | } catch (Throwable t) { |
| 136 | Log.w(LOGTAG, "Failed to save state", t); |
| 137 | return; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | }; |
| 142 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 143 | public void clearState() { |
| 144 | mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE); |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame^] | 145 | updateLastRecovered(0); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 146 | } |
| 147 | |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame^] | 148 | private boolean shouldRestore() { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 149 | SharedPreferences prefs = mContext.getSharedPreferences( |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 150 | RECOVERY_PREFERENCES, Context.MODE_PRIVATE); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 151 | long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, 0); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 152 | long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered; |
| 153 | if (timeSinceLastRecover > PROMPT_INTERVAL) { |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame^] | 154 | return true; |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 155 | } |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame^] | 156 | return false; |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 157 | } |
| 158 | |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame^] | 159 | private void updateLastRecovered(long time) { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 160 | SharedPreferences prefs = mContext.getSharedPreferences( |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 161 | RECOVERY_PREFERENCES, Context.MODE_PRIVATE); |
| 162 | prefs.edit() |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame^] | 163 | .putLong(KEY_LAST_RECOVERED, time) |
Ben Murdoch | fb72a9a | 2011-07-25 18:18:32 +0100 | [diff] [blame] | 164 | .apply(); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 165 | } |
| 166 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 167 | private Bundle loadCrashState() { |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame^] | 168 | if (!shouldRestore()) { |
| 169 | return null; |
| 170 | } |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 171 | Bundle state = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 172 | Parcel parcel = Parcel.obtain(); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 173 | FileInputStream fin = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 174 | try { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 175 | File stateFile = new File(mContext.getCacheDir(), STATE_FILE); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 176 | fin = new FileInputStream(stateFile); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 177 | ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); |
| 178 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 179 | int read; |
| 180 | while ((read = fin.read(buffer)) > 0) { |
| 181 | dataStream.write(buffer, 0, read); |
| 182 | } |
| 183 | byte[] data = dataStream.toByteArray(); |
| 184 | parcel.unmarshall(data, 0, data.length); |
| 185 | parcel.setDataPosition(0); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 186 | state = parcel.readBundle(); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 187 | } catch (FileNotFoundException e) { |
| 188 | // No state to recover |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 189 | state = null; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 190 | } catch (Throwable e) { |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 191 | Log.w(LOGTAG, "Failed to recover state!", e); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 192 | state = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 193 | } finally { |
| 194 | parcel.recycle(); |
Ben Murdoch | 679da25 | 2011-07-25 17:59:36 +0100 | [diff] [blame] | 195 | if (fin != null) { |
| 196 | try { |
| 197 | fin.close(); |
| 198 | } catch (IOException e) { } |
| 199 | } |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 200 | } |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame^] | 201 | if (state != null && !state.isEmpty()) { |
| 202 | return state; |
| 203 | } |
| 204 | return null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 205 | } |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 206 | |
| 207 | public void startRecovery(Intent intent) { |
| 208 | synchronized (CrashRecoveryHandler.this) { |
| 209 | while (mIsPreloading) { |
| 210 | try { |
| 211 | CrashRecoveryHandler.this.wait(); |
| 212 | } catch (InterruptedException e) {} |
| 213 | } |
| 214 | } |
| 215 | if (!mDidPreload) { |
| 216 | mRecoveryState = loadCrashState(); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 217 | } |
John Reck | 023124e | 2011-07-29 11:32:54 -0700 | [diff] [blame^] | 218 | updateLastRecovered(mRecoveryState != null |
| 219 | ? System.currentTimeMillis() : 0); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 220 | mController.doStart(mRecoveryState, intent); |
| 221 | mRecoveryState = null; |
| 222 | } |
| 223 | |
| 224 | public void preloadCrashState() { |
| 225 | synchronized (CrashRecoveryHandler.this) { |
| 226 | if (mIsPreloading) { |
| 227 | return; |
| 228 | } |
| 229 | mIsPreloading = true; |
| 230 | } |
| 231 | mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE); |
| 232 | } |
| 233 | |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 234 | } |