8c03a6217c856633f833e7ca02c59bbc915ecd92
[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 "content/public/browser/browser_thread.h"
23 #include "third_party/icu/source/common/unicode/locid.h"
24 #include "webkit/browser/fileapi/native_file_util.h"
25
26 namespace iapps {
27
28 IAppsDataProvider::IAppsDataProvider(const base::FilePath& library_path)
29     : library_path_(library_path),
30       needs_refresh_(true),
31       is_valid_(false),
32       weak_factory_(this) {
33   DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
34   DCHECK(!library_path_.empty());
35
36   StartFilePathWatchOnMediaTaskRunner(
37       library_path_,
38       base::Bind(&IAppsDataProvider::OnLibraryWatchStarted,
39                  weak_factory_.GetWeakPtr()),
40       base::Bind(&IAppsDataProvider::OnLibraryChanged,
41                  weak_factory_.GetWeakPtr()));
42 }
43
44 IAppsDataProvider::~IAppsDataProvider() {}
45
46 bool IAppsDataProvider::valid() const {
47   return is_valid_;
48 }
49
50 void IAppsDataProvider::set_valid(bool valid) {
51   is_valid_ = valid;
52 }
53
54 void IAppsDataProvider::RefreshData(const ReadyCallback& ready_callback) {
55   DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
56   if (!needs_refresh_) {
57     ready_callback.Run(valid());
58     return;
59   }
60
61   // TODO(gbillock): this needs re-examination. Could be a refresh bug.
62   needs_refresh_ = false;
63   DoParseLibrary(library_path_, ready_callback);
64 }
65
66 const base::FilePath& IAppsDataProvider::library_path() const {
67   return library_path_;
68 }
69
70 void IAppsDataProvider::OnLibraryWatchStarted(
71     scoped_ptr<base::FilePathWatcher> library_watcher) {
72   DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
73   library_watcher_.reset(library_watcher.release());
74 }
75
76 void IAppsDataProvider::OnLibraryChanged(const base::FilePath& path,
77                                          bool error) {
78   DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
79   DCHECK_EQ(library_path_.value(), path.value());
80   if (error)
81     LOG(ERROR) << "Error watching " << library_path_.value();
82   needs_refresh_ = true;
83 }
84
85 }  // namespace iapps