OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" | 5 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/threading/sequenced_worker_pool.h" | 9 #include "base/threading/sequenced_worker_pool.h" |
| 10 #include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h" |
10 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h" | 11 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h" |
11 #include "chrome/common/extensions/extension_constants.h" | 12 #include "chrome/common/extensions/extension_constants.h" |
12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
13 #include "webkit/browser/fileapi/file_system_task_runners.h" | 14 #include "webkit/browser/fileapi/file_system_task_runners.h" |
14 #include "webkit/browser/fileapi/isolated_context.h" | 15 #include "webkit/browser/fileapi/isolated_context.h" |
15 | 16 |
16 using base::Bind; | 17 using base::Bind; |
17 | 18 |
18 namespace chrome { | 19 namespace chrome { |
19 | 20 |
(...skipping 19 matching lines...) Expand all Loading... |
39 static base::LazyInstance<ImportedMediaGalleryRegistry>::Leaky | 40 static base::LazyInstance<ImportedMediaGalleryRegistry>::Leaky |
40 g_imported_media_gallery_registry = LAZY_INSTANCE_INITIALIZER; | 41 g_imported_media_gallery_registry = LAZY_INSTANCE_INITIALIZER; |
41 | 42 |
42 } | 43 } |
43 | 44 |
44 // static | 45 // static |
45 ImportedMediaGalleryRegistry* ImportedMediaGalleryRegistry::GetInstance() { | 46 ImportedMediaGalleryRegistry* ImportedMediaGalleryRegistry::GetInstance() { |
46 return g_imported_media_gallery_registry.Pointer(); | 47 return g_imported_media_gallery_registry.Pointer(); |
47 } | 48 } |
48 | 49 |
49 // static | |
50 std::string ImportedMediaGalleryRegistry::RegisterPicasaFilesystemOnUIThread( | 50 std::string ImportedMediaGalleryRegistry::RegisterPicasaFilesystemOnUIThread( |
51 const base::FilePath& database_path) { | 51 const base::FilePath& database_path) { |
52 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 52 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
53 | 53 |
54 std::string fsid = | 54 std::string fsid = |
55 fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath( | 55 fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath( |
56 fileapi::kFileSystemTypePicasa, | 56 fileapi::kFileSystemTypePicasa, |
57 extension_misc::kMediaFileSystemPathPart, | 57 extension_misc::kMediaFileSystemPathPart, |
58 base::FilePath()); | 58 base::FilePath()); |
59 | 59 |
60 if (fsid.empty()) | 60 if (fsid.empty()) |
61 return ""; | 61 return ""; |
62 | 62 |
63 MediaTaskRunner()->PostTask( | 63 picasa_fsids_.insert(fsid); |
64 FROM_HERE, | 64 |
65 Bind(&ImportedMediaGalleryRegistry::RegisterPicasaFileSystem, | 65 if (picasa_fsids_.size() == 1) { |
66 base::Unretained(GetInstance()), database_path)); | 66 MediaTaskRunner()->PostTask( |
| 67 FROM_HERE, |
| 68 Bind(&ImportedMediaGalleryRegistry::RegisterPicasaFileSystem, |
| 69 base::Unretained(GetInstance()), database_path)); |
| 70 #ifndef NDEBUG |
| 71 picasa_database_path_ = database_path; |
| 72 } else { |
| 73 DCHECK_EQ(picasa_database_path_, database_path); |
| 74 #endif |
| 75 } |
67 | 76 |
68 return fsid; | 77 return fsid; |
69 } | 78 } |
70 | 79 |
71 // static | 80 std::string ImportedMediaGalleryRegistry::RegisterITunesFilesystemOnUIThread( |
72 bool ImportedMediaGalleryRegistry::RevokePicasaFilesystemOnUIThread( | 81 const base::FilePath& library_xml_path) { |
| 82 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 83 |
| 84 std::string fsid = |
| 85 fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath( |
| 86 fileapi::kFileSystemTypeItunes, |
| 87 extension_misc::kMediaFileSystemPathPart, |
| 88 base::FilePath()); |
| 89 |
| 90 if (fsid.empty()) |
| 91 return ""; |
| 92 |
| 93 itunes_fsids_.insert(fsid); |
| 94 |
| 95 if (itunes_fsids_.size() == 1) { |
| 96 MediaTaskRunner()->PostTask( |
| 97 FROM_HERE, |
| 98 Bind(&ImportedMediaGalleryRegistry::RegisterITunesFileSystem, |
| 99 base::Unretained(GetInstance()), library_xml_path)); |
| 100 #ifndef NDEBUG |
| 101 itunes_xml_library_path_ = library_xml_path; |
| 102 } else { |
| 103 DCHECK_EQ(itumes_xml_library_path_, library_xml_path); |
| 104 #endif |
| 105 } |
| 106 |
| 107 return fsid; |
| 108 } |
| 109 |
| 110 bool ImportedMediaGalleryRegistry::RevokeImportedFilesystemOnUIThread( |
73 const std::string& fsid) { | 111 const std::string& fsid) { |
74 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 112 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
75 | 113 |
76 if (!fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(fsid)) | 114 if (!fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(fsid)) |
77 return false; | 115 return false; |
78 | 116 |
79 MediaTaskRunner()->PostTask( | 117 if (picasa_fsids_.erase(fsid) && !picasa_fsids_.size()) { |
80 FROM_HERE, | 118 MediaTaskRunner()->PostTask( |
81 Bind(&ImportedMediaGalleryRegistry::RevokePicasaFileSystem, | 119 FROM_HERE, |
82 base::Unretained(GetInstance()))); | 120 Bind(&ImportedMediaGalleryRegistry::RevokePicasaFileSystem, |
| 121 base::Unretained(GetInstance()))); |
| 122 return true; |
| 123 } |
83 | 124 |
84 return true; | 125 if (itunes_fsids_.erase(fsid) && !itunes_fsids_.size()) { |
| 126 MediaTaskRunner()->PostTask( |
| 127 FROM_HERE, |
| 128 Bind(&ImportedMediaGalleryRegistry::RevokeITunesFileSystem, |
| 129 base::Unretained(GetInstance()))); |
| 130 return true; |
| 131 } |
| 132 |
| 133 NOTREACHED(); |
| 134 return false; |
85 } | 135 } |
86 | 136 |
87 // static | 137 // static |
88 picasa::PicasaDataProvider* | 138 picasa::PicasaDataProvider* |
89 ImportedMediaGalleryRegistry::picasa_data_provider() { | 139 ImportedMediaGalleryRegistry::picasa_data_provider() { |
90 DCHECK(CurrentlyOnMediaTaskRunnerThread()); | 140 DCHECK(CurrentlyOnMediaTaskRunnerThread()); |
91 DCHECK(GetInstance()->picasa_data_provider_); | 141 DCHECK(GetInstance()->picasa_data_provider_); |
92 return GetInstance()->picasa_data_provider_.get(); | 142 return GetInstance()->picasa_data_provider_.get(); |
93 } | 143 } |
94 | 144 |
95 ImportedMediaGalleryRegistry::ImportedMediaGalleryRegistry() | 145 // static |
96 : picasa_filesystems_count_(0) { | 146 itunes::ITunesDataProvider* |
| 147 ImportedMediaGalleryRegistry::itunes_data_provider() { |
| 148 DCHECK(CurrentlyOnMediaTaskRunnerThread()); |
| 149 DCHECK(GetInstance()->itunes_data_provider_); |
| 150 return GetInstance()->itunes_data_provider_.get(); |
97 } | 151 } |
98 | 152 |
| 153 ImportedMediaGalleryRegistry::ImportedMediaGalleryRegistry() {} |
| 154 |
99 ImportedMediaGalleryRegistry::~ImportedMediaGalleryRegistry() { | 155 ImportedMediaGalleryRegistry::~ImportedMediaGalleryRegistry() { |
100 DCHECK_EQ(0, picasa_filesystems_count_); | 156 DCHECK_EQ(0U, picasa_fsids_.size()); |
| 157 DCHECK_EQ(0U, itunes_fsids_.size()); |
101 } | 158 } |
102 | 159 |
103 void ImportedMediaGalleryRegistry::RegisterPicasaFileSystem( | 160 void ImportedMediaGalleryRegistry::RegisterPicasaFileSystem( |
104 const base::FilePath& database_path) { | 161 const base::FilePath& database_path) { |
105 DCHECK(CurrentlyOnMediaTaskRunnerThread()); | 162 DCHECK(CurrentlyOnMediaTaskRunnerThread()); |
106 | 163 DCHECK(!picasa_data_provider_); |
107 if (++picasa_filesystems_count_ == 1) { | 164 picasa_data_provider_.reset(new picasa::PicasaDataProvider(database_path)); |
108 DCHECK(!picasa_data_provider_); | |
109 picasa_data_provider_.reset(new picasa::PicasaDataProvider(database_path)); | |
110 } | |
111 } | 165 } |
112 | 166 |
113 void ImportedMediaGalleryRegistry::RevokePicasaFileSystem() { | 167 void ImportedMediaGalleryRegistry::RevokePicasaFileSystem() { |
114 DCHECK(CurrentlyOnMediaTaskRunnerThread()); | 168 DCHECK(CurrentlyOnMediaTaskRunnerThread()); |
| 169 DCHECK(picasa_data_provider_); |
| 170 picasa_data_provider_.reset(); |
| 171 } |
115 | 172 |
116 if (--picasa_filesystems_count_ == 0) { | 173 void ImportedMediaGalleryRegistry::RegisterITunesFileSystem( |
117 DCHECK(picasa_data_provider_); | 174 const base::FilePath& xml_library_path) { |
118 picasa_data_provider_.reset(NULL); | 175 DCHECK(CurrentlyOnMediaTaskRunnerThread()); |
119 } | 176 DCHECK(!itunes_data_provider_); |
| 177 itunes_data_provider_.reset(new itunes::ITunesDataProvider(xml_library_path)); |
| 178 } |
| 179 |
| 180 void ImportedMediaGalleryRegistry::RevokeITunesFileSystem() { |
| 181 DCHECK(CurrentlyOnMediaTaskRunnerThread()); |
| 182 DCHECK(itunes_data_provider_); |
| 183 itunes_data_provider_.reset(); |
120 } | 184 } |
121 | 185 |
122 } // namespace chrome | 186 } // namespace chrome |
OLD | NEW |