OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
benwells
2014/11/06 20:45:35
Nit: "Copyright 2014..."
| |
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::StoragePartition; | |
22 using storage::SandboxFileSystemBackendDelegate; | |
23 | |
24 namespace { | |
25 | |
26 void CopyFileSystem(SandboxFileSystemBackendDelegate* old_sandbox_delegate, | |
27 SandboxFileSystemBackendDelegate* sandbox_delegate, | |
28 const GURL& origin, | |
29 storage::FileSystemType type) { | |
30 CHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
31 | |
32 base::FilePath old_base_path = | |
33 old_sandbox_delegate->GetBaseDirectoryForOriginAndType( | |
34 origin, type, false); | |
35 | |
36 base::FilePath base_path = | |
37 sandbox_delegate->GetBaseDirectoryForOriginAndType(origin, type, true); | |
38 | |
39 base::CopyDirectory(old_base_path, base_path.DirName(), true); | |
40 } | |
41 | |
42 void MigrateLegacyPartition(StoragePartition* old_partition, | |
43 StoragePartition* current_partition, | |
44 const extensions::Extension* extension) { | |
45 CHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
46 | |
47 GURL extension_url = | |
48 extensions::Extension::GetBaseURLFromExtensionId(extension->id()); | |
49 | |
50 content::IndexedDBContext* indexed_db_context = | |
51 current_partition->GetIndexedDBContext(); | |
52 content::IndexedDBContext* old_indexed_db_context = | |
53 old_partition->GetIndexedDBContext(); | |
54 | |
55 base::FilePath old_db_path = | |
56 old_indexed_db_context->GetFilePath(extension_url); | |
57 base::FilePath current_db_path = | |
58 indexed_db_context->GetFilePath(extension_url); | |
59 | |
60 if (base::PathExists(old_db_path) && !base::PathExists(current_db_path)) { | |
61 base::CopyDirectory(old_db_path, current_db_path.DirName(), true); | |
62 } | |
63 | |
64 storage::FileSystemContext* old_fs_context = | |
65 old_partition->GetFileSystemContext(); | |
66 storage::FileSystemContext* fs_context = | |
67 current_partition->GetFileSystemContext(); | |
68 | |
69 SandboxFileSystemBackendDelegate* old_sandbox_delegate = | |
70 old_fs_context->sandbox_delegate(); | |
71 SandboxFileSystemBackendDelegate* sandbox_delegate = | |
72 fs_context->sandbox_delegate(); | |
73 | |
74 scoped_ptr<storage::SandboxFileSystemBackendDelegate::OriginEnumerator> | |
75 enumerator(old_sandbox_delegate->CreateOriginEnumerator()); | |
76 | |
77 GURL origin; | |
78 do { | |
79 origin = enumerator->Next(); | |
80 } while (origin != extension_url && !origin.is_empty()); | |
81 | |
82 if (!origin.is_empty()) { | |
83 if (enumerator->HasFileSystemType(storage::kFileSystemTypeTemporary)) { | |
84 CopyFileSystem(old_sandbox_delegate, | |
85 sandbox_delegate, | |
86 extension_url, | |
87 storage::kFileSystemTypeTemporary); | |
88 } | |
89 if (enumerator->HasFileSystemType(storage::kFileSystemTypePersistent)) { | |
90 CopyFileSystem(old_sandbox_delegate, | |
91 sandbox_delegate, | |
92 extension_url, | |
93 storage::kFileSystemTypePersistent); | |
94 } | |
95 } | |
96 } | |
97 | |
98 } // namespace | |
99 | |
100 namespace extensions { | |
101 | |
102 AppDataMigrator::AppDataMigrator(Profile* profile, ExtensionRegistry* registry) | |
103 : profile_(profile), registry_(registry) { | |
104 } | |
105 | |
106 bool AppDataMigrator::NeedsMigration(const Extension* old, | |
107 const Extension* extension) { | |
108 return old && old->is_legacy_packaged_app() && extension->is_platform_app(); | |
109 } | |
110 | |
111 void AppDataMigrator::DoMigrationAndReply(const Extension* old, | |
112 const Extension* extension, | |
113 const base::Closure& reply) { | |
114 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
115 // juggle the enabled status of the new and old extensions so we can get | |
benwells
2014/11/06 20:45:35
Uber nit: Capital J and a full stop at the end of
| |
116 // the old and new storage partitions | |
117 content::StoragePartition* old_partition = | |
118 BrowserContext::GetStoragePartitionForSite( | |
119 profile_, Extension::GetBaseURLFromExtensionId(extension->id())); | |
120 | |
121 bool oldWasDisabled = registry_->AddEnabled(extension); | |
benwells
2014/11/06 20:45:35
Could you add a comment explaining what is happeni
| |
122 StoragePartition* new_partition = BrowserContext::GetStoragePartitionForSite( | |
123 profile_, Extension::GetBaseURLFromExtensionId(extension->id())); | |
124 | |
125 if (!oldWasDisabled) { | |
126 registry_->AddEnabled(old); | |
127 } else { | |
128 registry_->RemoveEnabled(extension->id()); | |
129 } | |
130 | |
131 BrowserThread::PostBlockingPoolTaskAndReply( | |
132 FROM_HERE, | |
133 base::Bind(&MigrateLegacyPartition, | |
134 old_partition, | |
135 new_partition, | |
136 make_scoped_refptr(extension)), | |
137 reply); | |
138 } | |
139 | |
140 } // namespace extensions | |
OLD | NEW |