Home | History | Annotate | Download | only in file_manager
      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/extensions/extension_service.h"
      9 #include "chrome/browser/extensions/extension_system.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "content/public/browser/browser_thread.h"
     12 #include "content/public/browser/render_view_host.h"
     13 #include "content/public/browser/site_instance.h"
     14 #include "content/public/browser/storage_partition.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 
     20 namespace file_manager {
     21 namespace util {
     22 
     23 fileapi::FileSystemContext* GetFileSystemContextForExtensionId(
     24     Profile* profile,
     25     const std::string& extension_id) {
     26   GURL site = extensions::ExtensionSystem::Get(profile)->
     27       extension_service()->GetSiteForExtensionId(extension_id);
     28   return content::BrowserContext::GetStoragePartitionForSite(profile, site)->
     29       GetFileSystemContext();
     30 }
     31 
     32 fileapi::FileSystemContext* GetFileSystemContextForRenderViewHost(
     33     Profile* profile,
     34     content::RenderViewHost* render_view_host) {
     35   content::SiteInstance* site_instance = render_view_host->GetSiteInstance();
     36   return content::BrowserContext::GetStoragePartition(profile, site_instance)->
     37       GetFileSystemContext();
     38 }
     39 
     40 GURL ConvertRelativeFilePathToFileSystemUrl(const base::FilePath& relative_path,
     41                                             const std::string& extension_id) {
     42   GURL base_url = fileapi::GetFileSystemRootURI(
     43       extensions::Extension::GetBaseURLFromExtensionId(extension_id),
     44       fileapi::kFileSystemTypeExternal);
     45   return GURL(base_url.spec() +
     46               net::EscapeUrlEncodedData(relative_path.AsUTF8Unsafe(),
     47                                         false));  // Space to %20 instead of +.
     48 }
     49 
     50 bool ConvertAbsoluteFilePathToFileSystemUrl(
     51     Profile* profile,
     52     const base::FilePath& absolute_path,
     53     const std::string& extension_id,
     54     GURL* url) {
     55   base::FilePath relative_path;
     56   if (!ConvertAbsoluteFilePathToRelativeFileSystemPath(
     57           profile,
     58           extension_id,
     59           absolute_path,
     60           &relative_path)) {
     61     return false;
     62   }
     63   *url = ConvertRelativeFilePathToFileSystemUrl(relative_path, extension_id);
     64   return true;
     65 }
     66 
     67 bool ConvertAbsoluteFilePathToRelativeFileSystemPath(
     68     Profile* profile,
     69     const std::string& extension_id,
     70     const base::FilePath& absolute_path,
     71     base::FilePath* virtual_path) {
     72   ExtensionService* service =
     73       extensions::ExtensionSystem::Get(profile)->extension_service();
     74   // May be NULL during unit_tests.
     75   if (!service)
     76     return false;
     77 
     78   // File browser APIs are meant to be used only from extension context, so the
     79   // extension's site is the one in whose file system context the virtual path
     80   // should be found.
     81   GURL site = service->GetSiteForExtensionId(extension_id);
     82   fileapi::ExternalFileSystemBackend* backend =
     83       content::BrowserContext::GetStoragePartitionForSite(profile, site)->
     84           GetFileSystemContext()->external_backend();
     85   if (!backend)
     86     return false;
     87 
     88   // Find if this file path is managed by the external backend.
     89   if (!backend->GetVirtualPath(absolute_path, virtual_path))
     90     return false;
     91 
     92   return true;
     93 }
     94 
     95 }  // namespace util
     96 }  // namespace file_manager
     97