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_finder_impl.h"
6
7#include "base/bind.h"
8#include "base/logging.h"
9#include "content/public/browser/browser_thread.h"
10
11namespace iapps {
12
13namespace {
14
15void PostResultToUIThread(storage_monitor::StorageInfo::Type type,
16                          const IAppsFinderCallback& callback,
17                          const std::string& unique_id) {
18  DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
19
20  std::string device_id;
21  if (!unique_id.empty())
22    device_id = storage_monitor::StorageInfo::MakeDeviceId(type, unique_id);
23
24  content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
25                                   base::Bind(callback, device_id));
26}
27
28}  // namespace
29
30void FindIAppsOnFileThread(storage_monitor::StorageInfo::Type type,
31                           const IAppsFinderTask& task,
32                           const IAppsFinderCallback& callback) {
33  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
34  DCHECK(!task.is_null());
35  DCHECK(!callback.is_null());
36
37  content::BrowserThread::PostTask(
38      content::BrowserThread::FILE,
39      FROM_HERE,
40      base::Bind(task, base::Bind(PostResultToUIThread, type, callback)));
41}
42
43// iPhoto is only supported on OSX.
44#if !defined(OS_MACOSX)
45void FindIPhotoLibrary(const IAppsFinderCallback& callback) {
46  callback.Run(std::string());
47}
48#endif  // !defined(OS_MACOSX)
49
50// iTunes is only support on OSX and Windows.
51#if !defined(OS_MACOSX) && !defined(OS_WIN)
52void FindITunesLibrary(const IAppsFinderCallback& callback) {
53  callback.Run(std::string());
54}
55#endif
56
57bool PathIndicatesIPhotoLibrary(const std::string& device_id,
58                                const base::FilePath& path) {
59  storage_monitor::StorageInfo::Type device_type;
60  std::string unique_id;
61  storage_monitor::StorageInfo::CrackDeviceId(
62      device_id, &device_type, &unique_id);
63  if (device_type != storage_monitor::StorageInfo::IPHOTO)
64    return false;
65
66  // |unique_id| is the path to the library XML file at the library root.
67  base::FilePath library_root =
68      base::FilePath::FromUTF8Unsafe(unique_id).DirName();
69  return (path == library_root) ||
70      (path == library_root.AppendASCII("Data")) ||
71      (path == library_root.AppendASCII("Originals")) ||
72      (path == library_root.AppendASCII("Masters"));
73}
74
75bool PathIndicatesITunesLibrary(const std::string& device_id,
76                                const base::FilePath& path) {
77  storage_monitor::StorageInfo::Type device_type;
78  std::string unique_id;
79  storage_monitor::StorageInfo::CrackDeviceId(
80      device_id, &device_type, &unique_id);
81  if (device_type != storage_monitor::StorageInfo::ITUNES)
82    return false;
83
84  // |unique_id| is the path to the library XML file at the library root.
85  base::FilePath library_root =
86      base::FilePath::FromUTF8Unsafe(unique_id).DirName();
87  return (path == library_root) ||
88      (path == library_root.AppendASCII("iTunes Media")) ||
89      (path == library_root.AppendASCII("iTunes Media").AppendASCII("Music"));
90}
91
92}  // namespace iapps
93