OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 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 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 // Retrieve the fs contexts in preparation for migration. | |
83 FileSystemContext* old_fs_context = old_partition->GetFileSystemContext(); | |
84 FileSystemContext* fs_context = current_partition->GetFileSystemContext(); | |
85 | |
86 // Perform the file system migration on the old file system's | |
87 // sequenced task runner. This is to ensure it queues after any | |
88 // in-flight file system operations. After it completes, it should | |
89 // invoke the original callback passed into DoMigrationAndReply. | |
90 old_fs_context->default_file_task_runner()->PostTaskAndReply( | |
91 FROM_HERE, | |
92 base::Bind(&MigrateOnFileSystemThread, make_scoped_refptr(old_fs_context), | |
93 make_scoped_refptr(fs_context), make_scoped_refptr(extension)), | |
94 reply); | |
95 } | |
96 | |
97 void MigrateLegacyPartition(StoragePartition* old_partition, | |
98 StoragePartition* current_partition, | |
99 const extensions::Extension* extension, | |
100 const base::Closure& reply) { | |
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
102 | |
103 // Retrieve both IndexedDB contexts in preparation for migration. | |
cmumford
2015/01/06 15:30:26
Nit: I'm not sure this context (and the one above)
| |
104 IndexedDBContext* indexed_db_context = | |
105 current_partition->GetIndexedDBContext(); | |
106 IndexedDBContext* old_indexed_db_context = | |
107 old_partition->GetIndexedDBContext(); | |
108 | |
109 // Create a closure for the file system migration. This is the next step in | |
110 // the migration flow after the IndexedDB migration. | |
111 base::Closure migrate_fs = | |
112 base::Bind(&MigrateFileSystem, old_partition, current_partition, | |
tzik
2015/01/16 05:56:36
This closure may run after the profile has gone.
| |
113 make_scoped_refptr(extension), reply); | |
114 | |
115 // Perform the IndexedDB migration on the old context's sequenced task | |
116 // runner. After completion, it should call MigrateFileSystem. | |
117 old_indexed_db_context->TaskRunner()->PostTaskAndReply( | |
118 FROM_HERE, base::Bind(&MigrateOnIndexedDBThread, | |
119 make_scoped_refptr(old_indexed_db_context), | |
120 make_scoped_refptr(indexed_db_context), | |
121 make_scoped_refptr(extension)), | |
122 migrate_fs); | |
123 } | |
124 | |
125 } // namespace | |
126 | |
127 namespace extensions { | |
128 | |
129 AppDataMigrator::AppDataMigrator(Profile* profile, ExtensionRegistry* registry) | |
130 : profile_(profile), registry_(registry) { | |
131 } | |
132 | |
133 bool AppDataMigrator::NeedsMigration(const Extension* old, | |
134 const Extension* extension) { | |
135 return old && old->is_legacy_packaged_app() && extension->is_platform_app(); | |
136 } | |
137 | |
138 void AppDataMigrator::DoMigrationAndReply(const Extension* old, | |
139 const Extension* extension, | |
140 const base::Closure& reply) { | |
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
142 DCHECK(NeedsMigration(old, extension)); | |
143 | |
144 // This should retrieve the general storage partition. | |
145 content::StoragePartition* old_partition = | |
146 BrowserContext::GetStoragePartitionForSite( | |
147 profile_, Extension::GetBaseURLFromExtensionId(extension->id())); | |
148 | |
149 // Enable the new extension so we can access its storage partition. | |
150 bool old_was_disabled = registry_->AddEnabled(extension); | |
151 | |
152 // This should create a new isolated partition for the new version of the | |
153 // extension. | |
154 StoragePartition* new_partition = BrowserContext::GetStoragePartitionForSite( | |
155 profile_, Extension::GetBaseURLFromExtensionId(extension->id())); | |
156 | |
157 // Now, restore the enabled/disabled state of the new and old extensions. | |
158 if (!old_was_disabled) { | |
cmumford
2015/01/06 15:30:26
No need for braces when scoping a single line of c
| |
159 registry_->AddEnabled(old); | |
160 } else { | |
161 registry_->RemoveEnabled(extension->id()); | |
162 } | |
163 | |
164 // Begin the actual migration flow. | |
cmumford
2015/01/06 15:30:26
Nit: does this comment add much?
| |
165 MigrateLegacyPartition(old_partition, new_partition, extension, reply); | |
166 } | |
167 | |
168 } // namespace extensions | |
OLD | NEW |