OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
benwells
2015/01/14 03:34:28
Ditto with the year.
| |
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 DCHECK( | |
32 old_fs_context->default_file_task_runner()->RunsTasksOnCurrentThread()); | |
33 | |
34 SandboxFileSystemBackendDelegate* old_sandbox_delegate = | |
35 old_fs_context->sandbox_delegate(); | |
36 SandboxFileSystemBackendDelegate* sandbox_delegate = | |
37 fs_context->sandbox_delegate(); | |
38 | |
39 GURL extension_url = | |
40 extensions::Extension::GetBaseURLFromExtensionId(extension->id()); | |
41 | |
42 scoped_ptr<storage::SandboxFileSystemBackendDelegate::OriginEnumerator> | |
43 enumerator(old_sandbox_delegate->CreateOriginEnumerator()); | |
44 | |
45 // Find out if there is a file system that needs migration. | |
46 GURL origin; | |
47 do { | |
48 origin = enumerator->Next(); | |
49 } while (origin != extension_url && !origin.is_empty()); | |
50 | |
51 if (!origin.is_empty()) { | |
52 // Copy the temporary file system. | |
53 if (enumerator->HasFileSystemType(storage::kFileSystemTypeTemporary)) { | |
54 old_sandbox_delegate->CopyFileSystem( | |
55 extension_url, storage::kFileSystemTypeTemporary, sandbox_delegate); | |
56 } | |
57 // Copy the persistent file system. | |
58 if (enumerator->HasFileSystemType(storage::kFileSystemTypePersistent)) { | |
59 old_sandbox_delegate->CopyFileSystem( | |
60 extension_url, storage::kFileSystemTypePersistent, sandbox_delegate); | |
61 } | |
62 } | |
63 } | |
64 | |
65 void MigrateOnIndexedDBThread(IndexedDBContext* old_indexed_db_context, | |
66 IndexedDBContext* indexed_db_context, | |
67 const extensions::Extension* extension) { | |
68 DCHECK(old_indexed_db_context->TaskRunner()->RunsTasksOnCurrentThread()); | |
69 | |
70 GURL extension_url = | |
71 extensions::Extension::GetBaseURLFromExtensionId(extension->id()); | |
72 | |
73 old_indexed_db_context->CopyOriginData(extension_url, indexed_db_context); | |
74 } | |
75 | |
76 void MigrateFileSystem(StoragePartition* old_partition, | |
77 StoragePartition* current_partition, | |
78 const extensions::Extension* extension, | |
79 const base::Closure& reply) { | |
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
81 | |
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 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
101 | |
102 IndexedDBContext* indexed_db_context = | |
103 current_partition->GetIndexedDBContext(); | |
104 IndexedDBContext* old_indexed_db_context = | |
105 old_partition->GetIndexedDBContext(); | |
106 | |
107 // Create a closure for the file system migration. This is the next step in | |
108 // the migration flow after the IndexedDB migration. | |
109 base::Closure migrate_fs = | |
110 base::Bind(&MigrateFileSystem, old_partition, current_partition, | |
111 make_scoped_refptr(extension), reply); | |
112 | |
113 // Perform the IndexedDB migration on the old context's sequenced task | |
114 // runner. After completion, it should call MigrateFileSystem. | |
115 old_indexed_db_context->TaskRunner()->PostTaskAndReply( | |
116 FROM_HERE, base::Bind(&MigrateOnIndexedDBThread, | |
117 make_scoped_refptr(old_indexed_db_context), | |
118 make_scoped_refptr(indexed_db_context), | |
119 make_scoped_refptr(extension)), | |
120 migrate_fs); | |
121 } | |
122 | |
123 } // namespace | |
124 | |
125 namespace extensions { | |
126 | |
127 AppDataMigrator::AppDataMigrator(Profile* profile, ExtensionRegistry* registry) | |
128 : profile_(profile), registry_(registry) { | |
129 } | |
130 | |
131 bool AppDataMigrator::NeedsMigration(const Extension* old, | |
132 const Extension* extension) { | |
133 return old && old->is_legacy_packaged_app() && extension->is_platform_app(); | |
134 } | |
135 | |
136 void AppDataMigrator::DoMigrationAndReply(const Extension* old, | |
137 const Extension* extension, | |
138 const base::Closure& reply) { | |
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
140 DCHECK(NeedsMigration(old, extension)); | |
141 | |
142 // This should retrieve the general storage partition. | |
143 content::StoragePartition* old_partition = | |
144 BrowserContext::GetStoragePartitionForSite( | |
145 profile_, Extension::GetBaseURLFromExtensionId(extension->id())); | |
146 | |
147 // Enable the new extension so we can access its storage partition. | |
148 bool old_was_disabled = registry_->AddEnabled(extension); | |
149 | |
150 // This should create a new isolated partition for the new version of the | |
151 // extension. | |
152 StoragePartition* new_partition = BrowserContext::GetStoragePartitionForSite( | |
153 profile_, Extension::GetBaseURLFromExtensionId(extension->id())); | |
154 | |
155 // Now, restore the enabled/disabled state of the new and old extensions. | |
156 if (old_was_disabled) | |
157 registry_->RemoveEnabled(extension->id()); | |
158 else | |
159 registry_->AddEnabled(old); | |
160 | |
161 MigrateLegacyPartition(old_partition, new_partition, extension, reply); | |
162 } | |
163 | |
164 } // namespace extensions | |
OLD | NEW |