Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
| 19 | import java.io.IOException; |
| 20 | |
Christopher Tate | b6a6544 | 2010-03-05 15:47:48 -0800 | [diff] [blame] | 21 | import android.app.backup.BackupAgent; |
| 22 | import android.app.backup.BackupDataInput; |
| 23 | import android.app.backup.BackupDataOutput; |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 24 | import android.database.Cursor; |
| 25 | import android.os.ParcelFileDescriptor; |
| 26 | import android.provider.Browser; |
| 27 | import android.provider.Browser.BookmarkColumns; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 28 | import android.util.Log; |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 29 | |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 30 | import java.io.ByteArrayOutputStream; |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 31 | import java.io.DataInputStream; |
| 32 | import java.io.DataOutputStream; |
| 33 | import java.io.EOFException; |
| 34 | import java.io.File; |
| 35 | import java.io.FileInputStream; |
| 36 | import java.io.FileOutputStream; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 38 | import java.util.zip.CRC32; |
| 39 | |
| 40 | /** |
| 41 | * Settings backup agent for the Android browser. Currently the only thing |
| 42 | * stored is the set of bookmarks. It's okay if I/O exceptions are thrown |
| 43 | * out of the agent; the calling code handles it and the backup operation |
| 44 | * simply fails. |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 45 | * |
| 46 | * @hide |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 47 | */ |
| 48 | public class BrowserBackupAgent extends BackupAgent { |
Christopher Tate | f8b5998 | 2009-09-29 12:40:25 -0700 | [diff] [blame] | 49 | static final String TAG = "BrowserBackupAgent"; |
| 50 | static final boolean DEBUG = false; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 51 | |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 52 | static final String BOOKMARK_KEY = "_bookmarks_"; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 53 | /** this version num MUST be incremented if the flattened-file schema ever changes */ |
| 54 | static final int BACKUP_AGENT_VERSION = 0; |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * In order to determine whether the bookmark set has changed since the |
| 58 | * last time we did a backup, we store the following bits of info in the |
| 59 | * state file after a backup: |
| 60 | * |
| 61 | * 1. the size of the flattened bookmark file |
| 62 | * 2. the CRC32 of that file |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 63 | * 3. the agent version number [relevant following an OTA] |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 64 | * |
| 65 | * After we flatten the bookmarks file here in onBackup, we compare its |
| 66 | * metrics with the values from the saved state. If they match, it means |
| 67 | * the bookmarks didn't really change and we don't need to send the data. |
| 68 | * (If they don't match, of course, then they've changed and we do indeed |
| 69 | * send the new flattened file to be backed up.) |
| 70 | */ |
| 71 | @Override |
| 72 | public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 73 | ParcelFileDescriptor newState) throws IOException { |
| 74 | long savedFileSize = -1; |
| 75 | long savedCrc = -1; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 76 | int savedVersion = -1; |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 77 | |
| 78 | // Extract the previous bookmark file size & CRC from the saved state |
| 79 | DataInputStream in = new DataInputStream( |
| 80 | new FileInputStream(oldState.getFileDescriptor())); |
| 81 | try { |
| 82 | savedFileSize = in.readLong(); |
| 83 | savedCrc = in.readLong(); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 84 | savedVersion = in.readInt(); |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 85 | } catch (EOFException e) { |
| 86 | // It means we had no previous state; that's fine |
Henrik Baard | 3755040 | 2010-04-21 12:36:33 +0200 | [diff] [blame] | 87 | } finally { |
| 88 | if (in != null) { |
| 89 | in.close(); |
| 90 | } |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 93 | // Build a flattened representation of the bookmarks table |
| 94 | File tmpfile = File.createTempFile("bkp", null, getCacheDir()); |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 95 | try { |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 96 | FileOutputStream outfstream = new FileOutputStream(tmpfile); |
| 97 | long newCrc = buildBookmarkFile(outfstream); |
| 98 | outfstream.close(); |
Christopher Tate | b4645a1 | 2009-07-10 13:36:58 -0700 | [diff] [blame] | 99 | |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 100 | // Any changes since the last backup? |
| 101 | if ((savedVersion != BACKUP_AGENT_VERSION) |
| 102 | || (newCrc != savedCrc) |
| 103 | || (tmpfile.length() != savedFileSize)) { |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 104 | // Different checksum or different size, so we need to back it up |
| 105 | copyFileToBackup(BOOKMARK_KEY, tmpfile, data); |
| 106 | } |
| 107 | |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 108 | // Record our backup state and we're done |
| 109 | writeBackupState(tmpfile.length(), newCrc, newState); |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 110 | } finally { |
| 111 | // Make sure to tidy up when we're done |
| 112 | tmpfile.delete(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Restore from backup -- reads in the flattened bookmark file as supplied from |
| 118 | * the backup service, parses that out, and rebuilds the bookmarks table in the |
| 119 | * browser database from it. |
| 120 | */ |
| 121 | @Override |
| 122 | public void onRestore(BackupDataInput data, int appVersionCode, |
| 123 | ParcelFileDescriptor newState) throws IOException { |
| 124 | long crc = -1; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 125 | File tmpfile = File.createTempFile("rst", null, getFilesDir()); |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 126 | try { |
| 127 | while (data.readNextHeader()) { |
| 128 | if (BOOKMARK_KEY.equals(data.getKey())) { |
| 129 | // Read the flattened bookmark data into a temp file |
| 130 | crc = copyBackupToFile(data, tmpfile, data.getDataSize()); |
| 131 | |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 132 | FileInputStream infstream = new FileInputStream(tmpfile); |
| 133 | DataInputStream in = new DataInputStream(infstream); |
| 134 | |
| 135 | try { |
| 136 | int count = in.readInt(); |
| 137 | ArrayList<Bookmark> bookmarks = new ArrayList<Bookmark>(count); |
| 138 | |
| 139 | // Read all the bookmarks, then process later -- if we can't read |
| 140 | // all the data successfully, we don't touch the bookmarks table |
| 141 | for (int i = 0; i < count; i++) { |
| 142 | Bookmark mark = new Bookmark(); |
| 143 | mark.url = in.readUTF(); |
| 144 | mark.visits = in.readInt(); |
| 145 | mark.date = in.readLong(); |
| 146 | mark.created = in.readLong(); |
| 147 | mark.title = in.readUTF(); |
| 148 | bookmarks.add(mark); |
| 149 | } |
| 150 | |
| 151 | // Okay, we have all the bookmarks -- now see if we need to add |
| 152 | // them to the browser's database |
| 153 | int N = bookmarks.size(); |
Christopher Tate | f8b5998 | 2009-09-29 12:40:25 -0700 | [diff] [blame] | 154 | int nUnique = 0; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 155 | if (DEBUG) Log.v(TAG, "Restoring " + N + " bookmarks"); |
| 156 | String[] urlCol = new String[] { BookmarkColumns.URL }; |
| 157 | for (int i = 0; i < N; i++) { |
| 158 | Bookmark mark = bookmarks.get(i); |
| 159 | |
| 160 | // Does this URL exist in the bookmark table? |
| 161 | Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI, |
| 162 | urlCol, BookmarkColumns.URL + " == '" + mark.url + "' AND " + |
| 163 | BookmarkColumns.BOOKMARK + " == 1 ", null, null); |
| 164 | // if not, insert it |
| 165 | if (cursor.getCount() <= 0) { |
Christopher Tate | f8b5998 | 2009-09-29 12:40:25 -0700 | [diff] [blame] | 166 | if (DEBUG) Log.v(TAG, "Did not see url: " + mark.url); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 167 | // Right now we do not reconstruct the db entry in its |
| 168 | // entirety; we just add a new bookmark with the same data |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 169 | // FIXME: This file needs to be reworked |
| 170 | // anyway For now, add the bookmark at |
| 171 | // the root level. |
Jeff Hamilton | 7f6cf3e | 2010-09-17 17:22:21 -0500 | [diff] [blame] | 172 | Bookmarks.addBookmark(this, false, |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 173 | mark.url, mark.title, null, false, 0); |
Christopher Tate | f8b5998 | 2009-09-29 12:40:25 -0700 | [diff] [blame] | 174 | nUnique++; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 175 | } else { |
Christopher Tate | f8b5998 | 2009-09-29 12:40:25 -0700 | [diff] [blame] | 176 | if (DEBUG) Log.v(TAG, "Skipping extant url: " + mark.url); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 177 | } |
| 178 | cursor.close(); |
| 179 | } |
Christopher Tate | f8b5998 | 2009-09-29 12:40:25 -0700 | [diff] [blame] | 180 | Log.i(TAG, "Restored " + nUnique + " of " + N + " bookmarks"); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 181 | } catch (IOException ioe) { |
| 182 | Log.w(TAG, "Bad backup data; not restoring"); |
| 183 | crc = -1; |
Henrik Baard | 3755040 | 2010-04-21 12:36:33 +0200 | [diff] [blame] | 184 | } finally { |
| 185 | if (in != null) { |
| 186 | in.close(); |
| 187 | } |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 188 | } |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // Last, write the state we just restored from so we can discern |
| 192 | // changes whenever we get invoked for backup in the future |
| 193 | writeBackupState(tmpfile.length(), crc, newState); |
| 194 | } |
| 195 | } finally { |
| 196 | // Whatever happens, delete the temp file |
| 197 | tmpfile.delete(); |
| 198 | } |
| 199 | } |
| 200 | |
Henrik Baard | 3755040 | 2010-04-21 12:36:33 +0200 | [diff] [blame] | 201 | static class Bookmark { |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 202 | public String url; |
| 203 | public int visits; |
| 204 | public long date; |
| 205 | public long created; |
| 206 | public String title; |
| 207 | } |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 208 | /* |
| 209 | * Utility functions |
| 210 | */ |
| 211 | |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 212 | // Flatten the bookmarks table into the given file, calculating its CRC in the process |
| 213 | private long buildBookmarkFile(FileOutputStream outfstream) throws IOException { |
| 214 | CRC32 crc = new CRC32(); |
| 215 | ByteArrayOutputStream bufstream = new ByteArrayOutputStream(512); |
| 216 | DataOutputStream bout = new DataOutputStream(bufstream); |
| 217 | |
| 218 | Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI, |
| 219 | new String[] { BookmarkColumns.URL, BookmarkColumns.VISITS, |
| 220 | BookmarkColumns.DATE, BookmarkColumns.CREATED, |
| 221 | BookmarkColumns.TITLE }, |
| 222 | BookmarkColumns.BOOKMARK + " == 1 ", null, null); |
| 223 | |
| 224 | // The first thing in the file is the row count... |
| 225 | int count = cursor.getCount(); |
| 226 | if (DEBUG) Log.v(TAG, "Backing up " + count + " bookmarks"); |
| 227 | bout.writeInt(count); |
| 228 | byte[] record = bufstream.toByteArray(); |
| 229 | crc.update(record); |
| 230 | outfstream.write(record); |
| 231 | |
| 232 | // ... followed by the data for each row |
| 233 | for (int i = 0; i < count; i++) { |
| 234 | cursor.moveToNext(); |
| 235 | |
| 236 | String url = cursor.getString(0); |
| 237 | int visits = cursor.getInt(1); |
| 238 | long date = cursor.getLong(2); |
| 239 | long created = cursor.getLong(3); |
| 240 | String title = cursor.getString(4); |
| 241 | |
| 242 | // construct the flattened record in a byte array |
| 243 | bufstream.reset(); |
| 244 | bout.writeUTF(url); |
| 245 | bout.writeInt(visits); |
| 246 | bout.writeLong(date); |
| 247 | bout.writeLong(created); |
| 248 | bout.writeUTF(title); |
| 249 | |
| 250 | // Update the CRC and write the record to the temp file |
| 251 | record = bufstream.toByteArray(); |
| 252 | crc.update(record); |
| 253 | outfstream.write(record); |
| 254 | |
| 255 | if (DEBUG) Log.v(TAG, " wrote url " + url); |
| 256 | } |
| 257 | |
| 258 | cursor.close(); |
| 259 | return crc.getValue(); |
| 260 | } |
| 261 | |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 262 | // Write the file to backup as a single record under the given key |
| 263 | private void copyFileToBackup(String key, File file, BackupDataOutput data) |
| 264 | throws IOException { |
| 265 | final int CHUNK = 8192; |
| 266 | byte[] buf = new byte[CHUNK]; |
| 267 | |
| 268 | int toCopy = (int) file.length(); |
| 269 | data.writeEntityHeader(key, toCopy); |
| 270 | |
| 271 | FileInputStream in = new FileInputStream(file); |
Henrik Baard | 3755040 | 2010-04-21 12:36:33 +0200 | [diff] [blame] | 272 | try { |
| 273 | int nRead; |
| 274 | while (toCopy > 0) { |
| 275 | nRead = in.read(buf, 0, CHUNK); |
| 276 | data.writeEntityData(buf, nRead); |
| 277 | toCopy -= nRead; |
| 278 | } |
| 279 | } finally { |
| 280 | if (in != null) { |
| 281 | in.close(); |
| 282 | } |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 283 | } |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // Read the given file from backup to a file, calculating a CRC32 along the way |
| 287 | private long copyBackupToFile(BackupDataInput data, File file, int toRead) |
| 288 | throws IOException { |
| 289 | final int CHUNK = 8192; |
| 290 | byte[] buf = new byte[CHUNK]; |
| 291 | CRC32 crc = new CRC32(); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 292 | FileOutputStream out = new FileOutputStream(file); |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 293 | |
Henrik Baard | 3755040 | 2010-04-21 12:36:33 +0200 | [diff] [blame] | 294 | try { |
| 295 | while (toRead > 0) { |
| 296 | int numRead = data.readEntityData(buf, 0, CHUNK); |
| 297 | crc.update(buf, 0, numRead); |
| 298 | out.write(buf, 0, numRead); |
| 299 | toRead -= numRead; |
| 300 | } |
| 301 | } finally { |
| 302 | if (out != null) { |
| 303 | out.close(); |
| 304 | } |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 305 | } |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 306 | return crc.getValue(); |
| 307 | } |
| 308 | |
| 309 | // Write the given metrics to the new state file |
| 310 | private void writeBackupState(long fileSize, long crc, ParcelFileDescriptor stateFile) |
| 311 | throws IOException { |
| 312 | DataOutputStream out = new DataOutputStream( |
| 313 | new FileOutputStream(stateFile.getFileDescriptor())); |
Henrik Baard | 3755040 | 2010-04-21 12:36:33 +0200 | [diff] [blame] | 314 | try { |
| 315 | out.writeLong(fileSize); |
| 316 | out.writeLong(crc); |
| 317 | out.writeInt(BACKUP_AGENT_VERSION); |
| 318 | } finally { |
| 319 | if (out != null) { |
| 320 | out.close(); |
| 321 | } |
| 322 | } |
Christopher Tate | de6f131 | 2009-07-07 13:11:41 -0700 | [diff] [blame] | 323 | } |
| 324 | } |