Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / fileapi / iphoto_file_util.cc
index 38ae639..9e07b6e 100644 (file)
@@ -47,21 +47,6 @@ bool ContainsElement(const std::vector<T>& collection, const T& key) {
   return false;
 }
 
-std::vector<std::string> GetVirtualPathComponents(
-    const fileapi::FileSystemURL& url) {
-  ImportedMediaGalleryRegistry* imported_registry =
-      ImportedMediaGalleryRegistry::GetInstance();
-  base::FilePath root = imported_registry->ImportedRoot().AppendASCII("iphoto");
-
-  DCHECK(root.IsParent(url.path()) || root == url.path());
-  base::FilePath virtual_path;
-  root.AppendRelativePath(url.path(), &virtual_path);
-
-  std::vector<std::string> result;
-  fileapi::VirtualPath::GetComponentsUTF8Unsafe(virtual_path, &result);
-  return result;
-}
-
 }  // namespace
 
 const char kIPhotoAlbumsDir[] = "Albums";
@@ -80,30 +65,52 @@ void IPhotoFileUtil::GetFileInfoOnTaskRunnerThread(
     scoped_ptr<fileapi::FileSystemOperationContext> context,
     const fileapi::FileSystemURL& url,
     const GetFileInfoCallback& callback) {
-  GetDataProvider()->RefreshData(
-      base::Bind(&IPhotoFileUtil::GetFileInfoWithFreshDataProvider,
-                 weak_factory_.GetWeakPtr(), base::Passed(&context), url,
-                 callback));
+  IPhotoDataProvider* data_provider = GetDataProvider();
+  // |data_provider| may be NULL if the file system was revoked before this
+  // operation had a chance to run.
+  if (!data_provider) {
+    GetFileInfoWithFreshDataProvider(context.Pass(), url, callback, false);
+  } else {
+    data_provider->RefreshData(
+        base::Bind(&IPhotoFileUtil::GetFileInfoWithFreshDataProvider,
+                   weak_factory_.GetWeakPtr(), base::Passed(&context), url,
+                   callback));
+  }
 }
 
 void IPhotoFileUtil::ReadDirectoryOnTaskRunnerThread(
     scoped_ptr<fileapi::FileSystemOperationContext> context,
     const fileapi::FileSystemURL& url,
     const ReadDirectoryCallback& callback) {
-  GetDataProvider()->RefreshData(
-      base::Bind(&IPhotoFileUtil::ReadDirectoryWithFreshDataProvider,
-                 weak_factory_.GetWeakPtr(), base::Passed(&context), url,
-                 callback));
+  IPhotoDataProvider* data_provider = GetDataProvider();
+  // |data_provider| may be NULL if the file system was revoked before this
+  // operation had a chance to run.
+  if (!data_provider) {
+    ReadDirectoryWithFreshDataProvider(context.Pass(), url, callback, false);
+  } else {
+    data_provider->RefreshData(
+        base::Bind(&IPhotoFileUtil::ReadDirectoryWithFreshDataProvider,
+                   weak_factory_.GetWeakPtr(), base::Passed(&context), url,
+                   callback));
+  }
 }
 
 void IPhotoFileUtil::CreateSnapshotFileOnTaskRunnerThread(
     scoped_ptr<fileapi::FileSystemOperationContext> context,
     const fileapi::FileSystemURL& url,
     const CreateSnapshotFileCallback& callback) {
-  GetDataProvider()->RefreshData(
-      base::Bind(&IPhotoFileUtil::CreateSnapshotFileWithFreshDataProvider,
-                 weak_factory_.GetWeakPtr(), base::Passed(&context), url,
-                 callback));
+  IPhotoDataProvider* data_provider = GetDataProvider();
+  // |data_provider| may be NULL if the file system was revoked before this
+  // operation had a chance to run.
+  if (!data_provider) {
+    CreateSnapshotFileWithFreshDataProvider(context.Pass(), url, callback,
+                                            false);
+  } else {
+    data_provider->RefreshData(
+        base::Bind(&IPhotoFileUtil::CreateSnapshotFileWithFreshDataProvider,
+                   weak_factory_.GetWeakPtr(), base::Passed(&context), url,
+                   callback));
+  }
 }
 
 void IPhotoFileUtil::GetFileInfoWithFreshDataProvider(
@@ -171,7 +178,8 @@ base::File::Error IPhotoFileUtil::GetFileInfoSync(
     const fileapi::FileSystemURL& url,
     base::File::Info* file_info,
     base::FilePath* platform_path) {
-  std::vector<std::string> components = GetVirtualPathComponents(url);
+  std::vector<std::string> components;
+  fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components);
 
   if (components.size() == 0) {
     return MakeDirectoryFileInfo(file_info);
@@ -220,7 +228,8 @@ base::File::Error IPhotoFileUtil::ReadDirectorySync(
     const fileapi::FileSystemURL& url,
     EntryList* file_list) {
   DCHECK(file_list->empty());
-  std::vector<std::string> components = GetVirtualPathComponents(url);
+  std::vector<std::string> components;
+  fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components);
 
   // Root directory. Child is the /Albums dir.
   if (components.size() == 0) {
@@ -303,7 +312,8 @@ base::File::Error IPhotoFileUtil::GetLocalFilePath(
     fileapi::FileSystemOperationContext* context,
     const fileapi::FileSystemURL& url,
     base::FilePath* local_file_path) {
-  std::vector<std::string> components = GetVirtualPathComponents(url);
+  std::vector<std::string> components;
+  fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components);
 
   if (components.size() == 3 && components[0] == kIPhotoAlbumsDir) {
     base::FilePath location = GetDataProvider()->GetPhotoLocationInAlbum(