Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
|
cmumford
2014/12/12 18:43:23
nit: no "(c)"
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/app_data_migrator.h" | |
| 6 | |
| 7 #include "base/files/file_util.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "content/public/browser/browser_context.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 #include "content/public/browser/indexed_db_context.h" | |
| 12 #include "content/public/browser/storage_partition.h" | |
| 13 #include "extensions/browser/extension_registry.h" | |
| 14 #include "extensions/common/extension.h" | |
| 15 #include "storage/browser/fileapi/file_system_context.h" | |
| 16 #include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h" | |
| 17 #include "storage/common/fileapi/file_system_types.h" | |
| 18 | |
| 19 using content::BrowserContext; | |
| 20 using content::BrowserThread; | |
| 21 using content::IndexedDBContext; | |
| 22 using content::StoragePartition; | |
| 23 using storage::FileSystemContext; | |
| 24 using storage::SandboxFileSystemBackendDelegate; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 void MigrateOnFileSystemThread(FileSystemContext* old_fs_context, | |
| 29 FileSystemContext* fs_context, | |
| 30 const extensions::Extension* extension) { | |
| 31 CHECK(old_fs_context->default_file_task_runner()->RunsTasksOnCurrentThread()); | |
| 32 | |
| 33 SandboxFileSystemBackendDelegate* old_sandbox_delegate = | |
| 34 old_fs_context->sandbox_delegate(); | |
| 35 SandboxFileSystemBackendDelegate* sandbox_delegate = | |
| 36 fs_context->sandbox_delegate(); | |
| 37 | |
| 38 GURL extension_url = | |
| 39 extensions::Extension::GetBaseURLFromExtensionId(extension->id()); | |
| 40 | |
| 41 scoped_ptr<storage::SandboxFileSystemBackendDelegate::OriginEnumerator> | |
| 42 enumerator(old_sandbox_delegate->CreateOriginEnumerator()); | |
| 43 | |
| 44 // Find out if there is a file system that needs migration. | |
| 45 GURL origin; | |
| 46 do { | |
| 47 origin = enumerator->Next(); | |
| 48 } while (origin != extension_url && !origin.is_empty()); | |
| 49 | |
| 50 if (!origin.is_empty()) { | |
| 51 // Copy the temporary file system. | |
| 52 if (enumerator->HasFileSystemType(storage::kFileSystemTypeTemporary)) { | |
| 53 old_sandbox_delegate->CopyFileSystem( | |
| 54 extension_url, storage::kFileSystemTypeTemporary, sandbox_delegate); | |
| 55 } | |
| 56 // Copy the persistent file system. | |
| 57 if (enumerator->HasFileSystemType(storage::kFileSystemTypePersistent)) { | |
| 58 old_sandbox_delegate->CopyFileSystem( | |
| 59 extension_url, storage::kFileSystemTypePersistent, sandbox_delegate); | |
| 60 } | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void MigrateOnIndexedDBThread(IndexedDBContext* old_indexed_db_context, | |
| 65 IndexedDBContext* indexed_db_context, | |
| 66 const extensions::Extension* extension) { | |
| 67 CHECK(old_indexed_db_context->TaskRunner()->RunsTasksOnCurrentThread()); | |
|
cmumford
2014/12/12 18:43:23
Question: In Chrome there are 640 DCHECK's for Run
| |
| 68 | |
| 69 GURL extension_url = | |
| 70 extensions::Extension::GetBaseURLFromExtensionId(extension->id()); | |
| 71 | |
| 72 old_indexed_db_context->CopyOriginData(extension_url, indexed_db_context); | |
| 73 } | |
| 74 | |
| 75 void MigrateFileSystem(StoragePartition* old_partition, | |
| 76 StoragePartition* current_partition, | |
| 77 const extensions::Extension* extension, | |
| 78 const base::Closure& reply) { | |
| 79 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 80 | |
| 81 // Retrieve the fs contexts in preparation for migration. | |
| 82 FileSystemContext* old_fs_context = old_partition->GetFileSystemContext(); | |
| 83 FileSystemContext* fs_context = current_partition->GetFileSystemContext(); | |
| 84 | |
| 85 // Perform the file system migration on the old file system's | |
| 86 // sequenced task runner. This is to ensure it queues after any | |
| 87 // in-flight file system operations. After it completes, it should | |
| 88 // invoke the original callback passed into DoMigrationAndReply. | |
| 89 old_fs_context->default_file_task_runner()->PostTaskAndReply( | |
| 90 FROM_HERE, | |
| 91 base::Bind(&MigrateOnFileSystemThread, make_scoped_refptr(old_fs_context), | |
| 92 make_scoped_refptr(fs_context), make_scoped_refptr(extension)), | |
| 93 reply); | |
| 94 } | |
| 95 | |
| 96 void MigrateLegacyPartition(StoragePartition* old_partition, | |
| 97 StoragePartition* current_partition, | |
| 98 const extensions::Extension* extension, | |
| 99 const base::Closure& reply) { | |
| 100 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 101 | |
| 102 // Retrieve both IndexedDB contexts in preparation for migration. | |
| 103 IndexedDBContext* indexed_db_context = | |
| 104 current_partition->GetIndexedDBContext(); | |
| 105 IndexedDBContext* old_indexed_db_context = | |
| 106 old_partition->GetIndexedDBContext(); | |
| 107 | |
| 108 // Create a closure for the file system migration. This is the next step in | |
| 109 // the migration flow after the IndexedDB migration. | |
| 110 base::Closure migrate_fs = | |
| 111 base::Bind(&MigrateFileSystem, old_partition, current_partition, | |
| 112 make_scoped_refptr(extension), reply); | |
| 113 | |
| 114 // Perform the IndexedDB migration on the old context's sequenced task | |
| 115 // runner. After completion, it should call MigrateFileSystem. | |
| 116 old_indexed_db_context->TaskRunner()->PostTaskAndReply( | |
| 117 FROM_HERE, base::Bind(&MigrateOnIndexedDBThread, | |
| 118 make_scoped_refptr(old_indexed_db_context), | |
| 119 make_scoped_refptr(indexed_db_context), | |
| 120 make_scoped_refptr(extension)), | |
| 121 migrate_fs); | |
| 122 } | |
| 123 | |
| 124 } // namespace | |
| 125 | |
| 126 namespace extensions { | |
| 127 | |
| 128 AppDataMigrator::AppDataMigrator(Profile* profile, ExtensionRegistry* registry) | |
| 129 : profile_(profile), registry_(registry) { | |
| 130 } | |
| 131 | |
| 132 bool AppDataMigrator::NeedsMigration(const Extension* old, | |
| 133 const Extension* extension) { | |
| 134 return old && old->is_legacy_packaged_app() && extension->is_platform_app(); | |
| 135 } | |
| 136 | |
| 137 void AppDataMigrator::DoMigrationAndReply(const Extension* old, | |
| 138 const Extension* extension, | |
| 139 const base::Closure& reply) { | |
| 140 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 141 DCHECK(NeedsMigration(old, extension)); | |
| 142 | |
| 143 // This should retrieve the general storage partition. | |
| 144 content::StoragePartition* old_partition = | |
| 145 BrowserContext::GetStoragePartitionForSite( | |
| 146 profile_, Extension::GetBaseURLFromExtensionId(extension->id())); | |
| 147 | |
| 148 // Enable the new extension so we can access its storage partition. | |
| 149 bool old_was_disabled = registry_->AddEnabled(extension); | |
| 150 | |
| 151 // This should create a new isolated partition for the new version of the | |
| 152 // extension. | |
| 153 StoragePartition* new_partition = BrowserContext::GetStoragePartitionForSite( | |
| 154 profile_, Extension::GetBaseURLFromExtensionId(extension->id())); | |
| 155 | |
| 156 // Now, restore the enabled/disabled state of the new and old extensions. | |
| 157 if (!old_was_disabled) { | |
| 158 registry_->AddEnabled(old); | |
| 159 } else { | |
| 160 registry_->RemoveEnabled(extension->id()); | |
| 161 } | |
| 162 | |
| 163 // Begin the actual migration flow. | |
| 164 MigrateLegacyPartition(old_partition, new_partition, extension, reply); | |
| 165 } | |
| 166 | |
| 167 } // namespace extensions | |
| OLD | NEW |