Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / iapps_data_provider.cc
blob44856412d242febc4e6be35824c96d2e12d47967
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/media_galleries/fileapi/iapps_data_provider.h"
7 #include <map>
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/format_macros.h"
12 #include "base/location.h"
13 #include "base/logging.h"
14 #include "base/stl_util.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/threading/thread_restrictions.h"
18 #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/common/media_galleries/itunes_library.h"
21 #include "storage/browser/fileapi/native_file_util.h"
22 #include "third_party/icu/source/common/unicode/locid.h"
24 namespace iapps {
26 IAppsDataProvider::IAppsDataProvider(const base::FilePath& library_path)
27 : library_path_(library_path),
28 needs_refresh_(true),
29 is_valid_(false),
30 weak_factory_(this) {
31 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
32 DCHECK(!library_path_.empty());
34 StartFilePathWatchOnMediaTaskRunner(
35 library_path_,
36 base::Bind(&IAppsDataProvider::OnLibraryWatchStarted,
37 weak_factory_.GetWeakPtr()),
38 base::Bind(&IAppsDataProvider::OnLibraryChanged,
39 weak_factory_.GetWeakPtr()));
42 IAppsDataProvider::~IAppsDataProvider() {}
44 bool IAppsDataProvider::valid() const {
45 return is_valid_;
48 void IAppsDataProvider::set_valid(bool valid) {
49 is_valid_ = valid;
52 void IAppsDataProvider::RefreshData(const ReadyCallback& ready_callback) {
53 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
54 if (!needs_refresh_) {
55 ready_callback.Run(valid());
56 return;
59 // TODO(gbillock): this needs re-examination. Could be a refresh bug.
60 needs_refresh_ = false;
61 DoParseLibrary(library_path_, ready_callback);
64 const base::FilePath& IAppsDataProvider::library_path() const {
65 return library_path_;
68 void IAppsDataProvider::OnLibraryWatchStarted(
69 scoped_ptr<base::FilePathWatcher> library_watcher) {
70 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
71 library_watcher_.reset(library_watcher.release());
74 void IAppsDataProvider::OnLibraryChanged(const base::FilePath& path,
75 bool error) {
76 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
77 DCHECK_EQ(library_path_.value(), path.value());
78 if (error)
79 LOG(ERROR) << "Error watching " << library_path_.value();
80 needs_refresh_ = true;
83 } // namespace iapps