17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file. 47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ppapi/shared_impl/file_ref_util.h" 67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/files/file_path.h" 87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string_util.h" 97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace ppapi { 127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)std::string GetNameForInternalFilePath(const std::string& path) { 147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (path == "/") 157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return path; 167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) size_t pos = path.rfind('/'); 177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CHECK(pos != std::string::npos); 187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return path.substr(pos + 1); 197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)std::string GetNameForExternalFilePath(const base::FilePath& path) { 227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const base::FilePath::StringType& file_path = path.value(); 237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) size_t pos = file_path.rfind(base::FilePath::kSeparators[0]); 247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CHECK(pos != base::FilePath::StringType::npos); 257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#if defined(OS_WIN) 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return base::WideToUTF8(file_path.substr(pos + 1)); 277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#elif defined(OS_POSIX) 287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return file_path.substr(pos + 1); 297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#else 307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#error "Unsupported platform." 317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#endif 327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool IsValidInternalPath(const std::string& path) { 357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // We check that: 367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // The path starts with '/' 377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // The path must contain valid UTF-8 characters. 387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // It must not FilePath::ReferencesParent(). 39010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) if (path.empty() || !base::IsStringUTF8(path) || path[0] != '/') 407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return false; 417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) base::FilePath file_path = base::FilePath::FromUTF8Unsafe(path); 427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (file_path.ReferencesParent()) 437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return false; 447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return true; 457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool IsValidExternalPath(const base::FilePath& path) { 48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return !path.empty() && !path.ReferencesParent(); 49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void NormalizeInternalPath(std::string* path) { 527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (path->size() > 1 && path->at(path->size() - 1) == '/') 537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) path->erase(path->size() - 1, 1); 547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} // namespace ppapi 57