OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "webkit/glue/plugins/pepper_file_ref.h" |
| 6 |
| 7 #include "base/string_util.h" |
| 8 #include "webkit/glue/plugins/pepper_plugin_instance.h" |
| 9 #include "webkit/glue/plugins/pepper_var.h" |
| 10 #include "webkit/glue/plugins/pepper_resource_tracker.h" |
| 11 |
| 12 namespace pepper { |
| 13 |
| 14 namespace { |
| 15 |
| 16 bool IsValidLocalPath(const std::string& path) { |
| 17 // The path must start with '/' |
| 18 if (path.empty() || path[0] != '/') |
| 19 return false; |
| 20 |
| 21 // The path must contain valid UTF-8 characters. |
| 22 if (!IsStringUTF8(path)) |
| 23 return false; |
| 24 |
| 25 return true; |
| 26 } |
| 27 |
| 28 void TrimTrailingSlash(std::string* path) { |
| 29 // If this path ends with a slash, then normalize it away unless path is the |
| 30 // root path. |
| 31 if (path->size() > 1 && path->at(path->size() - 1) == '/') |
| 32 path->erase(path->size() - 1, 1); |
| 33 } |
| 34 |
| 35 PP_Resource CreateFileRef(PP_Instance instance_id, |
| 36 PP_FileSystemType fs_type, |
| 37 const char* path) { |
| 38 PluginInstance* instance = PluginInstance::FromPPInstance(instance_id); |
| 39 if (!instance) |
| 40 return 0; |
| 41 |
| 42 std::string origin; // TODO(darin): Extract from PluginInstance. |
| 43 |
| 44 std::string validated_path(path); |
| 45 if (!IsValidLocalPath(validated_path)) |
| 46 return 0; |
| 47 TrimTrailingSlash(&validated_path); |
| 48 |
| 49 FileRef* file_ref = new FileRef(instance->module(), |
| 50 fs_type, |
| 51 validated_path, |
| 52 origin); |
| 53 file_ref->AddRef(); // AddRef for the caller. |
| 54 return file_ref->GetResource(); |
| 55 } |
| 56 |
| 57 PP_Resource CreatePersistentFileRef(PP_Instance instance_id, const char* path) { |
| 58 return CreateFileRef(instance_id, PP_FileSystemType_LocalPersistent, path); |
| 59 } |
| 60 |
| 61 PP_Resource CreateTemporaryFileRef(PP_Instance instance_id, const char* path) { |
| 62 return CreateFileRef(instance_id, PP_FileSystemType_LocalTemporary, path); |
| 63 } |
| 64 |
| 65 bool IsFileRef(PP_Resource resource) { |
| 66 return !!ResourceTracker::Get()->GetAsFileRef(resource).get(); |
| 67 } |
| 68 |
| 69 PP_FileSystemType GetFileSystemType(PP_Resource file_ref_id) { |
| 70 scoped_refptr<FileRef> file_ref( |
| 71 ResourceTracker::Get()->GetAsFileRef(file_ref_id)); |
| 72 if (!file_ref.get()) |
| 73 return PP_FileSystemType_External; |
| 74 |
| 75 return file_ref->file_system_type(); |
| 76 } |
| 77 |
| 78 PP_Var GetName(PP_Resource file_ref_id) { |
| 79 scoped_refptr<FileRef> file_ref( |
| 80 ResourceTracker::Get()->GetAsFileRef(file_ref_id)); |
| 81 if (!file_ref.get()) |
| 82 return PP_MakeVoid(); |
| 83 |
| 84 return StringToPPVar(file_ref->GetName()); |
| 85 } |
| 86 |
| 87 PP_Var GetPath(PP_Resource file_ref_id) { |
| 88 scoped_refptr<FileRef> file_ref( |
| 89 ResourceTracker::Get()->GetAsFileRef(file_ref_id)); |
| 90 if (!file_ref.get()) |
| 91 return PP_MakeVoid(); |
| 92 |
| 93 if (file_ref->file_system_type() == PP_FileSystemType_External) |
| 94 return PP_MakeVoid(); |
| 95 |
| 96 return StringToPPVar(file_ref->path()); |
| 97 } |
| 98 |
| 99 PP_Resource GetParent(PP_Resource file_ref_id) { |
| 100 scoped_refptr<FileRef> file_ref( |
| 101 ResourceTracker::Get()->GetAsFileRef(file_ref_id)); |
| 102 if (!file_ref.get()) |
| 103 return 0; |
| 104 |
| 105 if (file_ref->file_system_type() == PP_FileSystemType_External) |
| 106 return 0; |
| 107 |
| 108 scoped_refptr<FileRef> parent_ref(file_ref->GetParent()); |
| 109 if (!parent_ref.get()) |
| 110 return 0; |
| 111 parent_ref->AddRef(); // AddRef for the caller. |
| 112 |
| 113 return parent_ref->GetResource(); |
| 114 } |
| 115 |
| 116 const PPB_FileRef ppb_fileref = { |
| 117 &CreatePersistentFileRef, |
| 118 &CreateTemporaryFileRef, |
| 119 &IsFileRef, |
| 120 &GetFileSystemType, |
| 121 &GetName, |
| 122 &GetPath, |
| 123 &GetParent |
| 124 }; |
| 125 |
| 126 } // namespace |
| 127 |
| 128 FileRef::FileRef(PluginModule* module, |
| 129 PP_FileSystemType file_system_type, |
| 130 const std::string& validated_path, |
| 131 const std::string& origin) |
| 132 : Resource(module), |
| 133 fs_type_(file_system_type), |
| 134 path_(validated_path), |
| 135 origin_(origin) { |
| 136 } |
| 137 |
| 138 FileRef::~FileRef() { |
| 139 } |
| 140 |
| 141 // static |
| 142 const PPB_FileRef* FileRef::GetInterface() { |
| 143 return &ppb_fileref; |
| 144 } |
| 145 |
| 146 std::string FileRef::GetName() const { |
| 147 if (path_.size() == 1 && path_[0] == '/') |
| 148 return path_; |
| 149 |
| 150 // There should always be a leading slash at least! |
| 151 size_t pos = path_.rfind('/'); |
| 152 DCHECK(pos != std::string::npos); |
| 153 |
| 154 return path_.substr(pos + 1); |
| 155 } |
| 156 |
| 157 scoped_refptr<FileRef> FileRef::GetParent() { |
| 158 if (path_.size() == 1 && path_[0] == '/') |
| 159 return this; |
| 160 |
| 161 // There should always be a leading slash at least! |
| 162 size_t pos = path_.rfind('/'); |
| 163 DCHECK(pos != std::string::npos); |
| 164 |
| 165 // If the path is "/foo", then we want to include the slash. |
| 166 if (pos == 0) |
| 167 pos++; |
| 168 std::string parent_path = path_.substr(0, pos); |
| 169 |
| 170 FileRef* parent_ref = new FileRef(module(), fs_type_, parent_path, origin_); |
| 171 parent_ref->AddRef(); // AddRef for the caller. |
| 172 return parent_ref; |
| 173 } |
| 174 |
| 175 } // namespace pepper |
OLD | NEW |