| 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/fileapi/itunes_data_provider.h" | 5 #include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
| 19 #include "chrome/browser/media_galleries/fileapi/file_path_watcher_util.h" |
| 19 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | 20 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
| 20 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" | 21 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
| 21 #include "chrome/common/media_galleries/itunes_library.h" | 22 #include "chrome/common/media_galleries/itunes_library.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 23 #include "third_party/icu/source/common/unicode/locid.h" | 24 #include "third_party/icu/source/common/unicode/locid.h" |
| 24 #include "webkit/browser/fileapi/native_file_util.h" | 25 #include "webkit/browser/fileapi/native_file_util.h" |
| 25 | 26 |
| 26 using chrome::MediaFileSystemBackend; | 27 using chrome::MediaFileSystemBackend; |
| 27 | 28 |
| 28 namespace itunes { | 29 namespace itunes { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 typedef base::Callback<void(scoped_ptr<base::FilePathWatcher> watcher)> | |
| 33 FileWatchStartedCallback; | |
| 34 | |
| 35 // Colon and slash are not allowed in filenames, replace them with underscore. | 33 // Colon and slash are not allowed in filenames, replace them with underscore. |
| 36 std::string EscapeBadCharacters(const std::string& input) { | 34 std::string EscapeBadCharacters(const std::string& input) { |
| 37 std::string result; | 35 std::string result; |
| 38 ReplaceChars(input, ":/", "_", &result); | 36 ReplaceChars(input, ":/", "_", &result); |
| 39 return result; | 37 return result; |
| 40 } | 38 } |
| 41 | 39 |
| 42 ITunesDataProvider::Album MakeUniqueTrackNames(const parser::Album& album) { | 40 ITunesDataProvider::Album MakeUniqueTrackNames(const parser::Album& album) { |
| 43 // TODO(vandebo): It would be nice to ensure that names returned from here | 41 // TODO(vandebo): It would be nice to ensure that names returned from here |
| 44 // are stable, but aside from persisting every name returned, it's not | 42 // are stable, but aside from persisting every name returned, it's not |
| (...skipping 30 matching lines...) Expand all Loading... |
| 75 std::string escaped_track_name = | 73 std::string escaped_track_name = |
| 76 EscapeBadCharacters(uniquified_track_name); | 74 EscapeBadCharacters(uniquified_track_name); |
| 77 result[escaped_track_name] = (*track_it)->location; | 75 result[escaped_track_name] = (*track_it)->location; |
| 78 } | 76 } |
| 79 } | 77 } |
| 80 } | 78 } |
| 81 | 79 |
| 82 return result; | 80 return result; |
| 83 } | 81 } |
| 84 | 82 |
| 85 // Bounces |path| and |error| to |callback| from the FILE thread to the media | |
| 86 // task runner. | |
| 87 void OnLibraryChanged(const base::FilePathWatcher::Callback& callback, | |
| 88 const base::FilePath& path, | |
| 89 bool error) { | |
| 90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 91 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
| 92 FROM_HERE, base::Bind(callback, path, error)); | |
| 93 } | |
| 94 | |
| 95 // The watch has to be started on the FILE thread, and the callback called by | |
| 96 // the FilePathWatcher also needs to run on the FILE thread. | |
| 97 void StartLibraryWatchOnFileThread( | |
| 98 const base::FilePath& library_path, | |
| 99 const FileWatchStartedCallback& watch_started_callback, | |
| 100 const base::FilePathWatcher::Callback& library_changed_callback) { | |
| 101 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 102 // The watcher is created on the FILE thread because it is very difficult | |
| 103 // to safely pass an already-created member to a different thread. | |
| 104 scoped_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher); | |
| 105 bool success = watcher->Watch( | |
| 106 library_path, false /*recursive*/, | |
| 107 base::Bind(&OnLibraryChanged, library_changed_callback)); | |
| 108 if (!success) | |
| 109 LOG(ERROR) << "Adding watch for " << library_path.value() << " failed"; | |
| 110 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
| 111 FROM_HERE, | |
| 112 base::Bind(watch_started_callback, base::Passed(&watcher))); | |
| 113 } | |
| 114 | |
| 115 // |result_path| is set if |locale_string| maps to a localized directory name | 83 // |result_path| is set if |locale_string| maps to a localized directory name |
| 116 // and it exists in the filesystem. | 84 // and it exists in the filesystem. |
| 117 bool CheckLocaleStringAutoAddPath( | 85 bool CheckLocaleStringAutoAddPath( |
| 118 const base::FilePath& media_path, | 86 const base::FilePath& media_path, |
| 119 const std::map<std::string, std::string>& localized_dir_names, | 87 const std::map<std::string, std::string>& localized_dir_names, |
| 120 const std::string& locale_string, | 88 const std::string& locale_string, |
| 121 base::FilePath* result_path) { | 89 base::FilePath* result_path) { |
| 122 DCHECK(!media_path.empty()); | 90 DCHECK(!media_path.empty()); |
| 123 DCHECK(!localized_dir_names.empty()); | 91 DCHECK(!localized_dir_names.empty()); |
| 124 DCHECK(!locale_string.empty()); | 92 DCHECK(!locale_string.empty()); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 181 |
| 214 ITunesDataProvider::ITunesDataProvider(const base::FilePath& library_path) | 182 ITunesDataProvider::ITunesDataProvider(const base::FilePath& library_path) |
| 215 : library_path_(library_path), | 183 : library_path_(library_path), |
| 216 auto_add_path_(GetAutoAddPath(library_path)), | 184 auto_add_path_(GetAutoAddPath(library_path)), |
| 217 needs_refresh_(true), | 185 needs_refresh_(true), |
| 218 is_valid_(false), | 186 is_valid_(false), |
| 219 weak_factory_(this) { | 187 weak_factory_(this) { |
| 220 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 188 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 221 DCHECK(!library_path_.empty()); | 189 DCHECK(!library_path_.empty()); |
| 222 | 190 |
| 223 content::BrowserThread::PostTask( | 191 chrome::StartFilePathWatchOnMediaTaskRunner( |
| 224 content::BrowserThread::FILE, | 192 library_path_, |
| 225 FROM_HERE, | 193 false /* recursive */, |
| 226 base::Bind(StartLibraryWatchOnFileThread, | 194 base::Bind(&ITunesDataProvider::OnLibraryWatchStarted, |
| 227 library_path_, | 195 weak_factory_.GetWeakPtr()), |
| 228 base::Bind(&ITunesDataProvider::OnLibraryWatchStarted, | 196 base::Bind(&ITunesDataProvider::OnLibraryChanged, |
| 229 weak_factory_.GetWeakPtr()), | 197 weak_factory_.GetWeakPtr())); |
| 230 base::Bind(&ITunesDataProvider::OnLibraryChanged, | |
| 231 weak_factory_.GetWeakPtr()))); | |
| 232 } | 198 } |
| 233 | 199 |
| 234 ITunesDataProvider::~ITunesDataProvider() {} | 200 ITunesDataProvider::~ITunesDataProvider() {} |
| 235 | 201 |
| 236 // TODO(vandebo): add a file watch that resets |needs_refresh_| when the | 202 // TODO(vandebo): add a file watch that resets |needs_refresh_| when the |
| 237 // file changes. | 203 // file changes. |
| 238 void ITunesDataProvider::RefreshData(const ReadyCallback& ready_callback) { | 204 void ITunesDataProvider::RefreshData(const ReadyCallback& ready_callback) { |
| 239 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 205 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 240 if (!needs_refresh_) { | 206 if (!needs_refresh_) { |
| 241 ready_callback.Run(is_valid_); | 207 ready_callback.Run(is_valid_); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 std::string album_name = EscapeBadCharacters(album_it->first); | 335 std::string album_name = EscapeBadCharacters(album_it->first); |
| 370 library_[artist_name][album_name] = | 336 library_[artist_name][album_name] = |
| 371 MakeUniqueTrackNames(album_it->second); | 337 MakeUniqueTrackNames(album_it->second); |
| 372 } | 338 } |
| 373 } | 339 } |
| 374 } | 340 } |
| 375 ready_callback.Run(is_valid_); | 341 ready_callback.Run(is_valid_); |
| 376 } | 342 } |
| 377 | 343 |
| 378 } // namespace itunes | 344 } // namespace itunes |
| OLD | NEW |