Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(642)

Unified Diff: chrome/browser/chromeos/fileapi/external_file_url_util.cc

Issue 589473002: Files.app: Enable externalfile: protocol for MTP volumes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/fileapi/external_file_url_util.cc
diff --git a/chrome/browser/chromeos/fileapi/external_file_url_util.cc b/chrome/browser/chromeos/fileapi/external_file_url_util.cc
index 9ec10794c8add0f070f7eff12fb3aab631142d29..871563d70d30499bdbfa84f3e6322cddae881299 100644
--- a/chrome/browser/chromeos/fileapi/external_file_url_util.cc
+++ b/chrome/browser/chromeos/fileapi/external_file_url_util.cc
@@ -5,29 +5,81 @@
#include "chrome/browser/chromeos/fileapi/external_file_url_util.h"
#include <string>
+#include <vector>
+#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
+#include "chrome/browser/chromeos/file_manager/app_id.h"
+#include "chrome/browser/chromeos/file_manager/fileapi_util.h"
+#include "chrome/browser/extensions/extension_util.h"
+#include "chrome/browser/profiles/profile.h"
#include "chrome/common/url_constants.h"
+#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/storage_partition.h"
#include "net/base/escape.h"
+#include "storage/browser/fileapi/file_system_url.h"
using content::BrowserThread;
namespace chromeos {
+namespace {
-GURL FilePathToExternalFileURL(const base::FilePath& path) {
- std::string url(base::StringPrintf(
- "%s:%s", chrome::kExternalFileScheme, path.AsUTF8Unsafe().c_str()));
+const char kDriveMountNamePrefix[] = "drive";
+const char kMaskedDriveMountName[] = "drive";
+
+} // namespace
+
+GURL FileSystemURLToExternalFileURL(
+ const storage::FileSystemURL& file_system_url) {
+ base::FilePath virtual_path;
+
+ switch (file_system_url.type()) {
+ case storage::kFileSystemTypeDrive: {
+ // Mask the profile hash in the mount point name.
+ std::vector<base::FilePath::StringType> components;
+ file_system_url.virtual_path().GetComponents(&components);
+ if (components.size() <= 1)
+ return GURL();
+ DCHECK(StartsWithASCII(components[0], kDriveMountNamePrefix, true));
+ virtual_path = base::FilePath(kMaskedDriveMountName);
+ for (size_t i = 1; i < components.size(); ++i) {
+ virtual_path = virtual_path.Append(components[i]);
+ }
+ break;
+ }
+
+ case storage::kFileSystemTypeDeviceMediaAsFileStorage:
+ case storage::kFileSystemTypeProvided:
+ virtual_path = file_system_url.virtual_path();
+ break;
+
+ default:
+ return GURL();
+ }
+
+ std::string url(base::StringPrintf("%s:%s",
+ chrome::kExternalFileScheme,
+ virtual_path.AsUTF8Unsafe().c_str()));
return GURL(url);
}
-base::FilePath ExternalFileURLToFilePath(const GURL& url) {
- if (!url.is_valid() || url.scheme() != chrome::kExternalFileScheme)
- return base::FilePath();
- std::string path_string =
- net::UnescapeURLComponent(url.GetContent(), net::UnescapeRule::NORMAL);
- return base::FilePath::FromUTF8Unsafe(path_string);
+base::FilePath ExternalFileURLToVirtualPath(Profile* profile, const GURL& url) {
+ const base::FilePath virtual_path = base::FilePath::FromUTF8Unsafe(
+ net::UnescapeURLComponent(url.GetContent(), net::UnescapeRule::NORMAL));
+
+ const base::FilePath masked_drive_mount_name(kMaskedDriveMountName);
+ if (masked_drive_mount_name != virtual_path &&
+ !masked_drive_mount_name.IsParent(virtual_path))
+ return virtual_path;
+
+ // Unmask the drive hash.
+ base::FilePath drive_virtual_path =
+ drive::util::GetDriveMountPointPath(profile).BaseName();
+ base::FilePath(kMaskedDriveMountName)
+ .AppendRelativePath(virtual_path, &drive_virtual_path);
+ return drive_virtual_path;
}
void MaybeSetExternalFileURL(Profile* profile,
@@ -35,15 +87,26 @@ void MaybeSetExternalFileURL(Profile* profile,
GURL* url) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- if (!drive::util::IsUnderDriveMountPoint(path))
+ GURL raw_file_system_url;
+ if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl(
+ profile,
+ path,
+ file_manager::kFileManagerAppId,
+ &raw_file_system_url)) {
return;
+ }
- drive::FileSystemInterface* file_system =
- drive::util::GetFileSystemByProfile(profile);
- if (!file_system)
- return;
+ const GURL site = extensions::util::GetSiteForExtensionId(
+ file_manager::kFileManagerAppId, profile);
+ const storage::FileSystemURL file_system_url =
+ content::BrowserContext::GetStoragePartitionForSite(profile, site)
+ ->GetFileSystemContext()
+ ->CrackURL(raw_file_system_url);
- *url = FilePathToExternalFileURL(drive::util::ExtractDrivePath(path));
+ const GURL external_file_url =
+ FileSystemURLToExternalFileURL(file_system_url);
+ if (!external_file_url.is_empty())
+ *url = external_file_url;
}
} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698