summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media_gallery/media_file_system_registry.cc
blob: 37843e0dca14a2c8d82dedb219aacf8dd4f65d93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// MediaFileSystemRegistry implementation.

#include "chrome/browser/media_gallery/media_file_system_registry.h"

#include <set>

#include "base/path_service.h"
#include "base/sys_string_conversions.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/browser_thread.h"
#include "webkit/fileapi/isolated_context.h"

namespace chrome {

static base::LazyInstance<MediaFileSystemRegistry>::Leaky
    g_media_file_system_registry = LAZY_INSTANCE_INITIALIZER;

using content::BrowserThread;
using fileapi::IsolatedContext;

// static
MediaFileSystemRegistry* MediaFileSystemRegistry::GetInstance() {
  return g_media_file_system_registry.Pointer();
}

std::vector<MediaFileSystemRegistry::MediaFSIDAndPath>
MediaFileSystemRegistry::GetMediaFileSystems() const {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  std::vector<MediaFSIDAndPath> results;
  for (MediaPathToFSIDMap::const_iterator it = media_fs_map_.begin();
       it != media_fs_map_.end();
       ++it) {
    const FilePath path = it->first;
    const std::string fsid = it->second;
    results.push_back(std::make_pair(fsid, path));
  }
  return results;
}

MediaFileSystemRegistry::MediaFileSystemRegistry() {
  FilePath pictures_path;
  if (PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path)) {
    RegisterPathAsFileSystem(pictures_path);
  }
}

MediaFileSystemRegistry::~MediaFileSystemRegistry() {
}

void MediaFileSystemRegistry::RegisterPathAsFileSystem(const FilePath& path) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  // Sanity checks for |path|.
  CHECK(path.IsAbsolute());
  CHECK(!path.ReferencesParent());
  // Make sure |path| does not refer to '/' on Unix.
  // TODO(thestig) Check how BaseName() works for say, 'C:\' on Windows.
  CHECK(!path.BaseName().IsAbsolute());
  CHECK(!path.BaseName().empty());

  std::set<FilePath> fileset;
  fileset.insert(path);
  const std::string fsid =
      IsolatedContext::GetInstance()->RegisterIsolatedFileSystem(fileset);
  CHECK(!fsid.empty());
  media_fs_map_.insert(std::make_pair(path, fsid));
}

}  // namespace chrome