Index: chrome/browser/extensions/app_data_migrator.cc |
diff --git a/chrome/browser/extensions/app_data_migrator.cc b/chrome/browser/extensions/app_data_migrator.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..57883094f805568d71ba0a3bec3b78c2e486d80b |
--- /dev/null |
+++ b/chrome/browser/extensions/app_data_migrator.cc |
@@ -0,0 +1,140 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
benwells
2014/11/06 20:45:35
Nit: "Copyright 2014..."
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/app_data_migrator.h" |
+ |
+#include "base/files/file_util.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "content/public/browser/browser_context.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/indexed_db_context.h" |
+#include "content/public/browser/storage_partition.h" |
+#include "extensions/browser/extension_registry.h" |
+#include "extensions/common/extension.h" |
+#include "storage/browser/fileapi/file_system_context.h" |
+#include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h" |
+#include "storage/common/fileapi/file_system_types.h" |
+ |
+using content::BrowserContext; |
+using content::BrowserThread; |
+using content::StoragePartition; |
+using storage::SandboxFileSystemBackendDelegate; |
+ |
+namespace { |
+ |
+void CopyFileSystem(SandboxFileSystemBackendDelegate* old_sandbox_delegate, |
+ SandboxFileSystemBackendDelegate* sandbox_delegate, |
+ const GURL& origin, |
+ storage::FileSystemType type) { |
+ CHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
+ |
+ base::FilePath old_base_path = |
+ old_sandbox_delegate->GetBaseDirectoryForOriginAndType( |
+ origin, type, false); |
+ |
+ base::FilePath base_path = |
+ sandbox_delegate->GetBaseDirectoryForOriginAndType(origin, type, true); |
+ |
+ base::CopyDirectory(old_base_path, base_path.DirName(), true); |
+} |
+ |
+void MigrateLegacyPartition(StoragePartition* old_partition, |
+ StoragePartition* current_partition, |
+ const extensions::Extension* extension) { |
+ CHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
+ |
+ GURL extension_url = |
+ extensions::Extension::GetBaseURLFromExtensionId(extension->id()); |
+ |
+ content::IndexedDBContext* indexed_db_context = |
+ current_partition->GetIndexedDBContext(); |
+ content::IndexedDBContext* old_indexed_db_context = |
+ old_partition->GetIndexedDBContext(); |
+ |
+ base::FilePath old_db_path = |
+ old_indexed_db_context->GetFilePath(extension_url); |
+ base::FilePath current_db_path = |
+ indexed_db_context->GetFilePath(extension_url); |
+ |
+ if (base::PathExists(old_db_path) && !base::PathExists(current_db_path)) { |
+ base::CopyDirectory(old_db_path, current_db_path.DirName(), true); |
+ } |
+ |
+ storage::FileSystemContext* old_fs_context = |
+ old_partition->GetFileSystemContext(); |
+ storage::FileSystemContext* fs_context = |
+ current_partition->GetFileSystemContext(); |
+ |
+ SandboxFileSystemBackendDelegate* old_sandbox_delegate = |
+ old_fs_context->sandbox_delegate(); |
+ SandboxFileSystemBackendDelegate* sandbox_delegate = |
+ fs_context->sandbox_delegate(); |
+ |
+ scoped_ptr<storage::SandboxFileSystemBackendDelegate::OriginEnumerator> |
+ enumerator(old_sandbox_delegate->CreateOriginEnumerator()); |
+ |
+ GURL origin; |
+ do { |
+ origin = enumerator->Next(); |
+ } while (origin != extension_url && !origin.is_empty()); |
+ |
+ if (!origin.is_empty()) { |
+ if (enumerator->HasFileSystemType(storage::kFileSystemTypeTemporary)) { |
+ CopyFileSystem(old_sandbox_delegate, |
+ sandbox_delegate, |
+ extension_url, |
+ storage::kFileSystemTypeTemporary); |
+ } |
+ if (enumerator->HasFileSystemType(storage::kFileSystemTypePersistent)) { |
+ CopyFileSystem(old_sandbox_delegate, |
+ sandbox_delegate, |
+ extension_url, |
+ storage::kFileSystemTypePersistent); |
+ } |
+ } |
+} |
+ |
+} // namespace |
+ |
+namespace extensions { |
+ |
+AppDataMigrator::AppDataMigrator(Profile* profile, ExtensionRegistry* registry) |
+ : profile_(profile), registry_(registry) { |
+} |
+ |
+bool AppDataMigrator::NeedsMigration(const Extension* old, |
+ const Extension* extension) { |
+ return old && old->is_legacy_packaged_app() && extension->is_platform_app(); |
+} |
+ |
+void AppDataMigrator::DoMigrationAndReply(const Extension* old, |
+ const Extension* extension, |
+ const base::Closure& reply) { |
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ // 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
|
+ // the old and new storage partitions |
+ content::StoragePartition* old_partition = |
+ BrowserContext::GetStoragePartitionForSite( |
+ profile_, Extension::GetBaseURLFromExtensionId(extension->id())); |
+ |
+ bool oldWasDisabled = registry_->AddEnabled(extension); |
benwells
2014/11/06 20:45:35
Could you add a comment explaining what is happeni
|
+ StoragePartition* new_partition = BrowserContext::GetStoragePartitionForSite( |
+ profile_, Extension::GetBaseURLFromExtensionId(extension->id())); |
+ |
+ if (!oldWasDisabled) { |
+ registry_->AddEnabled(old); |
+ } else { |
+ registry_->RemoveEnabled(extension->id()); |
+ } |
+ |
+ BrowserThread::PostBlockingPoolTaskAndReply( |
+ FROM_HERE, |
+ base::Bind(&MigrateLegacyPartition, |
+ old_partition, |
+ new_partition, |
+ make_scoped_refptr(extension)), |
+ reply); |
+} |
+ |
+} // namespace extensions |