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/file_system_provider/service.h" | 5 #include "chrome/browser/chromeos/file_system_provider/service.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "chrome/browser/chromeos/file_system_provider/observer.h" | 9 #include "chrome/browser/chromeos/file_system_provider/observer.h" |
10 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" | 10 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 const base::FilePath::CharType kProvidedMountPointRoot[] = | 22 const base::FilePath::CharType kProvidedMountPointRoot[] = |
23 FILE_PATH_LITERAL("/provided"); | 23 FILE_PATH_LITERAL("/provided"); |
24 | 24 |
25 // Maximum number of file systems to be mounted in the same time, per profile. | 25 // Maximum number of file systems to be mounted in the same time, per profile. |
26 const size_t kMaxFileSystems = 16; | 26 const size_t kMaxFileSystems = 16; |
27 | 27 |
28 // Constructs a safe mount point path for the provided file system represented | 28 // Constructs a safe mount point path for the provided file system represented |
29 // by |file_system_handle|. The handle is a numeric part of the file system id. | 29 // by |file_system_handle|. The handle is a numeric part of the file system id. |
30 base::FilePath GetMountPointPath(Profile* profile, | 30 base::FilePath GetMountPointPath(Profile* profile, |
31 std::string extension_id, | 31 std::string extension_id, |
32 int file_system_handle) { | 32 int file_system_id) { |
33 chromeos::User* const user = | 33 chromeos::User* const user = |
34 chromeos::UserManager::IsInitialized() | 34 chromeos::UserManager::IsInitialized() |
35 ? chromeos::UserManager::Get()->GetUserByProfile( | 35 ? chromeos::UserManager::Get()->GetUserByProfile( |
36 profile->GetOriginalProfile()) | 36 profile->GetOriginalProfile()) |
37 : NULL; | 37 : NULL; |
38 const std::string user_suffix = user ? "-" + user->username_hash() : ""; | 38 const std::string user_suffix = user ? "-" + user->username_hash() : ""; |
39 return base::FilePath(kProvidedMountPointRoot).AppendASCII( | 39 return base::FilePath(kProvidedMountPointRoot).AppendASCII( |
40 extension_id + "-" + base::IntToString(file_system_handle) + user_suffix); | 40 extension_id + "-" + base::IntToString(file_system_id) + user_suffix); |
41 } | 41 } |
42 | 42 |
43 } // namespace | 43 } // namespace |
44 | 44 |
45 Service::Service(Profile* profile) : profile_(profile), next_handle_(1) {} | 45 Service::Service(Profile* profile) : profile_(profile), next_id_(1) {} |
46 | 46 |
47 Service::~Service() {} | 47 Service::~Service() {} |
48 | 48 |
49 // static | 49 // static |
50 Service* Service::Get(content::BrowserContext* context) { | 50 Service* Service::Get(content::BrowserContext* context) { |
51 return ServiceFactory::Get(context); | 51 return ServiceFactory::Get(context); |
52 } | 52 } |
53 | 53 |
54 void Service::AddObserver(Observer* observer) { | 54 void Service::AddObserver(Observer* observer) { |
55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
56 DCHECK(observer); | 56 DCHECK(observer); |
57 observers_.AddObserver(observer); | 57 observers_.AddObserver(observer); |
58 } | 58 } |
59 | 59 |
60 void Service::RemoveObserver(Observer* observer) { | 60 void Service::RemoveObserver(Observer* observer) { |
61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
62 DCHECK(observer); | 62 DCHECK(observer); |
63 observers_.RemoveObserver(observer); | 63 observers_.RemoveObserver(observer); |
64 } | 64 } |
65 | 65 |
66 std::string Service::RegisterFileSystem(const std::string& extension_id, | 66 int Service::RegisterFileSystem(const std::string& extension_id, |
67 const std::string& file_system_name) { | 67 const std::string& file_system_name) { |
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
69 | 69 |
70 // Restrict number of file systems to prevent system abusing. | 70 // Restrict number of file systems to prevent system abusing. |
71 if (file_systems_.size() + 1 > kMaxFileSystems) | 71 if (file_systems_.size() + 1 > kMaxFileSystems) |
72 return ""; | 72 return 0; |
73 | 73 |
74 // The file system handle is unique per service, so per profile. | 74 // The file system id is unique per service, so per profile. |
75 int file_system_handle = next_handle_; | 75 int file_system_id = next_id_; |
76 | 76 |
77 fileapi::ExternalMountPoints* const mount_points = | 77 fileapi::ExternalMountPoints* const mount_points = |
78 fileapi::ExternalMountPoints::GetSystemInstance(); | 78 fileapi::ExternalMountPoints::GetSystemInstance(); |
79 DCHECK(mount_points); | 79 DCHECK(mount_points); |
80 | 80 |
81 // The mount point path and name are unique per system, since they are system | 81 // The mount point path and name are unique per system, since they are system |
82 // wide. This is necessary for copying between profiles. | 82 // wide. This is necessary for copying between profiles. |
83 const base::FilePath& mount_point_path = | 83 const base::FilePath& mount_point_path = |
84 GetMountPointPath(profile_, extension_id, file_system_handle); | 84 GetMountPointPath(profile_, extension_id, file_system_id); |
85 const std::string mount_point_name = | 85 const std::string mount_point_name = |
86 mount_point_path.BaseName().AsUTF8Unsafe(); | 86 mount_point_path.BaseName().AsUTF8Unsafe(); |
87 | 87 |
88 if (!mount_points->RegisterFileSystem(mount_point_name, | 88 if (!mount_points->RegisterFileSystem(mount_point_name, |
89 fileapi::kFileSystemTypeProvided, | 89 fileapi::kFileSystemTypeProvided, |
90 fileapi::FileSystemMountOption(), | 90 fileapi::FileSystemMountOption(), |
91 mount_point_path)) { | 91 mount_point_path)) { |
92 return ""; | 92 return 0; |
93 } | 93 } |
94 | 94 |
95 // Store the file system descriptor. Use the mount point name as the file | 95 // Store the file system descriptor. Use the mount point name as the file |
96 // system provider file system id. | 96 // system provider file system id. |
97 // Examples: | 97 // Examples: |
98 // file_system_handle = 41 | 98 // file_system_id = 41 |
99 // mount_point_name = file_system_id = b33f1337-41-5aa5 | 99 // mount_point_name = file_system_id = b33f1337-41-5aa5 |
100 // mount_point_path = /provided/b33f1337-41-5aa5 | 100 // mount_point_path = /provided/b33f1337-41-5aa5 |
101 const std::string file_system_id = mount_point_name; | |
102 ProvidedFileSystem file_system( | 101 ProvidedFileSystem file_system( |
103 extension_id, file_system_id, file_system_name, mount_point_path); | 102 extension_id, file_system_id, file_system_name, mount_point_path); |
104 file_systems_[file_system_id] = file_system; | 103 file_systems_[file_system_id] = file_system; |
105 | 104 |
106 FOR_EACH_OBSERVER( | 105 FOR_EACH_OBSERVER( |
107 Observer, observers_, OnProvidedFileSystemRegistered(file_system)); | 106 Observer, observers_, OnProvidedFileSystemRegistered(file_system)); |
108 | 107 |
109 next_handle_++; | 108 next_id_++; |
110 return file_system_id; | 109 return file_system_id; |
111 } | 110 } |
112 | 111 |
113 bool Service::UnregisterFileSystem(const std::string& extension_id, | 112 bool Service::UnregisterFileSystem(const std::string& extension_id, |
114 const std::string& file_system_id) { | 113 int file_system_id) { |
115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 114 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
116 | 115 |
117 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | 116 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); |
118 if (file_system_it == file_systems_.end() || | 117 if (file_system_it == file_systems_.end() || |
119 file_system_it->second.extension_id() != extension_id) { | 118 file_system_it->second.extension_id() != extension_id) { |
120 return false; | 119 return false; |
121 } | 120 } |
122 | 121 |
123 fileapi::ExternalMountPoints* const mount_points = | 122 fileapi::ExternalMountPoints* const mount_points = |
124 fileapi::ExternalMountPoints::GetSystemInstance(); | 123 fileapi::ExternalMountPoints::GetSystemInstance(); |
125 DCHECK(mount_points); | 124 DCHECK(mount_points); |
126 | 125 |
127 if (!mount_points->RevokeFileSystem(file_system_it->second.file_system_id())) | 126 const std::string mount_point_name = |
| 127 file_system_it->second.mount_path().BaseName().value(); |
| 128 if (!mount_points->RevokeFileSystem(mount_point_name)) |
128 return false; | 129 return false; |
129 | 130 |
130 FOR_EACH_OBSERVER(Observer, | 131 FOR_EACH_OBSERVER(Observer, |
131 observers_, | 132 observers_, |
132 OnProvidedFileSystemUnregistered(file_system_it->second)); | 133 OnProvidedFileSystemUnregistered(file_system_it->second)); |
133 | 134 |
134 file_systems_.erase(file_system_it); | 135 file_systems_.erase(file_system_it); |
135 return true; | 136 return true; |
136 } | 137 } |
137 | 138 |
138 std::vector<ProvidedFileSystem> Service::GetRegisteredFileSystems() { | 139 std::vector<ProvidedFileSystem> Service::GetRegisteredFileSystems() { |
139 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 140 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
140 | 141 |
141 std::vector<ProvidedFileSystem> result; | 142 std::vector<ProvidedFileSystem> result; |
142 for (FileSystemMap::const_iterator it = file_systems_.begin(); | 143 for (FileSystemMap::const_iterator it = file_systems_.begin(); |
143 it != file_systems_.end(); | 144 it != file_systems_.end(); |
144 ++it) { | 145 ++it) { |
145 result.push_back(it->second); | 146 result.push_back(it->second); |
146 } | 147 } |
147 return result; | 148 return result; |
148 } | 149 } |
149 | 150 |
150 } // namespace file_system_provider | 151 } // namespace file_system_provider |
151 } // namespace chromeos | 152 } // namespace chromeos |
OLD | NEW |