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/file_system_provider/mount_path_util.h"
9 #include "base/stl_util.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h"
13 #include "chrome/browser/chromeos/file_system_provider/service.h"
14 #include "chrome/browser/chromeos/login/user.h"
15 #include "chrome/browser/chromeos/login/user_manager.h"
16 #include "chrome/browser/chromeos/profiles/profile_helper.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "content/public/browser/browser_thread.h"
21 using content::BrowserThread;
24 namespace file_system_provider {
29 // Root mount path for all of the provided file systems.
30 const base::FilePath::CharType kProvidedMountPointRoot[] =
31 FILE_PATH_LITERAL("/provided");
35 base::FilePath GetMountPath(Profile* profile,
36 std::string extension_id,
38 chromeos::User* const user =
39 chromeos::UserManager::IsInitialized()
40 ? chromeos::UserManager::Get()->GetUserByProfile(
41 profile->GetOriginalProfile())
43 const std::string user_suffix = user ? "-" + user->username_hash() : "";
44 return base::FilePath(kProvidedMountPointRoot).AppendASCII(
45 extension_id + "-" + base::IntToString(file_system_id) + user_suffix);
48 FileSystemURLParser::FileSystemURLParser(const fileapi::FileSystemURL& url)
49 : url_(url), file_system_(NULL) {
52 FileSystemURLParser::~FileSystemURLParser() {
55 bool FileSystemURLParser::Parse() {
56 DCHECK_CURRENTLY_ON(BrowserThread::UI);
58 if (url_.type() != fileapi::kFileSystemTypeProvided)
61 // First, find the service handling the mount point of the URL.
62 const std::vector<Profile*>& profiles =
63 g_browser_process->profile_manager()->GetLoadedProfiles();
65 for (size_t i = 0; i < profiles.size(); ++i) {
66 Profile* original_profile = profiles[i]->GetOriginalProfile();
68 if (original_profile != profiles[i] ||
69 chromeos::ProfileHelper::IsSigninProfile(original_profile)) {
73 Service* service = Service::Get(original_profile);
77 ProvidedFileSystemInterface* file_system =
78 service->GetProvidedFileSystem(url_.filesystem_id());
82 // Strip the mount point name from the virtual path, to extract the file
83 // path within the provided file system.
84 file_system_ = file_system;
85 std::vector<base::FilePath::StringType> components;
86 url_.virtual_path().GetComponents(&components);
87 DCHECK_LT(0u, components.size());
88 file_path_ = base::FilePath::FromUTF8Unsafe("/");
89 for (size_t i = 1; i < components.size(); ++i) {
90 // TODO(mtomasz): This could be optimized, to avoid unnecessary copies.
91 file_path_ = file_path_.Append(components[i]);
97 // Nothing has been found.
102 } // namespace file_system_provider
103 } // namespace chromeos