Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / fileapi / external_file_url_util.cc
blob74910d722cc2584c3509efc299e1cebecbd2e109
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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/fileapi/external_file_url_util.h"
7 #include <string>
8 #include <vector>
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/chromeos/file_manager/app_id.h"
13 #include "chrome/browser/chromeos/file_manager/fileapi_util.h"
14 #include "chrome/browser/extensions/extension_util.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/storage_partition.h"
19 #include "content/public/common/url_constants.h"
20 #include "net/base/escape.h"
21 #include "storage/browser/fileapi/file_system_url.h"
23 using content::BrowserThread;
25 namespace chromeos {
27 bool IsExternalFileURLType(storage::FileSystemType type) {
28 return type == storage::kFileSystemTypeDrive ||
29 type == storage::kFileSystemTypeDeviceMediaAsFileStorage ||
30 type == storage::kFileSystemTypeProvided;
33 GURL FileSystemURLToExternalFileURL(
34 const storage::FileSystemURL& file_system_url) {
35 if (file_system_url.mount_type() != storage::kFileSystemTypeExternal ||
36 !IsExternalFileURLType(file_system_url.type())) {
37 return GURL();
40 return GURL(base::StringPrintf(
41 "%s:%s",
42 content::kExternalFileScheme,
43 file_system_url.virtual_path().AsUTF8Unsafe().c_str()));
46 base::FilePath ExternalFileURLToVirtualPath(const GURL& url) {
47 if (!url.is_valid() || url.scheme() != content::kExternalFileScheme)
48 return base::FilePath();
49 const std::string path_string =
50 net::UnescapeURLComponent(url.GetContent(), net::UnescapeRule::NORMAL);
51 return base::FilePath::FromUTF8Unsafe(path_string);
54 GURL CreateExternalFileURLFromPath(Profile* profile,
55 const base::FilePath& path) {
56 DCHECK_CURRENTLY_ON(BrowserThread::UI);
58 GURL raw_file_system_url;
59 if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl(
60 profile,
61 path,
62 file_manager::kFileManagerAppId,
63 &raw_file_system_url)) {
64 return GURL();
67 const storage::FileSystemURL file_system_url =
68 file_manager::util::GetFileSystemContextForExtensionId(
69 profile, file_manager::kFileManagerAppId)
70 ->CrackURL(raw_file_system_url);
71 if (!file_system_url.is_valid())
72 return GURL();
74 return FileSystemURLToExternalFileURL(file_system_url);
77 } // namespace chromeos