Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / mount_path_util.cc
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.
4
5 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h"
6
7 #include <vector>
8
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"
20
21 using content::BrowserThread;
22
23 namespace chromeos {
24 namespace file_system_provider {
25 namespace util {
26
27 namespace {
28
29 // Root mount path for all of the provided file systems.
30 const base::FilePath::CharType kProvidedMountPointRoot[] =
31     FILE_PATH_LITERAL("/provided");
32
33 }  // namespace
34
35 base::FilePath GetMountPath(Profile* profile,
36                             std::string extension_id,
37                             int file_system_id) {
38   chromeos::User* const user =
39       chromeos::UserManager::IsInitialized()
40           ? chromeos::UserManager::Get()->GetUserByProfile(
41                 profile->GetOriginalProfile())
42           : NULL;
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);
46 }
47
48 FileSystemURLParser::FileSystemURLParser(const fileapi::FileSystemURL& url)
49     : url_(url), file_system_(NULL) {
50 }
51
52 FileSystemURLParser::~FileSystemURLParser() {
53 }
54
55 bool FileSystemURLParser::Parse() {
56   DCHECK_CURRENTLY_ON(BrowserThread::UI);
57
58   if (url_.type() != fileapi::kFileSystemTypeProvided)
59     return false;
60
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();
64
65   for (size_t i = 0; i < profiles.size(); ++i) {
66     Profile* original_profile = profiles[i]->GetOriginalProfile();
67
68     if (original_profile != profiles[i] ||
69         chromeos::ProfileHelper::IsSigninProfile(original_profile)) {
70       continue;
71     }
72
73     Service* service = Service::Get(original_profile);
74     if (!service)
75       continue;
76
77     ProvidedFileSystemInterface* file_system =
78         service->GetProvidedFileSystem(url_.filesystem_id());
79     if (!file_system)
80       continue;
81
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]);
92     }
93
94     return true;
95   }
96
97   // Nothing has been found.
98   return false;
99 }
100
101 }  // namespace util
102 }  // namespace file_system_provider
103 }  // namespace chromeos