Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "webkit/chromeos/fileapi/cros_mount_point_provider.h" | 5 #include "webkit/chromeos/fileapi/cros_mount_point_provider.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_callback_factory.h" | 8 #include "base/memory/scoped_callback_factory.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCString.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCString.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystem.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystem.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
| 17 #include "webkit/chromeos/fileapi/file_access_permissions.h" | |
| 17 #include "webkit/fileapi/file_system_path_manager.h" | 18 #include "webkit/fileapi/file_system_path_manager.h" |
| 19 #include "webkit/fileapi/file_system_util.h" | |
| 18 #include "webkit/glue/webkit_glue.h" | 20 #include "webkit/glue/webkit_glue.h" |
| 19 | 21 |
| 20 namespace chromeos { | 22 namespace chromeos { |
| 21 | 23 |
| 22 const char CrosMountPointProvider::kLocalName[] = "Local"; | |
| 23 const char CrosMountPointProvider::kLocalDirName[] = "/local/"; | |
| 24 | |
| 25 typedef struct { | 24 typedef struct { |
| 26 const char* local_root_path; | 25 const char* local_root_path; |
| 27 const char* web_root_path; | 26 const char* web_root_path; |
| 28 } FixedExposedPaths; | 27 } FixedExposedPaths; |
| 29 | 28 |
| 30 // Top level file system elements exposed in FileAPI in ChromeOS: | 29 // Top level file system elements exposed in FileAPI in ChromeOS: |
| 31 FixedExposedPaths fixed_exposed_paths[] = { | 30 FixedExposedPaths fixed_exposed_paths[] = { |
| 32 {"/home/chronos/user/", "Downloads"}, | 31 {"/home/chronos/user/", "Downloads"}, |
| 33 {"/", "media"}, | 32 {"/", "media"}, |
| 34 {"/", "tmp"}, | 33 {"/", "tmp"}, |
| 35 }; | 34 }; |
| 36 | 35 |
| 37 CrosMountPointProvider::CrosMountPointProvider( | 36 CrosMountPointProvider::CrosMountPointProvider( |
| 38 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy) | 37 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy) |
| 39 : local_dir_name_(kLocalName), | 38 : special_storage_policy_(special_storage_policy), |
| 40 special_storage_policy_(special_storage_policy) { | 39 file_access_permissions_(new FileAccessPermissions()) { |
| 41 | |
| 42 // TODO(zelidrag): There's got to be a better way to generate UUID. | |
| 43 srand(time(NULL)); | |
| 44 std::string virtual_root; | |
| 45 virtual_root.append(base::StringPrintf("%hx%hx-%hx-%hx-%hx-%hx%hx%hx", | |
| 46 static_cast<unsigned short>(rand()), static_cast<unsigned short>(rand()), | |
| 47 static_cast<unsigned short>(rand()), | |
| 48 static_cast<unsigned short>(rand()), | |
| 49 static_cast<unsigned short>(rand()), | |
| 50 static_cast<unsigned short>(rand()), static_cast<unsigned short>(rand()), | |
| 51 static_cast<unsigned short>(rand()))); | |
| 52 | |
| 53 // Create virtual root node. | |
| 54 virtual_root_path_ = FilePath(virtual_root); | |
| 55 base_path_ = virtual_root_path_.Append(local_dir_name_); | |
| 56 | |
| 57 for (size_t i = 0; i < arraysize(fixed_exposed_paths); i++) { | 40 for (size_t i = 0; i < arraysize(fixed_exposed_paths); i++) { |
| 58 mount_point_map_.insert(std::pair<std::string, std::string>( | 41 mount_point_map_.insert(std::pair<std::string, FilePath>( |
| 59 std::string(fixed_exposed_paths[i].web_root_path), | 42 std::string(fixed_exposed_paths[i].web_root_path), |
| 60 std::string(fixed_exposed_paths[i].local_root_path))); | 43 FilePath(std::string(fixed_exposed_paths[i].local_root_path)))); |
| 61 } | 44 } |
| 62 } | 45 } |
| 63 | 46 |
| 64 CrosMountPointProvider::~CrosMountPointProvider() { | 47 CrosMountPointProvider::~CrosMountPointProvider() { |
| 65 } | 48 } |
| 66 | 49 |
| 67 // TODO(zelidrag) share with SandboxMountPointProvider impl. | 50 // TODO(zelidrag) share with SandboxMountPointProvider impl. |
| 68 std::string GetOriginIdentifierFromURL( | 51 std::string GetOriginIdentifierFromURL( |
| 69 const GURL& url) { | 52 const GURL& url) { |
| 70 WebKit::WebSecurityOrigin web_security_origin = | 53 WebKit::WebSecurityOrigin web_security_origin = |
| 71 WebKit::WebSecurityOrigin::createFromString(UTF8ToUTF16(url.spec())); | 54 WebKit::WebSecurityOrigin::createFromString(UTF8ToUTF16(url.spec())); |
| 72 return web_security_origin.databaseIdentifier().utf8(); | 55 return web_security_origin.databaseIdentifier().utf8(); |
| 73 } | 56 } |
| 74 | 57 |
| 75 void CrosMountPointProvider::GetFileSystemRootPath( | 58 void CrosMountPointProvider::GetFileSystemRootPath( |
| 76 const GURL& origin_url, | 59 const GURL& origin_url, |
| 77 fileapi::FileSystemType type, | 60 fileapi::FileSystemType type, |
| 78 bool create, | 61 bool create, |
| 79 fileapi::FileSystemPathManager::GetRootPathCallback* callback_ptr) { | 62 fileapi::FileSystemPathManager::GetRootPathCallback* callback_ptr) { |
| 80 DCHECK(type == fileapi::kFileSystemTypeLocal); | 63 DCHECK(type == fileapi::kFileSystemTypeExternal); |
| 81 | 64 |
| 82 std::string name(GetOriginIdentifierFromURL(origin_url)); | 65 std::string name(GetOriginIdentifierFromURL(origin_url)); |
| 83 name += ':'; | 66 name += ':'; |
| 84 name += CrosMountPointProvider::kLocalName; | 67 name += CrosMountPointProvider::kExternalName; |
| 85 | 68 |
| 86 FilePath root_path = FilePath(CrosMountPointProvider::kLocalDirName); | 69 FilePath root_path = FilePath(fileapi::kExternalDir); |
| 87 callback_ptr->Run(!root_path.empty(), root_path, name); | 70 callback_ptr->Run(!root_path.empty(), root_path, name); |
| 88 } | 71 } |
| 89 | 72 |
| 90 // Like GetFileSystemRootPath, but synchronous, and can be called only while | 73 // Like GetFileSystemRootPath, but synchronous, and can be called only while |
| 91 // running on the file thread. | 74 // running on the file thread. |
| 92 FilePath CrosMountPointProvider::GetFileSystemRootPathOnFileThread( | 75 FilePath CrosMountPointProvider::GetFileSystemRootPathOnFileThread( |
| 93 const GURL& origin_url, | 76 const GURL& origin_url, |
| 94 fileapi::FileSystemType type, | 77 fileapi::FileSystemType type, |
| 95 const FilePath& virtual_path, | 78 const FilePath& virtual_path, |
| 96 bool create) { | 79 bool create) { |
| 97 DCHECK(type == fileapi::kFileSystemTypeLocal); | 80 DCHECK(type == fileapi::kFileSystemTypeExternal); |
| 98 | 81 |
| 99 std::vector<FilePath::StringType> components; | 82 std::vector<FilePath::StringType> components; |
| 100 virtual_path.GetComponents(&components); | 83 virtual_path.GetComponents(&components); |
| 101 if (components.size() < 1) { | 84 if (components.size() < 1) { |
| 102 return FilePath(); | 85 return FilePath(); |
| 103 } | 86 } |
| 104 | 87 |
| 105 // Check if this root directory is exposed by this provider. | 88 // Check if this root directory is exposed by this provider. |
| 106 MountPointMap::iterator iter = mount_point_map_.find(components[0]); | 89 MountPointMap::iterator iter = mount_point_map_.find(components[0]); |
| 107 if (iter == mount_point_map_.end()) { | 90 if (iter == mount_point_map_.end()) { |
| 108 return FilePath(); | 91 return FilePath(); |
| 109 } | 92 } |
| 110 | 93 |
| 111 return FilePath(iter->second); | 94 return iter->second; |
| 112 } | 95 } |
| 113 | 96 |
| 114 // TODO(zelidrag): Share this code with SandboxMountPointProvider impl. | 97 // TODO(zelidrag): Share this code with SandboxMountPointProvider impl. |
| 115 bool CrosMountPointProvider::IsRestrictedFileName(const FilePath& path) const { | 98 bool CrosMountPointProvider::IsRestrictedFileName(const FilePath& path) const { |
| 116 return false; | 99 return false; |
| 117 } | 100 } |
| 118 | 101 |
| 119 bool CrosMountPointProvider::IsAccessAllowed(const GURL& origin_url) { | 102 bool CrosMountPointProvider::IsAccessAllowed(const GURL& origin_url, |
| 120 return special_storage_policy_->IsLocalFileSystemAccessAllowed(origin_url); | 103 const FilePath& virtual_path) { |
| 104 std::string extension_id = origin_url.host(); | |
| 105 // Check first to make sure this extension has fileBrowserHander permissions. | |
| 106 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
| 107 return false; | |
| 108 return file_access_permissions_->HasAccessPermission(extension_id, | |
| 109 virtual_path); | |
| 110 } | |
| 111 | |
| 112 void CrosMountPointProvider::GrantFullAccessToExtension( | |
| 113 const std::string& extension_id) { | |
| 114 DCHECK(special_storage_policy_->IsFileHandler(extension_id)); | |
| 115 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
| 116 return; | |
| 117 for (MountPointMap::const_iterator iter = mount_point_map_.begin(); | |
| 118 iter != mount_point_map_.end(); | |
| 119 ++iter) { | |
| 120 GrantFileAccessToExtension(extension_id, FilePath(iter->first)); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 void CrosMountPointProvider::GrantFileAccessToExtension( | |
| 125 const std::string& extension_id, const FilePath& virtual_path) { | |
| 126 // All we care about here is access from extensions access for now. | |
|
ericu
2011/04/15 01:46:42
extra "access"
zel
2011/04/15 02:18:55
Done.
| |
| 127 DCHECK(special_storage_policy_->IsFileHandler(extension_id)); | |
| 128 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
| 129 return; | |
| 130 file_access_permissions_->GrantAccessPermission(extension_id, virtual_path); | |
| 131 } | |
| 132 | |
| 133 void CrosMountPointProvider::RevokeAccessForExtension( | |
| 134 const std::string& extension_id) { | |
| 135 file_access_permissions_->RevokePermissions(extension_id); | |
| 136 } | |
| 137 | |
| 138 std::vector<FilePath> CrosMountPointProvider::GetRootDirectories() const { | |
| 139 std::vector<FilePath> root_dirs; | |
| 140 for (MountPointMap::const_iterator iter = mount_point_map_.begin(); | |
| 141 iter != mount_point_map_.end(); | |
| 142 ++iter) { | |
| 143 root_dirs.push_back(iter->second.Append(iter->first)); | |
| 144 } | |
| 145 return root_dirs; | |
| 121 } | 146 } |
| 122 | 147 |
| 123 } // namespace chromeos | 148 } // namespace chromeos |
| 124 | 149 |
| OLD | NEW |