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