Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / fileapi / iapps_data_provider.cc
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.
4
5 #include "chrome/browser/media_galleries/fileapi/iapps_data_provider.h"
6
7 #include <map>
8
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/platform_file.h"
15 #include "base/stl_util.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/threading/thread_restrictions.h"
19 #include "chrome/browser/media_galleries/fileapi/file_path_watcher_util.h"
20 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
21 #include "chrome/common/media_galleries/itunes_library.h"
22 #include "third_party/icu/source/common/unicode/locid.h"
23 #include "webkit/browser/fileapi/native_file_util.h"
24
25 namespace iapps {
26
27 IAppsDataProvider::IAppsDataProvider(const base::FilePath& library_path)
28     : library_path_(library_path),
29       needs_refresh_(true),
30       is_valid_(false),
31       weak_factory_(this) {
32   DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
33   DCHECK(!library_path_.empty());
34
35   StartFilePathWatchOnMediaTaskRunner(
36       library_path_,
37       base::Bind(&IAppsDataProvider::OnLibraryWatchStarted,
38                  weak_factory_.GetWeakPtr()),
39       base::Bind(&IAppsDataProvider::OnLibraryChanged,
40                  weak_factory_.GetWeakPtr()));
41 }
42
43 IAppsDataProvider::~IAppsDataProvider() {}
44
45 bool IAppsDataProvider::valid() const {
46   return is_valid_;
47 }
48
49 void IAppsDataProvider::set_valid(bool valid) {
50   is_valid_ = valid;
51 }
52
53 void IAppsDataProvider::RefreshData(const ReadyCallback& ready_callback) {
54   DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
55   if (!needs_refresh_) {
56     ready_callback.Run(valid());
57     return;
58   }
59
60   // TODO(gbillock): this needs re-examination. Could be a refresh bug.
61   needs_refresh_ = false;
62   DoParseLibrary(library_path_, ready_callback);
63 }
64
65 const base::FilePath& IAppsDataProvider::library_path() const {
66   return library_path_;
67 }
68
69 void IAppsDataProvider::OnLibraryWatchStarted(
70     scoped_ptr<base::FilePathWatcher> library_watcher) {
71   DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
72   library_watcher_.reset(library_watcher.release());
73 }
74
75 void IAppsDataProvider::OnLibraryChanged(const base::FilePath& path,
76                                          bool error) {
77   DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
78   DCHECK_EQ(library_path_.value(), path.value());
79   if (error)
80     LOG(ERROR) << "Error watching " << library_path_.value();
81   needs_refresh_ = true;
82 }
83
84 }  // namespace iapps