OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
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 CopyFileSystem(SandboxFileSystemBackendDelegate* old_sandbox_delegate, | |
29 SandboxFileSystemBackendDelegate* sandbox_delegate, | |
30 const GURL& origin, | |
31 storage::FileSystemType type) { | |
32 CHECK(old_sandbox_delegate->file_task_runner()->RunsTasksOnCurrentThread()); | |
33 | |
34 base::FilePath old_base_path = | |
35 old_sandbox_delegate->GetBaseDirectoryForOriginAndType( | |
36 origin, type, false); | |
tzik
2014/11/20 06:56:21
The backing database may be left open here.
Could
| |
37 | |
38 if (base::PathExists(old_base_path)) { | |
39 // There shouldn't be an existing file system directory in the new | |
40 // partition. | |
41 DCHECK(!base::PathExists(sandbox_delegate->GetBaseDirectoryForOriginAndType( | |
42 origin, type, false))); | |
43 | |
44 base::FilePath base_path = | |
45 sandbox_delegate->GetBaseDirectoryForOriginAndType(origin, type, true); | |
46 | |
47 base::CopyDirectory(old_base_path, base_path.DirName(), true); | |
48 } | |
49 } | |
50 | |
51 void MigrateOnFileSystemThread( | |
52 SandboxFileSystemBackendDelegate* old_sandbox_delegate, | |
53 SandboxFileSystemBackendDelegate* sandbox_delegate, | |
54 const extensions::Extension* extension) { | |
55 CHECK(old_sandbox_delegate->file_task_runner()->RunsTasksOnCurrentThread()); | |
56 | |
57 GURL extension_url = | |
58 extensions::Extension::GetBaseURLFromExtensionId(extension->id()); | |
59 | |
60 scoped_ptr<storage::SandboxFileSystemBackendDelegate::OriginEnumerator> | |
61 enumerator(old_sandbox_delegate->CreateOriginEnumerator()); | |
62 | |
63 // Find out if there is a file system that needs migration. | |
64 GURL origin; | |
65 do { | |
66 origin = enumerator->Next(); | |
67 } while (origin != extension_url && !origin.is_empty()); | |
68 | |
69 if (!origin.is_empty()) { | |
70 // Copy the temporary file system. | |
71 if (enumerator->HasFileSystemType(storage::kFileSystemTypeTemporary)) { | |
72 CopyFileSystem(old_sandbox_delegate, | |
73 sandbox_delegate, | |
74 extension_url, | |
75 storage::kFileSystemTypeTemporary); | |
76 } | |
77 // Copy the persistent file system. | |
78 if (enumerator->HasFileSystemType(storage::kFileSystemTypePersistent)) { | |
79 CopyFileSystem(old_sandbox_delegate, | |
80 sandbox_delegate, | |
81 extension_url, | |
82 storage::kFileSystemTypePersistent); | |
83 } | |
84 } | |
85 } | |
86 | |
87 void MigrateOnIndexedDBThread(IndexedDBContext* old_indexed_db_context, | |
88 IndexedDBContext* indexed_db_context, | |
89 const extensions::Extension* extension) { | |
90 CHECK(old_indexed_db_context->TaskRunner()->RunsTasksOnCurrentThread()); | |
91 | |
92 GURL extension_url = | |
93 extensions::Extension::GetBaseURLFromExtensionId(extension->id()); | |
94 | |
95 old_indexed_db_context->CopyOriginData(extension_url, indexed_db_context); | |
96 } | |
97 void MigrateFileSystem(StoragePartition* old_partition, | |
98 StoragePartition* current_partition, | |
99 const extensions::Extension* extension, | |
100 const base::Closure& reply) { | |
101 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
102 | |
103 // Retrieve the fs contexts and sandbox delegates in preparation for | |
104 // migration. | |
105 FileSystemContext* old_fs_context = old_partition->GetFileSystemContext(); | |
106 FileSystemContext* fs_context = current_partition->GetFileSystemContext(); | |
107 | |
108 SandboxFileSystemBackendDelegate* old_sandbox_delegate = | |
109 old_fs_context->sandbox_delegate(); | |
110 SandboxFileSystemBackendDelegate* sandbox_delegate = | |
111 fs_context->sandbox_delegate(); | |
112 | |
113 // Perform the file system migration on the old file system's | |
114 // sequenced task runner. This is to ensure it queues after any | |
115 // in-flight file system operations. After it completes, it should | |
116 // invoke the original callback passed into DoMigrationAndReply. | |
117 old_sandbox_delegate->file_task_runner()->PostTaskAndReply( | |
118 FROM_HERE, | |
119 base::Bind(&MigrateOnFileSystemThread, | |
120 old_sandbox_delegate, | |
121 sandbox_delegate, | |
122 make_scoped_refptr(extension)), | |
123 reply); | |
124 } | |
125 void MigrateLegacyPartition(StoragePartition* old_partition, | |
126 StoragePartition* current_partition, | |
127 const extensions::Extension* extension, | |
128 const base::Closure& reply) { | |
129 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
130 | |
131 // Retrieve both IndexedDB contexts in preparation for migration. | |
132 IndexedDBContext* indexed_db_context = | |
133 current_partition->GetIndexedDBContext(); | |
134 IndexedDBContext* old_indexed_db_context = | |
135 old_partition->GetIndexedDBContext(); | |
136 | |
137 // Create a closure for the file system migration. This is the next step in | |
138 // the migration flow after the IndexedDB migration. | |
139 base::Closure migrateFs = base::Bind( | |
140 &MigrateFileSystem, old_partition, current_partition, extension, reply); | |
141 | |
142 // Perform the IndexedDB migration on the old context's sequenced task | |
143 // runner. After completion, it should call MigrateFileSystem. | |
144 old_indexed_db_context->TaskRunner()->PostTaskAndReply( | |
145 FROM_HERE, | |
146 base::Bind(&MigrateOnIndexedDBThread, | |
147 old_indexed_db_context, | |
148 indexed_db_context, | |
149 make_scoped_refptr(extension)), | |
150 migrateFs); | |
151 } | |
152 | |
153 } // namespace | |
154 | |
155 namespace extensions { | |
156 | |
157 AppDataMigrator::AppDataMigrator(Profile* profile, ExtensionRegistry* registry) | |
158 : profile_(profile), registry_(registry) { | |
159 } | |
160 | |
161 bool AppDataMigrator::NeedsMigration(const Extension* old, | |
162 const Extension* extension) { | |
163 return old && old->is_legacy_packaged_app() && extension->is_platform_app(); | |
164 } | |
165 | |
166 void AppDataMigrator::DoMigrationAndReply(const Extension* old, | |
167 const Extension* extension, | |
168 const base::Closure& reply) { | |
169 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
170 | |
171 // This should retrieve the general storage partition. | |
172 content::StoragePartition* old_partition = | |
173 BrowserContext::GetStoragePartitionForSite( | |
174 profile_, Extension::GetBaseURLFromExtensionId(extension->id())); | |
175 | |
176 // Enable the new extension so we can access its storage partition. | |
177 bool oldWasDisabled = registry_->AddEnabled(extension); | |
178 | |
179 // This should create a new isolated partition for the new version of the | |
180 // extension. | |
181 StoragePartition* new_partition = BrowserContext::GetStoragePartitionForSite( | |
182 profile_, Extension::GetBaseURLFromExtensionId(extension->id())); | |
183 | |
184 // Now, restore the enabled/disabled state of the new and old extensions. | |
185 if (!oldWasDisabled) { | |
186 registry_->AddEnabled(old); | |
187 } else { | |
188 registry_->RemoveEnabled(extension->id()); | |
189 } | |
190 | |
191 // Begin the actual migration flow. | |
192 MigrateLegacyPartition(old_partition, new_partition, extension, reply); | |
193 } | |
194 | |
195 } // namespace extensions | |
OLD | NEW |