fileapi_util.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/chromeos/file_manager/fileapi_util.h"
6
7#include "base/files/file_path.h"
8#include "chrome/browser/chromeos/drive/file_system_util.h"
9#include "chrome/browser/extensions/extension_service.h"
10#include "chrome/browser/profiles/profile.h"
11#include "content/public/browser/render_view_host.h"
12#include "content/public/browser/site_instance.h"
13#include "content/public/browser/storage_partition.h"
14#include "extensions/browser/extension_system.h"
15#include "net/base/escape.h"
16#include "url/gurl.h"
17#include "webkit/browser/fileapi/file_system_context.h"
18#include "webkit/common/fileapi/file_system_util.h"
19
20namespace file_manager {
21namespace util {
22
23namespace {
24
25GURL ConvertRelativeFilePathToFileSystemUrl(const base::FilePath& relative_path,
26                                            const std::string& extension_id) {
27  GURL base_url = fileapi::GetFileSystemRootURI(
28      extensions::Extension::GetBaseURLFromExtensionId(extension_id),
29      fileapi::kFileSystemTypeExternal);
30  return GURL(base_url.spec() +
31              net::EscapeUrlEncodedData(relative_path.AsUTF8Unsafe(),
32                                        false));  // Space to %20 instead of +.
33}
34
35}  // namespace
36
37fileapi::FileSystemContext* GetFileSystemContextForExtensionId(
38    Profile* profile,
39    const std::string& extension_id) {
40  GURL site = extensions::ExtensionSystem::Get(profile)->
41      extension_service()->GetSiteForExtensionId(extension_id);
42  return content::BrowserContext::GetStoragePartitionForSite(profile, site)->
43      GetFileSystemContext();
44}
45
46fileapi::FileSystemContext* GetFileSystemContextForRenderViewHost(
47    Profile* profile,
48    content::RenderViewHost* render_view_host) {
49  content::SiteInstance* site_instance = render_view_host->GetSiteInstance();
50  return content::BrowserContext::GetStoragePartition(profile, site_instance)->
51      GetFileSystemContext();
52}
53
54base::FilePath ConvertDrivePathToRelativeFileSystemPath(
55    Profile* profile,
56    const std::string& extension_id,
57    const base::FilePath& drive_path) {
58  // "/special/drive-xxx"
59  base::FilePath path = drive::util::GetDriveMountPointPath(profile);
60  // appended with (|drive_path| - "drive").
61  drive::util::GetDriveGrandRootPath().AppendRelativePath(drive_path, &path);
62
63  base::FilePath relative_path;
64  ConvertAbsoluteFilePathToRelativeFileSystemPath(profile,
65                                                  extension_id,
66                                                  path,
67                                                  &relative_path);
68  return relative_path;
69}
70
71GURL ConvertDrivePathToFileSystemUrl(Profile* profile,
72                                     const base::FilePath& drive_path,
73                                     const std::string& extension_id) {
74  const base::FilePath relative_path =
75      ConvertDrivePathToRelativeFileSystemPath(profile, extension_id,
76                                               drive_path);
77  if (relative_path.empty())
78    return GURL();
79  return ConvertRelativeFilePathToFileSystemUrl(relative_path, extension_id);
80}
81
82bool ConvertAbsoluteFilePathToFileSystemUrl(Profile* profile,
83                                            const base::FilePath& absolute_path,
84                                            const std::string& extension_id,
85                                            GURL* url) {
86  base::FilePath relative_path;
87  if (!ConvertAbsoluteFilePathToRelativeFileSystemPath(profile,
88                                                       extension_id,
89                                                       absolute_path,
90                                                       &relative_path)) {
91    return false;
92  }
93  *url = ConvertRelativeFilePathToFileSystemUrl(relative_path, extension_id);
94  return true;
95}
96
97bool ConvertAbsoluteFilePathToRelativeFileSystemPath(
98    Profile* profile,
99    const std::string& extension_id,
100    const base::FilePath& absolute_path,
101    base::FilePath* virtual_path) {
102  ExtensionService* service =
103      extensions::ExtensionSystem::Get(profile)->extension_service();
104  // May be NULL during unit_tests.
105  if (!service)
106    return false;
107
108  // File browser APIs are meant to be used only from extension context, so the
109  // extension's site is the one in whose file system context the virtual path
110  // should be found.
111  GURL site = service->GetSiteForExtensionId(extension_id);
112  fileapi::ExternalFileSystemBackend* backend =
113      content::BrowserContext::GetStoragePartitionForSite(profile, site)->
114          GetFileSystemContext()->external_backend();
115  if (!backend)
116    return false;
117
118  // Find if this file path is managed by the external backend.
119  if (!backend->GetVirtualPath(absolute_path, virtual_path))
120    return false;
121
122  return true;
123}
124
125}  // namespace util
126}  // namespace file_manager
127