Add version checking to webview library loads.
[chromium-blink-merge.git] / ppapi / shared_impl / file_ref_util.cc
bloba5523cbb944ad2e17417ffcfc19a05ae55e0221d
1 // Copyright 2013 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.
5 #include "ppapi/shared_impl/file_ref_util.h"
7 #include "base/files/file_path.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
11 namespace ppapi {
13 std::string GetNameForInternalFilePath(const std::string& path) {
14 if (path == "/")
15 return path;
16 size_t pos = path.rfind('/');
17 CHECK(pos != std::string::npos);
18 return path.substr(pos + 1);
21 std::string GetNameForExternalFilePath(const base::FilePath& path) {
22 const base::FilePath::StringType& file_path = path.value();
23 size_t pos = file_path.rfind(base::FilePath::kSeparators[0]);
24 CHECK(pos != base::FilePath::StringType::npos);
25 #if defined(OS_WIN)
26 return base::WideToUTF8(file_path.substr(pos + 1));
27 #elif defined(OS_POSIX)
28 return file_path.substr(pos + 1);
29 #else
30 #error "Unsupported platform."
31 #endif
34 bool IsValidInternalPath(const std::string& path) {
35 // We check that:
36 // The path starts with '/'
37 // The path must contain valid UTF-8 characters.
38 // It must not FilePath::ReferencesParent().
39 if (path.empty() || !base::IsStringUTF8(path) || path[0] != '/')
40 return false;
41 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(path);
42 if (file_path.ReferencesParent())
43 return false;
44 return true;
47 bool IsValidExternalPath(const base::FilePath& path) {
48 return !path.empty() && !path.ReferencesParent();
51 void NormalizeInternalPath(std::string* path) {
52 if (path->size() > 1 && path->at(path->size() - 1) == '/')
53 path->erase(path->size() - 1, 1);
56 } // namespace ppapi