- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / fileapi / iphoto_file_util.cc
1 // Copyright 2013 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/media_galleries/fileapi/iphoto_file_util.h"
6
7 #include <set>
8 #include <string>
9 #include <vector>
10
11 #include "base/bind_helpers.h"
12 #include "base/file_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/media_galleries/fileapi/iphoto_data_provider.h"
15 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h"
16 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "webkit/browser/fileapi/file_system_operation_context.h"
19 #include "webkit/browser/fileapi/file_system_url.h"
20 #include "webkit/browser/fileapi/native_file_util.h"
21 #include "webkit/common/blob/shareable_file_reference.h"
22 #include "webkit/common/fileapi/file_system_util.h"
23
24 namespace iphoto {
25
26 namespace {
27
28 base::PlatformFileError MakeDirectoryFileInfo(
29     base::PlatformFileInfo* file_info) {
30   base::PlatformFileInfo result;
31   result.is_directory = true;
32   *file_info = result;
33   return base::PLATFORM_FILE_OK;
34 }
35
36 }  // namespace
37
38 IPhotoFileUtil::IPhotoFileUtil(MediaPathFilter* media_path_filter)
39     : NativeMediaFileUtil(media_path_filter),
40       weak_factory_(this),
41       imported_registry_(NULL) {
42 }
43
44 IPhotoFileUtil::~IPhotoFileUtil() {
45 }
46
47 void IPhotoFileUtil::GetFileInfoOnTaskRunnerThread(
48     scoped_ptr<fileapi::FileSystemOperationContext> context,
49     const fileapi::FileSystemURL& url,
50     const GetFileInfoCallback& callback) {
51   GetDataProvider()->RefreshData(
52       base::Bind(&IPhotoFileUtil::GetFileInfoWithFreshDataProvider,
53                  weak_factory_.GetWeakPtr(), base::Passed(&context), url,
54                  callback));
55 }
56
57 void IPhotoFileUtil::ReadDirectoryOnTaskRunnerThread(
58     scoped_ptr<fileapi::FileSystemOperationContext> context,
59     const fileapi::FileSystemURL& url,
60     const ReadDirectoryCallback& callback) {
61   GetDataProvider()->RefreshData(
62       base::Bind(&IPhotoFileUtil::ReadDirectoryWithFreshDataProvider,
63                  weak_factory_.GetWeakPtr(), base::Passed(&context), url,
64                  callback));
65 }
66
67 void IPhotoFileUtil::CreateSnapshotFileOnTaskRunnerThread(
68     scoped_ptr<fileapi::FileSystemOperationContext> context,
69     const fileapi::FileSystemURL& url,
70     const CreateSnapshotFileCallback& callback) {
71   GetDataProvider()->RefreshData(
72       base::Bind(&IPhotoFileUtil::CreateSnapshotFileWithFreshDataProvider,
73                  weak_factory_.GetWeakPtr(), base::Passed(&context), url,
74                  callback));
75 }
76
77 void IPhotoFileUtil::GetFileInfoWithFreshDataProvider(
78     scoped_ptr<fileapi::FileSystemOperationContext> context,
79     const fileapi::FileSystemURL& url,
80     const GetFileInfoCallback& callback,
81     bool valid_parse) {
82   if (!valid_parse) {
83     if (!callback.is_null()) {
84       content::BrowserThread::PostTask(
85           content::BrowserThread::IO,
86           FROM_HERE,
87           base::Bind(callback, base::PLATFORM_FILE_ERROR_IO,
88                      base::PlatformFileInfo()));
89     }
90     return;
91   }
92   NativeMediaFileUtil::GetFileInfoOnTaskRunnerThread(context.Pass(), url,
93                                                      callback);
94 }
95
96 void IPhotoFileUtil::ReadDirectoryWithFreshDataProvider(
97     scoped_ptr<fileapi::FileSystemOperationContext> context,
98     const fileapi::FileSystemURL& url,
99     const ReadDirectoryCallback& callback,
100     bool valid_parse) {
101   if (!valid_parse) {
102     if (!callback.is_null()) {
103       content::BrowserThread::PostTask(
104           content::BrowserThread::IO,
105           FROM_HERE,
106           base::Bind(callback, base::PLATFORM_FILE_ERROR_IO, EntryList(),
107                      false));
108     }
109     return;
110   }
111   NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread(context.Pass(), url,
112                                                        callback);
113 }
114
115 void IPhotoFileUtil::CreateSnapshotFileWithFreshDataProvider(
116     scoped_ptr<fileapi::FileSystemOperationContext> context,
117     const fileapi::FileSystemURL& url,
118     const CreateSnapshotFileCallback& callback,
119     bool valid_parse) {
120   if (!valid_parse) {
121     if (!callback.is_null()) {
122       base::PlatformFileInfo file_info;
123       base::FilePath platform_path;
124       scoped_refptr<webkit_blob::ShareableFileReference> file_ref;
125       content::BrowserThread::PostTask(
126           content::BrowserThread::IO,
127           FROM_HERE,
128           base::Bind(callback, base::PLATFORM_FILE_ERROR_IO, file_info,
129                      platform_path, file_ref));
130     }
131     return;
132   }
133   NativeMediaFileUtil::CreateSnapshotFileOnTaskRunnerThread(context.Pass(), url,
134                                                             callback);
135 }
136
137 // TODO(gbillock): Right now, returns an always-empty FS. Document virtual FS.
138 base::PlatformFileError IPhotoFileUtil::GetFileInfoSync(
139     fileapi::FileSystemOperationContext* context,
140     const fileapi::FileSystemURL& url,
141     base::PlatformFileInfo* file_info,
142     base::FilePath* platform_path) {
143   std::vector<std::string> components;
144   fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components);
145
146   if (components.size() == 0)
147     return MakeDirectoryFileInfo(file_info);
148
149   return base::PLATFORM_FILE_ERROR_NOT_FOUND;
150 }
151
152 base::PlatformFileError IPhotoFileUtil::ReadDirectorySync(
153     fileapi::FileSystemOperationContext* context,
154     const fileapi::FileSystemURL& url,
155     EntryList* file_list) {
156   DCHECK(file_list->empty());
157   std::vector<std::string> components;
158   fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components);
159
160   if (components.size() == 0) {
161     return base::PLATFORM_FILE_OK;
162   }
163
164   return base::PLATFORM_FILE_ERROR_NOT_FOUND;
165 }
166
167 base::PlatformFileError IPhotoFileUtil::CreateSnapshotFileSync(
168     fileapi::FileSystemOperationContext* context,
169     const fileapi::FileSystemURL& url,
170     base::PlatformFileInfo* file_info,
171     base::FilePath* platform_path,
172     scoped_refptr<webkit_blob::ShareableFileReference>* file_ref) {
173   DCHECK(!url.path().IsAbsolute());
174
175   return base::PLATFORM_FILE_ERROR_NOT_FOUND;
176 }
177
178 base::PlatformFileError IPhotoFileUtil::GetLocalFilePath(
179     fileapi::FileSystemOperationContext* context,
180     const fileapi::FileSystemURL& url,
181     base::FilePath* local_file_path) {
182   return base::PLATFORM_FILE_ERROR_NOT_FOUND;
183 }
184
185 IPhotoDataProvider* IPhotoFileUtil::GetDataProvider() {
186   if (!imported_registry_)
187     imported_registry_ = ImportedMediaGalleryRegistry::GetInstance();
188   return imported_registry_->IPhotoDataProvider();
189 }
190
191 }  // namespace iphoto