Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: chrome/browser/extensions/app_data_migrator.cc

Issue 671873004: Migrates legacy packaged app data when it's upgraded to a platform app (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments and adds tests Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 MigrateOnFileSystemThread(
29 SandboxFileSystemBackendDelegate* old_sandbox_delegate,
30 SandboxFileSystemBackendDelegate* sandbox_delegate,
31 const extensions::Extension* extension) {
32 CHECK(old_sandbox_delegate->file_task_runner()->RunsTasksOnCurrentThread());
33
34 GURL extension_url =
35 extensions::Extension::GetBaseURLFromExtensionId(extension->id());
36
37 scoped_ptr<storage::SandboxFileSystemBackendDelegate::OriginEnumerator>
38 enumerator(old_sandbox_delegate->CreateOriginEnumerator());
39
40 // Find out if there is a file system that needs migration.
41 GURL origin;
42 do {
43 origin = enumerator->Next();
44 } while (origin != extension_url && !origin.is_empty());
45
46 if (!origin.is_empty()) {
47 // Copy the temporary file system.
48 if (enumerator->HasFileSystemType(storage::kFileSystemTypeTemporary)) {
49 old_sandbox_delegate->CopyFileSystem(
50 extension_url, storage::kFileSystemTypeTemporary, sandbox_delegate);
51 }
52 // Copy the persistent file system.
53 if (enumerator->HasFileSystemType(storage::kFileSystemTypePersistent)) {
54 old_sandbox_delegate->CopyFileSystem(
55 extension_url, storage::kFileSystemTypePersistent, sandbox_delegate);
56 }
57 }
58 }
59
60 void MigrateOnIndexedDBThread(IndexedDBContext* old_indexed_db_context,
61 IndexedDBContext* indexed_db_context,
62 const extensions::Extension* extension) {
63 CHECK(old_indexed_db_context->TaskRunner()->RunsTasksOnCurrentThread());
64
65 GURL extension_url =
66 extensions::Extension::GetBaseURLFromExtensionId(extension->id());
67
68 old_indexed_db_context->CopyOriginData(extension_url, indexed_db_context);
69 }
tzik 2014/12/03 08:35:49 nit: insert empty line here?
70 void MigrateFileSystem(StoragePartition* old_partition,
71 StoragePartition* current_partition,
72 const extensions::Extension* extension,
73 const base::Closure& reply) {
74 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75
76 // Retrieve the fs contexts and sandbox delegates in preparation for
77 // migration.
78 FileSystemContext* old_fs_context = old_partition->GetFileSystemContext();
79 FileSystemContext* fs_context = current_partition->GetFileSystemContext();
tzik 2014/12/03 08:35:49 It is not clear that these FileSystemContexts outl
80
81 SandboxFileSystemBackendDelegate* old_sandbox_delegate =
82 old_fs_context->sandbox_delegate();
83 SandboxFileSystemBackendDelegate* sandbox_delegate =
84 fs_context->sandbox_delegate();
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_sandbox_delegate->file_task_runner()->PostTaskAndReply(
91 FROM_HERE,
92 base::Bind(&MigrateOnFileSystemThread,
93 old_sandbox_delegate,
94 sandbox_delegate,
95 make_scoped_refptr(extension)),
96 reply);
97 }
tzik 2014/12/03 08:35:49 ditto
98 void MigrateLegacyPartition(StoragePartition* old_partition,
99 StoragePartition* current_partition,
100 const extensions::Extension* extension,
101 const base::Closure& reply) {
102 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
103
104 // Retrieve both IndexedDB contexts in preparation for migration.
105 IndexedDBContext* indexed_db_context =
106 current_partition->GetIndexedDBContext();
107 IndexedDBContext* old_indexed_db_context =
108 old_partition->GetIndexedDBContext();
109
110 // Create a closure for the file system migration. This is the next step in
111 // the migration flow after the IndexedDB migration.
112 base::Closure migrateFs = base::Bind(&MigrateFileSystem,
tzik 2014/12/03 08:35:49 s/migrateFs/migrate_fs/?
113 old_partition,
114 current_partition,
115 make_scoped_refptr(extension),
116 reply);
117
118 // Perform the IndexedDB migration on the old context's sequenced task
119 // runner. After completion, it should call MigrateFileSystem.
120 old_indexed_db_context->TaskRunner()->PostTaskAndReply(
121 FROM_HERE,
122 base::Bind(&MigrateOnIndexedDBThread,
123 old_indexed_db_context,
124 indexed_db_context,
125 make_scoped_refptr(extension)),
126 migrateFs);
127 }
128
129 } // namespace
130
131 namespace extensions {
132
133 AppDataMigrator::AppDataMigrator(Profile* profile, ExtensionRegistry* registry)
134 : profile_(profile), registry_(registry) {
135 }
136
137 bool AppDataMigrator::NeedsMigration(const Extension* old,
138 const Extension* extension) {
139 return old && old->is_legacy_packaged_app() && extension->is_platform_app();
140 }
141
142 void AppDataMigrator::DoMigrationAndReply(const Extension* old,
143 const Extension* extension,
144 const base::Closure& reply) {
145 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
146
147 // This should retrieve the general storage partition.
148 content::StoragePartition* old_partition =
149 BrowserContext::GetStoragePartitionForSite(
150 profile_, Extension::GetBaseURLFromExtensionId(extension->id()));
151
152 // Enable the new extension so we can access its storage partition.
153 bool oldWasDisabled = registry_->AddEnabled(extension);
tzik 2014/12/03 08:35:49 s/oldWasDisabled/old_was_disabled/?
154
155 // This should create a new isolated partition for the new version of the
156 // extension.
157 StoragePartition* new_partition = BrowserContext::GetStoragePartitionForSite(
158 profile_, Extension::GetBaseURLFromExtensionId(extension->id()));
159
160 // Now, restore the enabled/disabled state of the new and old extensions.
161 if (!oldWasDisabled) {
162 registry_->AddEnabled(old);
163 } else {
164 registry_->RemoveEnabled(extension->id());
165 }
166
167 // Begin the actual migration flow.
168 MigrateLegacyPartition(old_partition, new_partition, extension, reply);
169 }
170
171 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698