summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media_galleries/fileapi/iphoto_file_util.cc
blob: f35595720bd872d3cacf83abd4a41e353d5fc614 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/media_galleries/fileapi/iphoto_file_util.h"

#include <set>
#include <string>
#include <vector>

#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/media_galleries/fileapi/iphoto_data_provider.h"
#include "chrome/browser/media_galleries/fileapi/media_path_filter.h"
#include "chrome/browser/media_galleries/imported_media_gallery_registry.h"
#include "content/public/browser/browser_thread.h"
#include "webkit/browser/fileapi/file_system_operation_context.h"
#include "webkit/browser/fileapi/file_system_url.h"
#include "webkit/browser/fileapi/native_file_util.h"
#include "webkit/common/blob/shareable_file_reference.h"
#include "webkit/common/fileapi/directory_entry.h"
#include "webkit/common/fileapi/file_system_util.h"

using fileapi::DirectoryEntry;

namespace iphoto {

namespace {

base::File::Error MakeDirectoryFileInfo(base::File::Info* file_info) {
  base::File::Info result;
  result.is_directory = true;
  *file_info = result;
  return base::File::FILE_OK;
}

template <typename T>
bool ContainsElement(const std::vector<T>& collection, const T& key) {
  typename std::vector<T>::const_iterator it = collection.begin();
  while (it != collection.end()) {
    if (*it == key)
      return true;
    it++;
  }
  return false;
}

}  // namespace

const char kIPhotoAlbumsDir[] = "Albums";
const char kIPhotoOriginalsDir[] = "Originals";

IPhotoFileUtil::IPhotoFileUtil(MediaPathFilter* media_path_filter)
    : NativeMediaFileUtil(media_path_filter),
      weak_factory_(this),
      imported_registry_(NULL) {
}

IPhotoFileUtil::~IPhotoFileUtil() {
}

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));
}

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));
}

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));
}

void IPhotoFileUtil::GetFileInfoWithFreshDataProvider(
    scoped_ptr<fileapi::FileSystemOperationContext> context,
    const fileapi::FileSystemURL& url,
    const GetFileInfoCallback& callback,
    bool valid_parse) {
  if (!valid_parse) {
    if (!callback.is_null()) {
      content::BrowserThread::PostTask(
          content::BrowserThread::IO,
          FROM_HERE,
          base::Bind(callback, base::File::FILE_ERROR_IO, base::File::Info()));
    }
    return;
  }
  NativeMediaFileUtil::GetFileInfoOnTaskRunnerThread(context.Pass(), url,
                                                     callback);
}

void IPhotoFileUtil::ReadDirectoryWithFreshDataProvider(
    scoped_ptr<fileapi::FileSystemOperationContext> context,
    const fileapi::FileSystemURL& url,
    const ReadDirectoryCallback& callback,
    bool valid_parse) {
  if (!valid_parse) {
    if (!callback.is_null()) {
      content::BrowserThread::PostTask(
          content::BrowserThread::IO,
          FROM_HERE,
          base::Bind(callback, base::File::FILE_ERROR_IO, EntryList(), false));
    }
    return;
  }
  NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread(context.Pass(), url,
                                                       callback);
}

void IPhotoFileUtil::CreateSnapshotFileWithFreshDataProvider(
    scoped_ptr<fileapi::FileSystemOperationContext> context,
    const fileapi::FileSystemURL& url,
    const CreateSnapshotFileCallback& callback,
    bool valid_parse) {
  if (!valid_parse) {
    if (!callback.is_null()) {
      base::File::Info file_info;
      base::FilePath platform_path;
      scoped_refptr<webkit_blob::ShareableFileReference> file_ref;
      content::BrowserThread::PostTask(
          content::BrowserThread::IO,
          FROM_HERE,
          base::Bind(callback, base::File::FILE_ERROR_IO, file_info,
                     platform_path, file_ref));
    }
    return;
  }
  NativeMediaFileUtil::CreateSnapshotFileOnTaskRunnerThread(context.Pass(), url,
                                                            callback);
}

// Begin actual implementation.

base::File::Error IPhotoFileUtil::GetFileInfoSync(
    fileapi::FileSystemOperationContext* context,
    const fileapi::FileSystemURL& url,
    base::File::Info* file_info,
    base::FilePath* platform_path) {
  std::vector<std::string> components;
  fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components);

  if (components.size() == 0) {
    return MakeDirectoryFileInfo(file_info);
  }

  // The 'Albums' directory.
  if (components[0] == kIPhotoAlbumsDir) {
    if (components.size() == 1) {
      return MakeDirectoryFileInfo(file_info);
    } else if (components.size() == 2) {
      std::vector<std::string> albums =
          GetDataProvider()->GetAlbumNames();
      if (ContainsElement(albums, components[1]))
        return MakeDirectoryFileInfo(file_info);
    } else if (components.size() == 3) {
      if (components[2] == kIPhotoOriginalsDir) {
        if (GetDataProvider()->HasOriginals(components[1]))
          return MakeDirectoryFileInfo(file_info);
        else
          return base::File::FILE_ERROR_NOT_FOUND;
      }

      base::FilePath location = GetDataProvider()->GetPhotoLocationInAlbum(
          components[1], components[2]);
      if (!location.empty()) {
        return NativeMediaFileUtil::GetFileInfoSync(
            context, url, file_info, platform_path);
      }
    } else if (components.size() == 4 &&
               GetDataProvider()->HasOriginals(components[1]) &&
               components[2] == kIPhotoOriginalsDir) {
      base::FilePath location = GetDataProvider()->GetOriginalPhotoLocation(
          components[1], components[3]);
      if (!location.empty()) {
        return NativeMediaFileUtil::GetFileInfoSync(
            context, url, file_info, platform_path);
      }
    }
  }

  return base::File::FILE_ERROR_NOT_FOUND;
}

base::File::Error IPhotoFileUtil::ReadDirectorySync(
    fileapi::FileSystemOperationContext* context,
    const fileapi::FileSystemURL& url,
    EntryList* file_list) {
  DCHECK(file_list->empty());
  std::vector<std::string> components;
  fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components);

  // Root directory. Child is the /Albums dir.
  if (components.size() == 0) {
    file_list->push_back(DirectoryEntry(kIPhotoAlbumsDir,
                                        DirectoryEntry::DIRECTORY,
                                        0, base::Time()));
    return base::File::FILE_OK;
  }

  if (components[0] == kIPhotoAlbumsDir) {
    if (components.size() == 1) {
      // Albums dir contains all album names.
      std::vector<std::string> albums =
          GetDataProvider()->GetAlbumNames();
      for (std::vector<std::string>::const_iterator it = albums.begin();
           it != albums.end(); it++) {
        file_list->push_back(DirectoryEntry(*it, DirectoryEntry::DIRECTORY,
                                            0, base::Time()));
      }
      return base::File::FILE_OK;
    } else if (components.size() == 2) {
      std::vector<std::string> albums =
          GetDataProvider()->GetAlbumNames();
      if (!ContainsElement(albums, components[1]))
        return base::File::FILE_ERROR_NOT_FOUND;

      // Album dirs contain all photos in them.
      if (GetDataProvider()->HasOriginals(components[1])) {
        file_list->push_back(DirectoryEntry(kIPhotoOriginalsDir,
                                            DirectoryEntry::DIRECTORY,
                                            0, base::Time()));
      }
      std::map<std::string, base::FilePath> locations =
          GetDataProvider()->GetAlbumContents(components[1]);
      for (std::map<std::string, base::FilePath>::const_iterator it =
               locations.begin();
           it != locations.end(); it++) {
        base::File::Info info;
        if (!base::GetFileInfo(it->second, &info))
          return base::File::FILE_ERROR_IO;
        file_list->push_back(DirectoryEntry(it->first, DirectoryEntry::FILE,
                                            info.size, info.last_modified));
      }
      return base::File::FILE_OK;
    } else if (components.size() == 3 &&
               components[2] == kIPhotoOriginalsDir &&
               GetDataProvider()->HasOriginals(components[1])) {
      std::map<std::string, base::FilePath> originals =
          GetDataProvider()->GetOriginals(components[1]);
      for (std::map<std::string, base::FilePath>::const_iterator it =
               originals.begin();
           it != originals.end(); it++) {
        base::File::Info info;
        if (!base::GetFileInfo(it->second, &info))
          return base::File::FILE_ERROR_IO;
        file_list->push_back(DirectoryEntry(it->first, DirectoryEntry::FILE,
                                            info.size, info.last_modified));
      }
      return base::File::FILE_OK;
    }
  }

  return base::File::FILE_ERROR_NOT_FOUND;
}

base::File::Error IPhotoFileUtil::DeleteDirectorySync(
    fileapi::FileSystemOperationContext* context,
    const fileapi::FileSystemURL& url) {
  return base::File::FILE_ERROR_SECURITY;
}

base::File::Error IPhotoFileUtil::DeleteFileSync(
    fileapi::FileSystemOperationContext* context,
    const fileapi::FileSystemURL& url) {
  return base::File::FILE_ERROR_SECURITY;
}


base::File::Error IPhotoFileUtil::GetLocalFilePath(
    fileapi::FileSystemOperationContext* context,
    const fileapi::FileSystemURL& url,
    base::FilePath* local_file_path) {
  std::vector<std::string> components;
  fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components);

  if (components.size() == 3 && components[0] == kIPhotoAlbumsDir) {
    base::FilePath location = GetDataProvider()->GetPhotoLocationInAlbum(
        components[1], components[2]);
    if (!location.empty()) {
      *local_file_path = location;
      return base::File::FILE_OK;
    }
  }

  if (components.size() == 4 && components[0] == kIPhotoAlbumsDir &&
      GetDataProvider()->HasOriginals(components[1]) &&
      components[2] == kIPhotoOriginalsDir) {
    base::FilePath location = GetDataProvider()->GetOriginalPhotoLocation(
        components[1], components[3]);
    if (!location.empty()) {
      *local_file_path = location;
      return base::File::FILE_OK;
    }
  }

  return base::File::FILE_ERROR_NOT_FOUND;
}

IPhotoDataProvider* IPhotoFileUtil::GetDataProvider() {
  if (!imported_registry_)
    imported_registry_ = ImportedMediaGalleryRegistry::GetInstance();
  return imported_registry_->IPhotoDataProvider();
}

}  // namespace iphoto