diff options
Diffstat (limited to 'chrome/browser/media_galleries/fileapi')
14 files changed, 4 insertions, 1683 deletions
diff --git a/chrome/browser/media_galleries/fileapi/iapps_finder.h b/chrome/browser/media_galleries/fileapi/iapps_finder.h index 4184cc3..1002d81 100644 --- a/chrome/browser/media_galleries/fileapi/iapps_finder.h +++ b/chrome/browser/media_galleries/fileapi/iapps_finder.h @@ -13,22 +13,17 @@ namespace iapps { typedef base::Callback<void(const std::string&)> IAppsFinderCallback; -// These methods look for the iTunes/iPhoto library in in an asynchronous +// These methods look for the iTunes library in in an asynchronous // manner and call |callback| on the UI thread as soon as the result is known. // If a library exists, |callback| gets the device id for the library. If a -// library does not exist, or the OS does not support iTunes/iPhoto, then -// |callback| gets an empty string. - -void FindIPhotoLibrary(const IAppsFinderCallback& callback); +// library does not exist, or the OS does not support iTunes, then |callback| +// gets an empty string. void FindITunesLibrary(const IAppsFinderCallback& callback); // These methods returns true if |path| should be interpreted as the device // indicated by |device_id|. -bool PathIndicatesIPhotoLibrary(const std::string& device_id, - const base::FilePath& path); - bool PathIndicatesITunesLibrary(const std::string& device_id, const base::FilePath& path); diff --git a/chrome/browser/media_galleries/fileapi/iapps_finder_impl.cc b/chrome/browser/media_galleries/fileapi/iapps_finder_impl.cc index e9c1f0fa..ac8357f 100644 --- a/chrome/browser/media_galleries/fileapi/iapps_finder_impl.cc +++ b/chrome/browser/media_galleries/fileapi/iapps_finder_impl.cc @@ -41,13 +41,6 @@ void FindIAppsOnFileThread(storage_monitor::StorageInfo::Type type, base::Bind(task, base::Bind(PostResultToUIThread, type, callback))); } -// iPhoto is only supported on OSX. -#if !defined(OS_MACOSX) -void FindIPhotoLibrary(const IAppsFinderCallback& callback) { - callback.Run(std::string()); -} -#endif // !defined(OS_MACOSX) - // iTunes is only support on OSX and Windows. #if !defined(OS_MACOSX) && !defined(OS_WIN) void FindITunesLibrary(const IAppsFinderCallback& callback) { @@ -55,24 +48,6 @@ void FindITunesLibrary(const IAppsFinderCallback& callback) { } #endif -bool PathIndicatesIPhotoLibrary(const std::string& device_id, - const base::FilePath& path) { - storage_monitor::StorageInfo::Type device_type; - std::string unique_id; - storage_monitor::StorageInfo::CrackDeviceId( - device_id, &device_type, &unique_id); - if (device_type != storage_monitor::StorageInfo::IPHOTO) - return false; - - // |unique_id| is the path to the library XML file at the library root. - base::FilePath library_root = - base::FilePath::FromUTF8Unsafe(unique_id).DirName(); - return (path == library_root) || - (path == library_root.AppendASCII("Data")) || - (path == library_root.AppendASCII("Originals")) || - (path == library_root.AppendASCII("Masters")); -} - bool PathIndicatesITunesLibrary(const std::string& device_id, const base::FilePath& path) { storage_monitor::StorageInfo::Type device_type; diff --git a/chrome/browser/media_galleries/fileapi/iapps_finder_impl.h b/chrome/browser/media_galleries/fileapi/iapps_finder_impl.h index 82c8424..409cc52 100644 --- a/chrome/browser/media_galleries/fileapi/iapps_finder_impl.h +++ b/chrome/browser/media_galleries/fileapi/iapps_finder_impl.h @@ -30,7 +30,6 @@ namespace iapps { #if defined(OS_MACOSX) -extern NSString* const kIPhotoRecentDatabasesKey; extern NSString* const kITunesRecentDatabasePathsKey; // Set the mac preferences to use for testing. The caller continues to own diff --git a/chrome/browser/media_galleries/fileapi/iapps_finder_impl_mac.mm b/chrome/browser/media_galleries/fileapi/iapps_finder_impl_mac.mm index 93960af..3140864 100644 --- a/chrome/browser/media_galleries/fileapi/iapps_finder_impl_mac.mm +++ b/chrome/browser/media_galleries/fileapi/iapps_finder_impl_mac.mm @@ -72,15 +72,6 @@ void FindMostRecentDatabase( callback.Run(most_recent_db_path.value()); } -base::FilePath ExtractIPhotoPath(NSString* path_ns) { - NSURL* url = [NSURL URLWithString:path_ns]; - if (![url isFileURL]) - return base::FilePath(); - - NSString* expanded_path_ns = [url path]; - return base::mac::NSStringToFilePath(expanded_path_ns); -} - base::FilePath ExtractITunesPath(NSString* path_ns) { NSString* expanded_path_ns = [path_ns stringByExpandingTildeInPath]; return base::mac::NSStringToFilePath(expanded_path_ns); @@ -88,18 +79,8 @@ base::FilePath ExtractITunesPath(NSString* path_ns) { } // namespace -NSString* const kIPhotoRecentDatabasesKey = @"iPhotoRecentDatabases"; NSString* const kITunesRecentDatabasePathsKey = @"iTunesRecentDatabasePaths"; -void FindIPhotoLibrary(const IAppsFinderCallback& callback) { - FindIAppsOnFileThread( - storage_monitor::StorageInfo::IPHOTO, - base::Bind(&FindMostRecentDatabase, - base::scoped_nsobject<NSString>(kIPhotoRecentDatabasesKey), - base::Bind(&ExtractIPhotoPath)), - callback); -} - void FindITunesLibrary(const IAppsFinderCallback& callback) { FindIAppsOnFileThread( storage_monitor::StorageInfo::ITUNES, diff --git a/chrome/browser/media_galleries/fileapi/iphoto_data_provider.cc b/chrome/browser/media_galleries/fileapi/iphoto_data_provider.cc deleted file mode 100644 index 9d7372b..0000000 --- a/chrome/browser/media_galleries/fileapi/iphoto_data_provider.cc +++ /dev/null @@ -1,187 +0,0 @@ -// 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_data_provider.h" - -#include <stdint.h> - -#include <map> - -#include "base/bind.h" -#include "base/callback.h" -#include "base/location.h" -#include "base/logging.h" -#include "base/stl_util.h" -#include "base/strings/string_number_conversions.h" -#include "base/threading/thread_restrictions.h" -#include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" -#include "chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.h" - -namespace iphoto { - -IPhotoDataProvider::IPhotoDataProvider(const base::FilePath& library_path) - : iapps::IAppsDataProvider(library_path), - weak_factory_(this) {} - -IPhotoDataProvider::~IPhotoDataProvider() {} - -void IPhotoDataProvider::DoParseLibrary( - const base::FilePath& library_path, - const ReadyCallback& ready_callback) { - xml_parser_ = new iapps::SafeIAppsLibraryParser; - xml_parser_->ParseIPhotoLibrary( - library_path, - base::Bind(&IPhotoDataProvider::OnLibraryParsed, - weak_factory_.GetWeakPtr(), - ready_callback)); -} - -void IPhotoDataProvider::OnLibraryParsed(const ReadyCallback& ready_callback, - bool result, - const parser::Library& library) { - DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); - set_valid(result); - if (valid()) - BuildIndices(library); - ready_callback.Run(valid()); -} - -void IPhotoDataProvider::BuildIndices(const parser::Library& library) { - typedef base::hash_map<uint64_t, const base::FilePath*> IdIndex; - - IdIndex photo_id_index; - IdIndex originals_id_index; - for (std::set<parser::Photo>::const_iterator photo_it = - library.all_photos.begin(); - photo_it != library.all_photos.end(); photo_it++) { - photo_id_index[photo_it->id] = &(photo_it->location); - if (!photo_it->original_location.empty()) - originals_id_index[photo_it->id] = &(photo_it->original_location); - } - - // Build up a set of IDs which have in-album duplicates. - // Those are the filenames we want to globally mangle. - std::set<uint64_t> dupe_ids; - for (parser::Albums::const_iterator album_it = library.albums.begin(); - album_it != library.albums.end(); album_it++) { - const parser::Album& album = album_it->second; - - std::set<std::string> album_paths; - for (parser::Album::const_iterator id_it = album.begin(); - id_it != album.end(); id_it++) { - uint64_t id = *id_it; - IdIndex::const_iterator photo_it = photo_id_index.find(id); - if (photo_it == photo_id_index.end()) - continue; - - std::string filename = photo_it->second->BaseName().value(); - if (ContainsKey(album_paths, filename)) - dupe_ids.insert(id); - else - album_paths.insert(filename); - } - } - - // Now build the directory index. - dir_index_.clear(); - originals_index_.clear(); - for (parser::Albums::const_iterator album_it = library.albums.begin(); - album_it != library.albums.end(); album_it++) { - std::string album_name = album_it->first; - const parser::Album& album = album_it->second; - - for (parser::Album::const_iterator id_it = album.begin(); - id_it != album.end(); id_it++) { - uint64_t id = *id_it; - IdIndex::const_iterator photo_it = photo_id_index.find(id); - if (photo_it == photo_id_index.end()) - continue; - base::FilePath path = *(photo_it->second); - - std::string filename = path.BaseName().value(); - if (ContainsKey(dupe_ids, id)) { - filename = path.BaseName().InsertBeforeExtension( - "(" + base::Uint64ToString(id) + ")").value(); - } - - dir_index_[album_name][filename] = path; - - IdIndex::const_iterator original_it = originals_id_index.find(id); - if (original_it != originals_id_index.end()) - originals_index_[album_name][filename] = *(original_it->second); - } - } -} - -std::vector<std::string> IPhotoDataProvider::GetAlbumNames() const { - std::vector<std::string> names; - - for (DirIndex::const_iterator dir_it = dir_index_.begin(); - dir_it != dir_index_.end(); dir_it++) { - names.push_back(dir_it->first); - } - - return names; -} - -std::map<std::string, base::FilePath> IPhotoDataProvider::GetAlbumContents( - const std::string& album) const { - std::map<std::string, base::FilePath> locations; - DirIndex::const_iterator dir_it = dir_index_.find(album); - if (dir_it == dir_index_.end()) - return locations; - - for (FileIndex::const_iterator file_it = dir_it->second.begin(); - file_it != dir_it->second.end(); file_it++) { - locations.insert(make_pair(file_it->first, file_it->second)); - } - - return locations; -} - -base::FilePath IPhotoDataProvider::GetPhotoLocationInAlbum( - const std::string& album, - const std::string& filename) const { - DirIndex::const_iterator dir_it = dir_index_.find(album); - if (dir_it == dir_index_.end()) - return base::FilePath(); - FileIndex::const_iterator file_it = dir_it->second.find(filename); - if (file_it == dir_it->second.end()) - return base::FilePath(); - return file_it->second; -} - -bool IPhotoDataProvider::HasOriginals(const std::string& album) const { - DirIndex::const_iterator originals_it = originals_index_.find(album); - return originals_it != originals_index_.end(); -} - -std::map<std::string, base::FilePath> IPhotoDataProvider::GetOriginals( - const std::string& album) const { - std::map<std::string, base::FilePath> locations; - DirIndex::const_iterator originals_it = originals_index_.find(album); - if (originals_it == originals_index_.end()) - return locations; - - for (FileIndex::const_iterator file_it = originals_it->second.begin(); - file_it != originals_it->second.end(); file_it++) { - locations.insert(make_pair(file_it->first, file_it->second)); - } - - return locations; -} - -base::FilePath IPhotoDataProvider::GetOriginalPhotoLocation( - const std::string& album, - const std::string& filename) const { - DirIndex::const_iterator originals_it = originals_index_.find(album); - if (originals_it == originals_index_.end()) - return base::FilePath(); - FileIndex::const_iterator file_it = originals_it->second.find(filename); - if (file_it == originals_it->second.end()) - return base::FilePath(); - return file_it->second; -} - -} // namespace iphoto diff --git a/chrome/browser/media_galleries/fileapi/iphoto_data_provider.h b/chrome/browser/media_galleries/fileapi/iphoto_data_provider.h deleted file mode 100644 index 6266012..0000000 --- a/chrome/browser/media_galleries/fileapi/iphoto_data_provider.h +++ /dev/null @@ -1,77 +0,0 @@ -// 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. - -#ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_IPHOTO_DATA_PROVIDER_H_ -#define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_IPHOTO_DATA_PROVIDER_H_ - -#include <map> -#include <set> -#include <string> - -#include "base/callback_forward.h" -#include "base/containers/hash_tables.h" -#include "base/files/file_path.h" -#include "base/files/file_path_watcher.h" -#include "base/macros.h" -#include "base/memory/weak_ptr.h" -#include "chrome/browser/media_galleries/fileapi/iapps_data_provider.h" -#include "chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.h" -#include "chrome/common/media_galleries/iphoto_library.h" - -namespace iphoto { - -// This class is the holder for iPhoto parsed data. Given a path to the iPhoto -// library XML file it will read it in, parse the data, and provide convenient -// methods to access it. When the file changes, it will update the data. -// It is not thread safe, but can be run on any thread with IO access. -class IPhotoDataProvider : public iapps::IAppsDataProvider { - public: - explicit IPhotoDataProvider(const base::FilePath& library_path); - ~IPhotoDataProvider() override; - - // Parse the library xml file. - void DoParseLibrary(const base::FilePath& library_path, - const ReadyCallback& ready_callback) override; - - virtual std::vector<std::string> GetAlbumNames() const; - virtual std::map<std::string, base::FilePath> GetAlbumContents( - const std::string& album) const; - virtual base::FilePath GetPhotoLocationInAlbum( - const std::string& album, - const std::string& filename) const; - - virtual bool HasOriginals(const std::string& album) const; - virtual std::map<std::string, base::FilePath> GetOriginals( - const std::string& album) const; - virtual base::FilePath GetOriginalPhotoLocation( - const std::string& album, - const std::string& filename) const; - - private: - typedef base::hash_map<std::string, base::FilePath> FileIndex; - // Map from album name to a map of filename to path. - typedef std::map<std::string, FileIndex> DirIndex; - - void OnLibraryParsed(const ReadyCallback& ready_callback, - bool result, - const parser::Library& library); - - void BuildIndices(const parser::Library& library); - - // Index for library data as it is presented in the virtual FS. - DirIndex dir_index_; - - // Index for originals data. - DirIndex originals_index_; - - scoped_refptr<iapps::SafeIAppsLibraryParser> xml_parser_; - - base::WeakPtrFactory<IPhotoDataProvider> weak_factory_; - - DISALLOW_COPY_AND_ASSIGN(IPhotoDataProvider); -}; - -} // namespace iphoto - -#endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_IPHOTO_DATA_PROVIDER_H_ diff --git a/chrome/browser/media_galleries/fileapi/iphoto_data_provider_browsertest.cc b/chrome/browser/media_galleries/fileapi/iphoto_data_provider_browsertest.cc deleted file mode 100644 index 99d2d9b..0000000 --- a/chrome/browser/media_galleries/fileapi/iphoto_data_provider_browsertest.cc +++ /dev/null @@ -1,474 +0,0 @@ -// 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 <string> -#include <vector> - -#include "base/bind.h" -#include "base/files/file_path.h" -#include "base/files/file_util.h" -#include "base/files/scoped_temp_dir.h" -#include "base/format_macros.h" -#include "base/logging.h" -#include "base/macros.h" -#include "base/memory/scoped_ptr.h" -#include "base/message_loop/message_loop.h" -#include "base/run_loop.h" -#include "base/stl_util.h" -#include "base/strings/stringprintf.h" -#include "chrome/browser/media_galleries/fileapi/iphoto_data_provider.h" -#include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" -#include "chrome/browser/media_galleries/imported_media_gallery_registry.h" -#include "chrome/test/base/in_process_browser_test.h" -#include "content/public/browser/browser_thread.h" -#include "url/gurl.h" - -using base::FilePath; - -namespace iphoto { - -class TestIPhotoDataProvider : public IPhotoDataProvider { - public: - TestIPhotoDataProvider(const base::FilePath& xml_library_path, - const base::Closure& callback) - : IPhotoDataProvider(xml_library_path), - callback_(callback) { - } - ~TestIPhotoDataProvider() override {} - - private: - void OnLibraryChanged(const base::FilePath& path, bool error) override { - IPhotoDataProvider::OnLibraryChanged(path, error); - callback_.Run(); - } - - base::Closure callback_; - - DISALLOW_COPY_AND_ASSIGN(TestIPhotoDataProvider); -}; - -class IPhotoDataProviderTest : public InProcessBrowserTest { - public: - IPhotoDataProviderTest() {} - - protected: - void SetUp() override { - ASSERT_TRUE(library_dir_.CreateUniqueTempDir()); - WriteLibraryInternal(); - // The ImportedMediaGalleryRegistry is created on which ever thread calls - // GetInstance() first. It shouldn't matter what thread creates, however - // in practice it is always created on the UI thread, so this calls - // GetInstance here to mirror those real conditions. - ImportedMediaGalleryRegistry::GetInstance(); - InProcessBrowserTest::SetUp(); - } - - void RunTest() { - DCHECK_CURRENTLY_ON(content::BrowserThread::UI); - base::RunLoop loop; - quit_closure_ = loop.QuitClosure(); - MediaFileSystemBackend::MediaTaskRunner()->PostTask( - FROM_HERE, - base::Bind(&IPhotoDataProviderTest::StartTestOnMediaTaskRunner, - base::Unretained(this))); - loop.Run(); - } - - void WriteLibrary(const base::Closure& callback) { - SetLibraryChangeCallback(callback); - WriteLibraryInternal(); - } - - void SetLibraryChangeCallback(const base::Closure& callback) { - EXPECT_TRUE(library_changed_callback_.is_null()); - library_changed_callback_ = callback; - } - - IPhotoDataProvider* data_provider() const { - return ImportedMediaGalleryRegistry::IPhotoDataProvider(); - } - - const base::FilePath& library_dir() const { - return library_dir_.path(); - } - - base::FilePath XmlFile() const { - return library_dir_.path().AppendASCII("library.xml"); - } - - // Start the test. The data provider is refreshed before calling StartTest - // and the result of the refresh is passed in. - virtual void StartTest(bool parse_success) = 0; - - void TestDone() { - DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); - ImportedMediaGalleryRegistry* imported_registry = - ImportedMediaGalleryRegistry::GetInstance(); - imported_registry->iphoto_data_provider_.reset(); - content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, - quit_closure_); - } - - // Override to provide a full library string. - virtual std::string GetLibraryString() { - return "<plist><dict>\n</dict></plist>\n"; - } - - private: - void StartTestOnMediaTaskRunner() { - DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); - ImportedMediaGalleryRegistry* imported_registry = - ImportedMediaGalleryRegistry::GetInstance(); - imported_registry->iphoto_data_provider_.reset( - new TestIPhotoDataProvider( - XmlFile(), - base::Bind(&IPhotoDataProviderTest::OnLibraryChanged, - base::Unretained(this)))); - data_provider()->RefreshData(base::Bind(&IPhotoDataProviderTest::StartTest, - base::Unretained(this))); - }; - - void OnLibraryChanged() { - DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); - if (!library_changed_callback_.is_null()) { - library_changed_callback_.Run(); - library_changed_callback_.Reset(); - } - } - - void WriteLibraryInternal() { - std::string xml = GetLibraryString(); - ASSERT_EQ(static_cast<int>(xml.size()), - base::WriteFile(XmlFile(), xml.c_str(), xml.size())); - } - - base::ScopedTempDir library_dir_; - - base::Closure library_changed_callback_; - - base::Closure quit_closure_; - - DISALLOW_COPY_AND_ASSIGN(IPhotoDataProviderTest); -}; - -class IPhotoDataProviderBasicTest : public IPhotoDataProviderTest { - public: - IPhotoDataProviderBasicTest() {} - - std::string GetLibraryString() override { - return "<plist><dict>\n" - "<key>List of Albums</key>\n" - "<array>" - " <dict>\n" - " <key>AlbumId</key>" - " <integer>14</integer>" - " <key>AlbumName</key>" - " <string>Album1</string>" - " <key>KeyList</key>" - " <array>" - " <string>1</string>" - " <string>3</string>" // [3] and [4] are name dupes - " <string>4</string>" - " </array>" - " </dict>\n" - " <dict>\n" - " <key>AlbumId</key>" - " <integer>15</integer>" - " <key>AlbumName</key>" - " <string>Album2</string>" - " <key>KeyList</key>" - " <array>" - " <string>2</string>" - " </array>" - " </dict>\n" - " <dict>\n" - " <key>AlbumId</key>" - " <integer>16</integer>" - " <key>AlbumName</key>" - " <string>Album5</string>" - " <key>KeyList</key>" - " <array>" - " <string>5</string>" // A name dupe of [2], but in another album. - " </array>" - " </dict>\n" - "</array>\n" - "<key>Master Image List</key>\n" - "<dict>\n" - " <key>1</key>\n" - " <dict>\n" - " <key>MediaType</key>" - " <string>Image</string>" - " <key>Caption</key>" - " <string>caption</string>" - " <key>GUID</key>\n" - " <string>guid1</string>" - " <key>ImagePath</key>" - " <string>/vol/path1.jpg</string>" - " <key>ThumbPath</key>" - " <string>/vol/thumb1.jpg</string>" - " </dict>\n" - " <key>2</key>\n" - " <dict>\n" - " <key>MediaType</key>" - " <string>Image</string>" - " <key>Caption</key>" - " <string>caption2</string>" - " <key>GUID</key>\n" - " <string>guid2</string>" - " <key>ImagePath</key>" - " <string>/vol/path2.jpg</string>" - " <key>ThumbPath</key>" - " <string>/vol/thumb2.jpg</string>" - " </dict>\n" - " <key>3</key>\n" - " <dict>\n" - " <key>MediaType</key>" - " <string>Image</string>" - " <key>Caption</key>" - " <string>caption3</string>" - " <key>GUID</key>\n" - " <string>guid3</string>" - " <key>ImagePath</key>" - " <string>/vol/path3.jpg</string>" - " <key>ThumbPath</key>" - " <string>/vol/thumb3.jpg</string>" - " </dict>\n" - " <key>4</key>\n" // A name duplicate of [3] in another path. - " <dict>\n" - " <key>MediaType</key>" - " <string>Image</string>" - " <key>Caption</key>" - " <string>caption</string>" - " <key>GUID</key>\n" - " <string>guid3</string>" - " <key>ImagePath</key>" - " <string>/vol/dupe/path3.jpg</string>" - " <key>ThumbPath</key>" - " <string>/vol/dupe/thumb3.jpg</string>" - " </dict>\n" - " <key>5</key>\n" // A name duplicate of [2] in another path. - " <dict>\n" - " <key>MediaType</key>" - " <string>Image</string>" - " <key>Caption</key>" - " <string>caption5</string>" - " <key>GUID</key>\n" - " <string>guid2</string>" - " <key>ImagePath</key>" - " <string>/vol/dupe/path2.jpg</string>" - " <key>ThumbPath</key>" - " <string>/vol/dupe/thumb2.jpg</string>" - " <key>OriginalPath</key>" \ - " <string>/original/vol/another2.jpg</string>" \ - " </dict>\n" - "</dict>\n" - "</dict></plist>\n"; - } - - void StartTest(bool parse_success) override { - EXPECT_TRUE(parse_success); - - std::vector<std::string> names = data_provider()->GetAlbumNames(); - EXPECT_EQ(3U, names.size()); - EXPECT_EQ("Album1", names[0]); - - EXPECT_EQ(FilePath("/vol/path1.jpg").value(), - data_provider()->GetPhotoLocationInAlbum( - "Album1", "path1.jpg").value()); - EXPECT_EQ(FilePath("/vol/path3.jpg").value(), - data_provider()->GetPhotoLocationInAlbum( - "Album1", "path3.jpg").value()); - EXPECT_EQ(FilePath("/vol/dupe/path3.jpg").value(), - data_provider()->GetPhotoLocationInAlbum( - "Album1", "path3(4).jpg").value()); - EXPECT_EQ(FilePath().value(), - data_provider()->GetPhotoLocationInAlbum( - "Album1", "path5.jpg").value()); - - // path2.jpg is name-duped, but in different albums, and so should not - // be mangled. - EXPECT_EQ(FilePath("/vol/dupe/path2.jpg").value(), - data_provider()->GetPhotoLocationInAlbum( - "Album5", "path2.jpg").value()); - EXPECT_EQ(FilePath("/vol/path2.jpg").value(), - data_provider()->GetPhotoLocationInAlbum( - "Album2", "path2.jpg").value()); - - std::map<std::string, base::FilePath> photos = - data_provider()->GetAlbumContents("nonexistent"); - EXPECT_EQ(0U, photos.size()); - photos = data_provider()->GetAlbumContents("Album1"); - EXPECT_EQ(3U, photos.size()); - EXPECT_TRUE(ContainsKey(photos, "path1.jpg")); - EXPECT_FALSE(ContainsKey(photos, "path2.jpg")); - EXPECT_TRUE(ContainsKey(photos, "path3.jpg")); - EXPECT_TRUE(ContainsKey(photos, "path3(4).jpg")); - EXPECT_EQ(FilePath("/vol/path1.jpg").value(), photos["path1.jpg"].value()); - EXPECT_EQ(FilePath("/vol/path3.jpg").value(), - photos["path3.jpg"].value()); - EXPECT_EQ(FilePath("/vol/dupe/path3.jpg").value(), - photos["path3(4).jpg"].value()); - - photos = data_provider()->GetAlbumContents("Album2"); - EXPECT_EQ(1U, photos.size()); - EXPECT_TRUE(ContainsKey(photos, "path2.jpg")); - - EXPECT_FALSE(data_provider()->HasOriginals("Album1")); - EXPECT_TRUE(data_provider()->HasOriginals("Album5")); - std::map<std::string, base::FilePath> originals = - data_provider()->GetOriginals("Album1"); - EXPECT_EQ(0U, originals.size()); - originals = data_provider()->GetOriginals("Album5"); - EXPECT_EQ(1U, originals.size()); - EXPECT_TRUE(ContainsKey(originals, "path2.jpg")); - EXPECT_FALSE(ContainsKey(originals, "path1.jpg")); - EXPECT_EQ(FilePath("/original/vol/another2.jpg").value(), - originals["path2.jpg"].value()); - base::FilePath original_path = - data_provider()->GetOriginalPhotoLocation("Album5", "path2.jpg"); - EXPECT_EQ(FilePath("/original/vol/another2.jpg").value(), - original_path.value()); - - TestDone(); - } - - private: - DISALLOW_COPY_AND_ASSIGN(IPhotoDataProviderBasicTest); -}; - -class IPhotoDataProviderRefreshTest : public IPhotoDataProviderTest { - public: - IPhotoDataProviderRefreshTest() {} - - std::string another_album; - - std::string GetLibraryString() override { - return "<plist><dict>\n" - "<key>List of Albums</key>\n" - "<array>" - " <dict>" - " <key>AlbumId</key>" - " <integer>14</integer>" - " <key>AlbumName</key>" - " <string>Album1</string>" - " <key>KeyList</key>" - " <array>" - " <string>1</string>" - " </array>" - " </dict>\n" + - another_album + - "</array>\n" - "<key>Master Image List</key>\n" - "<dict>\n" - " <key>1</key>\n" - " <dict>\n" - " <key>MediaType</key>" - " <string>Image</string>" - " <key>Caption1</key>" - " <string>caption</string>" - " <key>GUID</key>\n" - " <string>guid1</string>" - " <key>ImagePath</key>" - " <string>/vol/path1.jpg</string>" - " <key>ThumbPath</key>" - " <string>/vol/thumb1.jpg</string>" - " </dict>\n" - "</dict>\n" - "</dict></plist>\n"; - } - - void StartTest(bool parse_success) override { - EXPECT_TRUE(parse_success); - - EXPECT_EQ(FilePath("/vol/path1.jpg"), - data_provider()->GetPhotoLocationInAlbum("Album1", "path1.jpg")); - std::vector<std::string> names = data_provider()->GetAlbumNames(); - EXPECT_EQ(1U, names.size()); - EXPECT_EQ("Album1", names[0]); - - another_album = - " <dict>" - " <key>AlbumId</key>" - " <integer>14</integer>" - " <key>AlbumName</key>" - " <string>Another Album</string>" - " <key>KeyList</key>" - " <array>" - " <string>1</string>" - " </array>" - " </dict>\n"; - - WriteLibrary(base::Bind(&IPhotoDataProviderRefreshTest::CheckAfterWrite, - base::Unretained(this))); - } - - void CheckAfterWrite() { - // No change -- data has not been parsed. - EXPECT_EQ(FilePath("/vol/path1.jpg"), - data_provider()->GetPhotoLocationInAlbum("Album1", "path1.jpg")); - std::vector<std::string> names = data_provider()->GetAlbumNames(); - EXPECT_EQ(1U, names.size()); - EXPECT_EQ("Album1", names[0]); - - data_provider()->RefreshData( - base::Bind(&IPhotoDataProviderRefreshTest::CheckRefresh, - base::Unretained(this))); - } - - void CheckRefresh(bool is_valid) { - EXPECT_TRUE(is_valid); - - EXPECT_EQ(FilePath("/vol/path1.jpg"), - data_provider()->GetPhotoLocationInAlbum("Album1", "path1.jpg")); - std::vector<std::string> names = data_provider()->GetAlbumNames(); - EXPECT_EQ(2U, names.size()); - if (names.size() == 2U) { - EXPECT_EQ("Album1", names[0]); - EXPECT_EQ("Another Album", names[1]); - } - - TestDone(); - } - - private: - DISALLOW_COPY_AND_ASSIGN(IPhotoDataProviderRefreshTest); -}; - -class IPhotoDataProviderInvalidTest : public IPhotoDataProviderTest { - public: - IPhotoDataProviderInvalidTest() {} - - void StartTest(bool parse_success) override { - EXPECT_TRUE(parse_success); - - SetLibraryChangeCallback( - base::Bind(&IPhotoDataProvider::RefreshData, - base::Unretained(data_provider()), - base::Bind(&IPhotoDataProviderInvalidTest::CheckInvalid, - base::Unretained(this)))); - EXPECT_EQ(1L, base::WriteFile(XmlFile(), " ", 1)); - } - - void CheckInvalid(bool is_valid) { - EXPECT_FALSE(is_valid); - TestDone(); - } - - private: - DISALLOW_COPY_AND_ASSIGN(IPhotoDataProviderInvalidTest); -}; - -IN_PROC_BROWSER_TEST_F(IPhotoDataProviderBasicTest, BasicTest) { - RunTest(); -} - -IN_PROC_BROWSER_TEST_F(IPhotoDataProviderRefreshTest, RefreshTest) { - RunTest(); -} - -IN_PROC_BROWSER_TEST_F(IPhotoDataProviderInvalidTest, InvalidTest) { - RunTest(); -} - -} // namespace iphoto diff --git a/chrome/browser/media_galleries/fileapi/iphoto_file_util.cc b/chrome/browser/media_galleries/fileapi/iphoto_file_util.cc deleted file mode 100644 index 1f58e3d..0000000 --- a/chrome/browser/media_galleries/fileapi/iphoto_file_util.cc +++ /dev/null @@ -1,355 +0,0 @@ -// 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 <utility> -#include <vector> - -#include "base/bind_helpers.h" -#include "base/files/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 "storage/browser/blob/shareable_file_reference.h" -#include "storage/browser/fileapi/file_system_operation_context.h" -#include "storage/browser/fileapi/file_system_url.h" -#include "storage/browser/fileapi/native_file_util.h" -#include "storage/common/fileapi/directory_entry.h" -#include "storage/common/fileapi/file_system_util.h" - -using storage::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; -} - -std::vector<std::string> GetVirtualPathComponents( - const storage::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; - storage::VirtualPath::GetComponentsUTF8Unsafe(virtual_path, &result); - return result; -} - -} // namespace - -const char kIPhotoAlbumsDir[] = "Albums"; -const char kIPhotoOriginalsDir[] = "Originals"; - -IPhotoFileUtil::IPhotoFileUtil(MediaPathFilter* media_path_filter) - : NativeMediaFileUtil(media_path_filter), - imported_registry_(NULL), - weak_factory_(this) { -} - -IPhotoFileUtil::~IPhotoFileUtil() { -} - -void IPhotoFileUtil::GetFileInfoOnTaskRunnerThread( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::FileSystemURL& url, - const GetFileInfoCallback& 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(std::move(context), url, callback, false); - } else { - data_provider->RefreshData( - base::Bind(&IPhotoFileUtil::GetFileInfoWithFreshDataProvider, - weak_factory_.GetWeakPtr(), base::Passed(&context), url, - callback)); - } -} - -void IPhotoFileUtil::ReadDirectoryOnTaskRunnerThread( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::FileSystemURL& url, - const ReadDirectoryCallback& 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(std::move(context), url, callback, - false); - } else { - data_provider->RefreshData( - base::Bind(&IPhotoFileUtil::ReadDirectoryWithFreshDataProvider, - weak_factory_.GetWeakPtr(), base::Passed(&context), url, - callback)); - } -} - -void IPhotoFileUtil::CreateSnapshotFileOnTaskRunnerThread( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::FileSystemURL& url, - const CreateSnapshotFileCallback& 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(std::move(context), url, callback, - false); - } else { - data_provider->RefreshData( - base::Bind(&IPhotoFileUtil::CreateSnapshotFileWithFreshDataProvider, - weak_factory_.GetWeakPtr(), base::Passed(&context), url, - callback)); - } -} - -void IPhotoFileUtil::GetFileInfoWithFreshDataProvider( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::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(std::move(context), url, - callback); -} - -void IPhotoFileUtil::ReadDirectoryWithFreshDataProvider( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::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(std::move(context), url, - callback); -} - -void IPhotoFileUtil::CreateSnapshotFileWithFreshDataProvider( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::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<storage::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(std::move(context), - url, callback); -} - -// Begin actual implementation. - -base::File::Error IPhotoFileUtil::GetFileInfoSync( - storage::FileSystemOperationContext* context, - const storage::FileSystemURL& url, - base::File::Info* file_info, - base::FilePath* platform_path) { - std::vector<std::string> components = GetVirtualPathComponents(url); - - 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( - storage::FileSystemOperationContext* context, - const storage::FileSystemURL& url, - EntryList* file_list) { - DCHECK(file_list->empty()); - std::vector<std::string> components = GetVirtualPathComponents(url); - - // Root directory. Child is the /Albums dir. - if (components.size() == 0) { - file_list->push_back( - DirectoryEntry(kIPhotoAlbumsDir, DirectoryEntry::DIRECTORY)); - 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)); - } - 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)); - } - 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)); - } - 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)); - } - return base::File::FILE_OK; - } - } - - return base::File::FILE_ERROR_NOT_FOUND; -} - -base::File::Error IPhotoFileUtil::DeleteDirectorySync( - storage::FileSystemOperationContext* context, - const storage::FileSystemURL& url) { - return base::File::FILE_ERROR_SECURITY; -} - -base::File::Error IPhotoFileUtil::DeleteFileSync( - storage::FileSystemOperationContext* context, - const storage::FileSystemURL& url) { - return base::File::FILE_ERROR_SECURITY; -} - -base::File::Error IPhotoFileUtil::GetLocalFilePath( - storage::FileSystemOperationContext* context, - const storage::FileSystemURL& url, - base::FilePath* local_file_path) { - std::vector<std::string> components = GetVirtualPathComponents(url); - - 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 diff --git a/chrome/browser/media_galleries/fileapi/iphoto_file_util.h b/chrome/browser/media_galleries/fileapi/iphoto_file_util.h deleted file mode 100644 index db35156..0000000 --- a/chrome/browser/media_galleries/fileapi/iphoto_file_util.h +++ /dev/null @@ -1,99 +0,0 @@ -// 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. - -#ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_IPHOTO_FILE_UTIL_H_ -#define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_IPHOTO_FILE_UTIL_H_ - -#include "base/macros.h" -#include "base/memory/scoped_ptr.h" -#include "base/memory/weak_ptr.h" -#include "chrome/browser/media_galleries/fileapi/native_media_file_util.h" - -class ImportedMediaGalleryRegistry; - -namespace iphoto { - -class IPhotoDataProvider; - -// Presents a virtual file system containing iPhoto contents in the -// following organization: -// / = virtual root -// |- /Albums -// |- /AlbumName = An album entry by name. -// |- pic1.jpg = Entries for photos in the album. -// |- pic2.jpg -// |- /AnotherAlbum -// |- pic3.jpg -// |- /originals = A directory for originals. -// |- pic3.jpg = Original picture. Same name as album photo. - -extern const char kIPhotoAlbumsDir[]; - -class IPhotoFileUtil : public NativeMediaFileUtil { - public: - explicit IPhotoFileUtil(MediaPathFilter* media_path_filter); - ~IPhotoFileUtil() override; - - protected: - // NativeMediaFileUtil overrides. - void GetFileInfoOnTaskRunnerThread( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::FileSystemURL& url, - const GetFileInfoCallback& callback) override; - void ReadDirectoryOnTaskRunnerThread( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::FileSystemURL& url, - const ReadDirectoryCallback& callback) override; - void CreateSnapshotFileOnTaskRunnerThread( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::FileSystemURL& url, - const CreateSnapshotFileCallback& callback) override; - base::File::Error GetFileInfoSync( - storage::FileSystemOperationContext* context, - const storage::FileSystemURL& url, - base::File::Info* file_info, - base::FilePath* platform_path) override; - base::File::Error ReadDirectorySync( - storage::FileSystemOperationContext* context, - const storage::FileSystemURL& url, - EntryList* file_list) override; - base::File::Error DeleteDirectorySync( - storage::FileSystemOperationContext* context, - const storage::FileSystemURL& url) override; - base::File::Error DeleteFileSync(storage::FileSystemOperationContext* context, - const storage::FileSystemURL& url) override; - base::File::Error GetLocalFilePath( - storage::FileSystemOperationContext* context, - const storage::FileSystemURL& url, - base::FilePath* local_file_path) override; - - private: - void GetFileInfoWithFreshDataProvider( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::FileSystemURL& url, - const GetFileInfoCallback& callback, - bool valid_parse); - void ReadDirectoryWithFreshDataProvider( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::FileSystemURL& url, - const ReadDirectoryCallback& callback, - bool valid_parse); - virtual void CreateSnapshotFileWithFreshDataProvider( - scoped_ptr<storage::FileSystemOperationContext> context, - const storage::FileSystemURL& url, - const CreateSnapshotFileCallback& callback, - bool valid_parse); - - virtual IPhotoDataProvider* GetDataProvider(); - - ImportedMediaGalleryRegistry* imported_registry_; - - base::WeakPtrFactory<IPhotoFileUtil> weak_factory_; - - DISALLOW_COPY_AND_ASSIGN(IPhotoFileUtil); -}; - -} // namespace iphoto - -#endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_IPHOTO_FILE_UTIL_H_ diff --git a/chrome/browser/media_galleries/fileapi/iphoto_file_util_unittest.cc b/chrome/browser/media_galleries/fileapi/iphoto_file_util_unittest.cc deleted file mode 100644 index c912ef5..0000000 --- a/chrome/browser/media_galleries/fileapi/iphoto_file_util_unittest.cc +++ /dev/null @@ -1,353 +0,0 @@ -// 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 <map> -#include <set> -#include <string> -#include <utility> -#include <vector> - -#include "base/bind.h" -#include "base/bind_helpers.h" -#include "base/files/file_util.h" -#include "base/files/scoped_temp_dir.h" -#include "base/macros.h" -#include "base/run_loop.h" -#include "base/single_thread_task_runner.h" -#include "base/synchronization/waitable_event.h" -#include "base/thread_task_runner_handle.h" -#include "base/time/time.h" -#include "chrome/browser/media_galleries/fileapi/iphoto_data_provider.h" -#include "chrome/browser/media_galleries/fileapi/media_file_system_backend.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 "content/public/test/mock_special_storage_policy.h" -#include "content/public/test/test_browser_thread.h" -#include "content/public/test/test_file_system_options.h" -#include "storage/browser/fileapi/async_file_util.h" -#include "storage/browser/fileapi/external_mount_points.h" -#include "storage/browser/fileapi/file_system_context.h" -#include "storage/browser/fileapi/file_system_operation_context.h" -#include "storage/browser/fileapi/file_system_operation_runner.h" -#include "testing/gtest/include/gtest/gtest.h" - -using storage::FileSystemOperationContext; -using storage::FileSystemOperation; -using storage::FileSystemURL; - -namespace iphoto { - -namespace { - -void ReadDirectoryTestHelperCallback( - base::RunLoop* run_loop, - FileSystemOperation::FileEntryList* contents, - bool* completed, - base::File::Error error, - const FileSystemOperation::FileEntryList& file_list, - bool has_more) { - DCHECK(!*completed); - *completed = !has_more && error == base::File::FILE_OK; - *contents = file_list; - run_loop->Quit(); -} - -void ReadDirectoryTestHelper(storage::FileSystemOperationRunner* runner, - const FileSystemURL& url, - FileSystemOperation::FileEntryList* contents, - bool* completed) { - DCHECK(contents); - DCHECK(completed); - base::RunLoop run_loop; - runner->ReadDirectory( - url, base::Bind(&ReadDirectoryTestHelperCallback, &run_loop, contents, - completed)); - run_loop.Run(); -} - -} // namespace - -class TestIPhotoDataProvider : public IPhotoDataProvider { - public: - explicit TestIPhotoDataProvider(const base::FilePath& fake_library_path) - : IPhotoDataProvider(fake_library_path) { - EXPECT_TRUE(fake_auto_add_dir_.CreateUniqueTempDir()); - } - - ~TestIPhotoDataProvider() override {} - - void RefreshData(const ReadyCallback& ready_callback) override { - ready_callback.Run(true /* success */); - } - - std::vector<std::string> GetAlbumNames() const override { - std::vector<std::string> names; - names.push_back("Album1"); - names.push_back("has_originals"); - return names; - } - - std::map<std::string, base::FilePath> GetAlbumContents( - const std::string& album) const override { - std::map<std::string, base::FilePath> contents; - contents["a.jpg"] = library_path().AppendASCII("a.jpg"); - return contents; - } - - base::FilePath GetPhotoLocationInAlbum( - const std::string& album, - const std::string& filename) const override { - return library_path().AppendASCII("a.jpg"); - } - - bool HasOriginals(const std::string& album) const override { - return (album == "has_originals"); - } - - std::map<std::string, base::FilePath> GetOriginals( - const std::string& album) const override { - std::map<std::string, base::FilePath> contents; - contents["a.jpg"] = library_path().AppendASCII("orig.jpg"); - return contents; - } - - base::FilePath GetOriginalPhotoLocation( - const std::string& album, - const std::string& filename) const override { - return library_path().AppendASCII("orig.jpg"); - } - - private: - base::ScopedTempDir fake_auto_add_dir_; -}; - -class TestIPhotoFileUtil : public IPhotoFileUtil { - public: - explicit TestIPhotoFileUtil(MediaPathFilter* media_path_filter, - IPhotoDataProvider* data_provider) - : IPhotoFileUtil(media_path_filter), - data_provider_(data_provider) { - } - ~TestIPhotoFileUtil() override {} - - private: - IPhotoDataProvider* GetDataProvider() override { return data_provider_; } - - IPhotoDataProvider* data_provider_; -}; - -class TestMediaFileSystemBackend : public MediaFileSystemBackend { - public: - TestMediaFileSystemBackend(const base::FilePath& profile_path, - IPhotoFileUtil* iphoto_file_util) - : MediaFileSystemBackend( - profile_path, - MediaFileSystemBackend::MediaTaskRunner().get()), - test_file_util_(iphoto_file_util) {} - - storage::AsyncFileUtil* GetAsyncFileUtil( - storage::FileSystemType type) override { - if (type != storage::kFileSystemTypeIphoto) - return NULL; - - return test_file_util_.get(); - } - - private: - scoped_ptr<storage::AsyncFileUtil> test_file_util_; -}; - -class IPhotoFileUtilTest : public testing::Test { - public: - IPhotoFileUtilTest() - : io_thread_(content::BrowserThread::IO, &message_loop_) { - } - - void SetUpDataProvider() { - ASSERT_TRUE(fake_library_dir_.CreateUniqueTempDir()); - ASSERT_EQ( - 0, - base::WriteFile( - fake_library_dir_.path().AppendASCII("a.jpg"), - NULL, - 0)); - ASSERT_EQ( - 0, - base::WriteFile( - fake_library_dir_.path().AppendASCII("orig.jpg"), - NULL, - 0)); - - iphoto_data_provider_.reset( - new TestIPhotoDataProvider(fake_library_dir_.path())); - } - - void SetUp() override { - ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); - ImportedMediaGalleryRegistry::GetInstance()->Initialize(); - - scoped_refptr<storage::SpecialStoragePolicy> storage_policy = - new content::MockSpecialStoragePolicy(); - - // Initialize fake IPhotoDataProvider on media task runner thread. - MediaFileSystemBackend::MediaTaskRunner()->PostTask( - FROM_HERE, - base::Bind(&IPhotoFileUtilTest::SetUpDataProvider, - base::Unretained(this))); - base::WaitableEvent event(true, false /* initially_signalled */); - MediaFileSystemBackend::MediaTaskRunner()->PostTask( - FROM_HERE, - base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event))); - event.Wait(); - - media_path_filter_.reset(new MediaPathFilter()); - ScopedVector<storage::FileSystemBackend> additional_providers; - additional_providers.push_back(new TestMediaFileSystemBackend( - profile_dir_.path(), - new TestIPhotoFileUtil(media_path_filter_.get(), - iphoto_data_provider_.get()))); - - file_system_context_ = new storage::FileSystemContext( - base::ThreadTaskRunnerHandle::Get().get(), - base::ThreadTaskRunnerHandle::Get().get(), - storage::ExternalMountPoints::CreateRefCounted().get(), - storage_policy.get(), NULL, std::move(additional_providers), - std::vector<storage::URLRequestAutoMountHandler>(), profile_dir_.path(), - content::CreateAllowFileAccessOptions()); - } - - protected: - void TestNonexistentFolder(const std::string& path_append) { - FileSystemOperation::FileEntryList contents; - FileSystemURL url = CreateURL(path_append); - bool completed = false; - ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed); - - ASSERT_FALSE(completed); - } - - FileSystemURL CreateURL(const std::string& path) const { - base::FilePath virtual_path = - ImportedMediaGalleryRegistry::GetInstance()->ImportedRoot(); - virtual_path = virtual_path.AppendASCII("iphoto"); - virtual_path = virtual_path.AppendASCII(path); - return file_system_context_->CreateCrackedFileSystemURL( - GURL("http://www.example.com"), - storage::kFileSystemTypeIphoto, - virtual_path); - } - - storage::FileSystemOperationRunner* operation_runner() const { - return file_system_context_->operation_runner(); - } - - scoped_refptr<storage::FileSystemContext> file_system_context() const { - return file_system_context_; - } - - TestIPhotoDataProvider* data_provider() const { - return iphoto_data_provider_.get(); - } - - private: - base::MessageLoop message_loop_; - content::TestBrowserThread io_thread_; - - base::ScopedTempDir profile_dir_; - base::ScopedTempDir fake_library_dir_; - - scoped_refptr<storage::FileSystemContext> file_system_context_; - scoped_ptr<MediaPathFilter> media_path_filter_; - scoped_ptr<TestIPhotoDataProvider> iphoto_data_provider_; - - DISALLOW_COPY_AND_ASSIGN(IPhotoFileUtilTest); -}; - -TEST_F(IPhotoFileUtilTest, RootContents) { - FileSystemOperation::FileEntryList contents; - FileSystemURL url = CreateURL(""); - bool completed = false; - ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed); - - ASSERT_TRUE(completed); - ASSERT_EQ(1u, contents.size()); - - EXPECT_TRUE(contents.front().is_directory); - - EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kIPhotoAlbumsDir).value(), - contents.back().name); -} - -TEST_F(IPhotoFileUtilTest, AlbumsDirectoryContents) { - FileSystemOperation::FileEntryList contents; - FileSystemURL url = CreateURL(kIPhotoAlbumsDir); - bool completed = false; - ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed); - - ASSERT_TRUE(completed); - ASSERT_EQ(2u, contents.size()); - - EXPECT_TRUE(contents.front().is_directory); - - EXPECT_EQ("Album1", contents.front().name); - EXPECT_EQ("has_originals", contents.back().name); -} - -TEST_F(IPhotoFileUtilTest, AlbumContents) { - FileSystemOperation::FileEntryList contents; - FileSystemURL url = CreateURL(std::string(kIPhotoAlbumsDir) + "/Album1"); - bool completed = false; - ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed); - - ASSERT_TRUE(completed); - ASSERT_EQ(1u, contents.size()); - - EXPECT_FALSE(contents.front().is_directory); - - EXPECT_EQ("a.jpg", contents.back().name); -} - -TEST_F(IPhotoFileUtilTest, BadAccess) { - FileSystemOperation::FileEntryList contents; - FileSystemURL url = CreateURL("None"); - bool completed = false; - ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed); - ASSERT_FALSE(completed); - ASSERT_EQ(0u, contents.size()); - - url = CreateURL(std::string(kIPhotoAlbumsDir) + "/NoAlbum"); - completed = false; - ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed); - ASSERT_FALSE(completed); - ASSERT_EQ(0u, contents.size()); -} - -TEST_F(IPhotoFileUtilTest, Originals) { - FileSystemOperation::FileEntryList contents; - FileSystemURL url = - CreateURL(std::string(kIPhotoAlbumsDir) + "/has_originals"); - bool completed = false; - ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed); - - ASSERT_TRUE(completed); - ASSERT_EQ(2u, contents.size()); - EXPECT_TRUE(contents.front().is_directory); - EXPECT_EQ("Originals", contents.front().name); - EXPECT_FALSE(contents.back().is_directory); - EXPECT_EQ("a.jpg", contents.back().name); - - url = CreateURL(std::string(kIPhotoAlbumsDir) + "/has_originals/Originals"); - completed = false; - ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed); - ASSERT_TRUE(completed); - ASSERT_EQ(1u, contents.size()); - - EXPECT_FALSE(contents.front().is_directory); - EXPECT_EQ("a.jpg", contents.front().name); -} - -} // namespace iphoto diff --git a/chrome/browser/media_galleries/fileapi/media_file_system_backend.cc b/chrome/browser/media_galleries/fileapi/media_file_system_backend.cc index 954a58a..c7f1c34 100644 --- a/chrome/browser/media_galleries/fileapi/media_file_system_backend.cc +++ b/chrome/browser/media_galleries/fileapi/media_file_system_backend.cc @@ -50,10 +50,6 @@ #include "chrome/browser/media_galleries/fileapi/picasa_file_util.h" #endif // defined(OS_WIN) || defined(OS_MACOSX) -#if defined(OS_MACOSX) -#include "chrome/browser/media_galleries/fileapi/iphoto_file_util.h" -#endif // defined(OS_MACOSX) - using storage::FileSystemContext; using storage::FileSystemURL; @@ -147,11 +143,6 @@ MediaFileSystemBackend::MediaFileSystemBackend( picasa_file_util_used_(false), itunes_file_util_used_(false) #endif // defined(OS_WIN) || defined(OS_MACOSX) -#if defined(OS_MACOSX) - , - iphoto_file_util_(new iphoto::IPhotoFileUtil(media_path_filter_.get())), - iphoto_file_util_used_(false) -#endif // defined(OS_MACOSX) { } @@ -236,9 +227,6 @@ bool MediaFileSystemBackend::CanHandleType(storage::FileSystemType type) const { case storage::kFileSystemTypePicasa: case storage::kFileSystemTypeItunes: #endif // defined(OS_WIN) || defined(OS_MACOSX) -#if defined(OS_MACOSX) - case storage::kFileSystemTypeIphoto: -#endif // defined(OS_MACOSX) return true; default: return false; @@ -284,14 +272,6 @@ storage::AsyncFileUtil* MediaFileSystemBackend::GetAsyncFileUtil( } return picasa_file_util_.get(); #endif // defined(OS_WIN) || defined(OS_MACOSX) -#if defined(OS_MACOSX) - case storage::kFileSystemTypeIphoto: - if (!iphoto_file_util_used_) { - media_galleries::UsageCount(media_galleries::IPHOTO_FILE_SYSTEM_USED); - iphoto_file_util_used_ = true; - } - return iphoto_file_util_.get(); -#endif // defined(OS_MACOSX) default: NOTREACHED(); } @@ -312,7 +292,6 @@ MediaFileSystemBackend::GetCopyOrMoveFileValidatorFactory( switch (type) { case storage::kFileSystemTypeNativeMedia: case storage::kFileSystemTypeDeviceMedia: - case storage::kFileSystemTypeIphoto: case storage::kFileSystemTypeItunes: if (!media_copy_or_move_file_validator_factory_) { *error_code = base::File::FILE_ERROR_SECURITY; @@ -351,8 +330,7 @@ bool MediaFileSystemBackend::HasInplaceCopyImplementation( DCHECK(type == storage::kFileSystemTypeNativeMedia || type == storage::kFileSystemTypeDeviceMedia || type == storage::kFileSystemTypeItunes || - type == storage::kFileSystemTypePicasa || - type == storage::kFileSystemTypeIphoto); + type == storage::kFileSystemTypePicasa); return true; } diff --git a/chrome/browser/media_galleries/fileapi/media_file_system_backend.h b/chrome/browser/media_galleries/fileapi/media_file_system_backend.h index 71bdbcc..b8a0736 100644 --- a/chrome/browser/media_galleries/fileapi/media_file_system_backend.h +++ b/chrome/browser/media_galleries/fileapi/media_file_system_backend.h @@ -116,12 +116,6 @@ class MediaFileSystemBackend : public storage::FileSystemBackend { bool picasa_file_util_used_; bool itunes_file_util_used_; #endif // defined(OS_WIN) || defined(OS_MACOSX) -#if defined(OS_MACOSX) - scoped_ptr<storage::AsyncFileUtil> iphoto_file_util_; - - // Used for usage UMA tracking. - bool iphoto_file_util_used_; -#endif // defined(OS_MACOSX) DISALLOW_COPY_AND_ASSIGN(MediaFileSystemBackend); }; diff --git a/chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.cc b/chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.cc index 827b295..53e5bd0 100644 --- a/chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.cc +++ b/chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.cc @@ -24,14 +24,6 @@ namespace iapps { SafeIAppsLibraryParser::SafeIAppsLibraryParser() : parser_state_(INITIAL_STATE) {} -void SafeIAppsLibraryParser::ParseIPhotoLibrary( - const base::FilePath& library_file, - const IPhotoParserCallback& callback) { - library_file_path_ = library_file; - iphoto_callback_ = callback; - Start(); -} - void SafeIAppsLibraryParser::ParseITunesLibrary( const base::FilePath& library_file, const ITunesParserCallback& callback) { @@ -95,34 +87,11 @@ void SafeIAppsLibraryParser::OnUtilityProcessStarted() { IPC::TakeFileHandleForProcess( std::move(library_file_), utility_process_host_->GetData().handle))); - } else if (!iphoto_callback_.is_null()) { -#if defined(OS_MACOSX) - utility_process_host_->Send(new ChromeUtilityMsg_ParseIPhotoLibraryXmlFile( - IPC::TakeFileHandleForProcess( - std::move(library_file_), - utility_process_host_->GetData().handle))); -#endif } parser_state_ = STARTED_PARSING_STATE; } -#if defined(OS_MACOSX) -void SafeIAppsLibraryParser::OnGotIPhotoLibrary( - bool result, const iphoto::parser::Library& library) { - DCHECK_CURRENTLY_ON(BrowserThread::IO); - DCHECK(!iphoto_callback_.is_null()); - - if (parser_state_ != STARTED_PARSING_STATE) - return; - - MediaFileSystemBackend::MediaTaskRunner()->PostTask( - FROM_HERE, - base::Bind(iphoto_callback_, result, library)); - parser_state_ = FINISHED_PARSING_STATE; -} -#endif - void SafeIAppsLibraryParser::OnGotITunesLibrary( bool result, const itunes::parser::Library& library) { DCHECK_CURRENTLY_ON(BrowserThread::IO); @@ -150,11 +119,6 @@ void SafeIAppsLibraryParser::OnError() { parser_state_ = FINISHED_PARSING_STATE; if (!itunes_callback_.is_null()) OnGotITunesLibrary(false /* failed */, itunes::parser::Library()); - -#if defined(OS_MACOSX) - if (!iphoto_callback_.is_null()) - OnGotIPhotoLibrary(false /* failed */, iphoto::parser::Library()); -#endif } bool SafeIAppsLibraryParser::OnMessageReceived( @@ -163,10 +127,6 @@ bool SafeIAppsLibraryParser::OnMessageReceived( IPC_BEGIN_MESSAGE_MAP(SafeIAppsLibraryParser, message) IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, OnUtilityProcessStarted) -#if defined(OS_MACOSX) - IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GotIPhotoLibrary, - OnGotIPhotoLibrary) -#endif IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GotITunesLibrary, OnGotITunesLibrary) IPC_MESSAGE_UNHANDLED(handled = false) diff --git a/chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.h b/chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.h index bd79e51..7b6dcfa 100644 --- a/chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.h +++ b/chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.h @@ -14,7 +14,6 @@ #include "base/macros.h" #include "base/memory/weak_ptr.h" #include "build/build_config.h" -#include "chrome/common/media_galleries/iphoto_library.h" #include "chrome/common/media_galleries/itunes_library.h" #include "content/public/browser/utility_process_host.h" #include "content/public/browser/utility_process_host_client.h" @@ -36,17 +35,11 @@ namespace iapps { // noted. class SafeIAppsLibraryParser : public content::UtilityProcessHostClient { public: - typedef base::Callback<void(bool, const iphoto::parser::Library&)> - IPhotoParserCallback; typedef base::Callback<void(bool, const itunes::parser::Library&)> ITunesParserCallback; SafeIAppsLibraryParser(); - // Start the parse of the iPhoto library file. - void ParseIPhotoLibrary(const base::FilePath& library_file, - const IPhotoParserCallback& callback); - // Start the parse of the iTunes library file. void ParseITunesLibrary(const base::FilePath& library_file, const ITunesParserCallback& callback); @@ -75,12 +68,6 @@ class SafeIAppsLibraryParser : public content::UtilityProcessHostClient { void OnUtilityProcessStarted(); // Notification from the utility process when it finishes parsing the - // iPhoto XML. Runs on the IO thread. -#if defined(OS_MACOSX) - void OnGotIPhotoLibrary(bool result, const iphoto::parser::Library& library); -#endif - - // Notification from the utility process when it finishes parsing the // iTunes XML. Runs on the IO thread. void OnGotITunesLibrary(bool result, const itunes::parser::Library& library); @@ -108,9 +95,6 @@ class SafeIAppsLibraryParser : public content::UtilityProcessHostClient { // Only accessed on the Media Task Runner. ITunesParserCallback itunes_callback_; - // Only accessed on the Media Task Runner. - IPhotoParserCallback iphoto_callback_; - // Verifies the messages from the utility process came at the right time. // Initialized on the Media Task Runner, but only accessed on the IO thread. ParserState parser_state_; |