OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/fileapi/external_file_url_util.h" | 5 #include "chrome/browser/chromeos/fileapi/external_file_url_util.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
| 8 #include <vector> |
8 | 9 |
| 10 #include "base/strings/string_util.h" |
9 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
10 #include "chrome/browser/chromeos/drive/file_system_util.h" | 12 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 13 #include "chrome/browser/chromeos/file_manager/app_id.h" |
| 14 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |
| 15 #include "chrome/browser/extensions/extension_util.h" |
| 16 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 18 #include "content/public/browser/browser_context.h" |
12 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/storage_partition.h" |
13 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
| 22 #include "storage/browser/fileapi/file_system_url.h" |
14 | 23 |
15 using content::BrowserThread; | 24 using content::BrowserThread; |
16 | 25 |
17 namespace chromeos { | 26 namespace chromeos { |
| 27 namespace { |
18 | 28 |
19 GURL FilePathToExternalFileURL(const base::FilePath& path) { | 29 const char kDriveMountNamePrefix[] = "drive"; |
20 std::string url(base::StringPrintf( | 30 const char kMaskedDriveMountName[] = "drive"; |
21 "%s:%s", chrome::kExternalFileScheme, path.AsUTF8Unsafe().c_str())); | 31 |
| 32 } // namespace |
| 33 |
| 34 GURL FileSystemURLToExternalFileURL( |
| 35 const storage::FileSystemURL& file_system_url) { |
| 36 base::FilePath virtual_path; |
| 37 |
| 38 switch (file_system_url.type()) { |
| 39 case storage::kFileSystemTypeDrive: { |
| 40 // Mask the profile hash in the mount point name. |
| 41 std::vector<base::FilePath::StringType> components; |
| 42 file_system_url.virtual_path().GetComponents(&components); |
| 43 if (components.size() <= 1) |
| 44 return GURL(); |
| 45 DCHECK(StartsWithASCII(components[0], kDriveMountNamePrefix, true)); |
| 46 virtual_path = base::FilePath(kMaskedDriveMountName); |
| 47 for (size_t i = 1; i < components.size(); ++i) { |
| 48 virtual_path = virtual_path.Append(components[i]); |
| 49 } |
| 50 break; |
| 51 } |
| 52 |
| 53 case storage::kFileSystemTypeDeviceMediaAsFileStorage: |
| 54 case storage::kFileSystemTypeProvided: |
| 55 virtual_path = file_system_url.virtual_path(); |
| 56 break; |
| 57 |
| 58 default: |
| 59 return GURL(); |
| 60 } |
| 61 |
| 62 std::string url(base::StringPrintf("%s:%s", |
| 63 chrome::kExternalFileScheme, |
| 64 virtual_path.AsUTF8Unsafe().c_str())); |
22 return GURL(url); | 65 return GURL(url); |
23 } | 66 } |
24 | 67 |
25 base::FilePath ExternalFileURLToFilePath(const GURL& url) { | 68 base::FilePath ExternalFileURLToVirtualPath(Profile* profile, const GURL& url) { |
26 if (!url.is_valid() || url.scheme() != chrome::kExternalFileScheme) | 69 const base::FilePath virtual_path = base::FilePath::FromUTF8Unsafe( |
27 return base::FilePath(); | 70 net::UnescapeURLComponent(url.GetContent(), net::UnescapeRule::NORMAL)); |
28 std::string path_string = | 71 |
29 net::UnescapeURLComponent(url.GetContent(), net::UnescapeRule::NORMAL); | 72 const base::FilePath masked_drive_mount_name(kMaskedDriveMountName); |
30 return base::FilePath::FromUTF8Unsafe(path_string); | 73 if (masked_drive_mount_name != virtual_path && |
| 74 !masked_drive_mount_name.IsParent(virtual_path)) |
| 75 return virtual_path; |
| 76 |
| 77 // Unmask the drive hash. |
| 78 base::FilePath drive_virtual_path = |
| 79 drive::util::GetDriveMountPointPath(profile).BaseName(); |
| 80 base::FilePath(kMaskedDriveMountName) |
| 81 .AppendRelativePath(virtual_path, &drive_virtual_path); |
| 82 return drive_virtual_path; |
31 } | 83 } |
32 | 84 |
33 void MaybeSetExternalFileURL(Profile* profile, | 85 void MaybeSetExternalFileURL(Profile* profile, |
34 const base::FilePath& path, | 86 const base::FilePath& path, |
35 GURL* url) { | 87 GURL* url) { |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
37 | 89 |
38 if (!drive::util::IsUnderDriveMountPoint(path)) | 90 GURL raw_file_system_url; |
| 91 if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl( |
| 92 profile, |
| 93 path, |
| 94 file_manager::kFileManagerAppId, |
| 95 &raw_file_system_url)) { |
39 return; | 96 return; |
| 97 } |
40 | 98 |
41 drive::FileSystemInterface* file_system = | 99 const GURL site = extensions::util::GetSiteForExtensionId( |
42 drive::util::GetFileSystemByProfile(profile); | 100 file_manager::kFileManagerAppId, profile); |
43 if (!file_system) | 101 const storage::FileSystemURL file_system_url = |
44 return; | 102 content::BrowserContext::GetStoragePartitionForSite(profile, site) |
| 103 ->GetFileSystemContext() |
| 104 ->CrackURL(raw_file_system_url); |
45 | 105 |
46 *url = FilePathToExternalFileURL(drive::util::ExtractDrivePath(path)); | 106 const GURL external_file_url = |
| 107 FileSystemURLToExternalFileURL(file_system_url); |
| 108 if (!external_file_url.is_empty()) |
| 109 *url = external_file_url; |
47 } | 110 } |
48 | 111 |
49 } // namespace chromeos | 112 } // namespace chromeos |
OLD | NEW |