1 // Copyright 2016 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/arc/fileapi/arc_content_file_system_url_util.h"
6 
7 #include "base/files/file_path.h"
8 #include "chrome/browser/chromeos/fileapi/external_file_url_util.h"
9 #include "net/base/escape.h"
10 #include "storage/browser/file_system/file_system_url.h"
11 
12 namespace arc {
13 
14 const char kContentFileSystemMountPointName[] = "arc-content";
15 const char kFileSystemFileproviderUrl[] =
16     "content://org.chromium.arc.file_system.fileprovider/";
17 
18 const base::FilePath::CharType kContentFileSystemMountPointPath[] =
19     FILE_PATH_LITERAL("/special/arc-content");
20 
EscapeArcUrl(const GURL & arc_url)21 std::string EscapeArcUrl(const GURL& arc_url) {
22   return net::EscapeQueryParamValue(arc_url.spec(), false);
23 }
24 
UnescapeArcUrl(const std::string & escaped_arc_url)25 GURL UnescapeArcUrl(const std::string& escaped_arc_url) {
26   return GURL(net::UnescapeBinaryURLComponent(escaped_arc_url));
27 }
28 
ArcUrlToExternalFileUrl(const GURL & arc_url)29 GURL ArcUrlToExternalFileUrl(const GURL& arc_url) {
30   // Return "externalfile:arc-content/<|arc_url| escaped>".
31   base::FilePath virtual_path =
32       base::FilePath::FromUTF8Unsafe(kContentFileSystemMountPointName)
33           .Append(base::FilePath::FromUTF8Unsafe(EscapeArcUrl(arc_url)));
34   return chromeos::VirtualPathToExternalFileURL(virtual_path);
35 }
36 
ExternalFileUrlToArcUrl(const GURL & external_file_url)37 GURL ExternalFileUrlToArcUrl(const GURL& external_file_url) {
38   base::FilePath virtual_path =
39       chromeos::ExternalFileURLToVirtualPath(external_file_url);
40   base::FilePath path_after_root;
41   if (!base::FilePath::FromUTF8Unsafe(kContentFileSystemMountPointName)
42            .AppendRelativePath(virtual_path, &path_after_root)) {
43     return GURL();
44   }
45   return UnescapeArcUrl(path_after_root.AsUTF8Unsafe());
46 }
47 
FileSystemUrlToArcUrl(const storage::FileSystemURL & url)48 GURL FileSystemUrlToArcUrl(const storage::FileSystemURL& url) {
49   return PathToArcUrl(url.path());
50 }
51 
PathToArcUrl(const base::FilePath & path)52 GURL PathToArcUrl(const base::FilePath& path) {
53   base::FilePath path_after_mount_point;
54   if (!base::FilePath(kContentFileSystemMountPointPath)
55            .AppendRelativePath(path, &path_after_mount_point)) {
56     return GURL();
57   }
58   return UnescapeArcUrl(path_after_mount_point.AsUTF8Unsafe());
59 }
60 
61 }  // namespace arc
62