forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pepper: Create FileRef utility routines.
The host and plugin implementations will need to share some common code for creating display names from internal and external paths and for validating internal paths. BUG=225441 Review URL: https://chromiumcodereview.appspot.com/16830008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@206438 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
teravest@chromium.org
committed
Jun 14, 2013
1 parent
db6c4ea
commit bf8961a
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2013 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "ppapi/shared_impl/file_ref_util.h" | ||
|
||
#include "base/strings/string_util.h" | ||
#include "base/strings/utf_string_conversions.h" | ||
|
||
namespace ppapi { | ||
|
||
std::string GetNameForInternalFilePath(const std::string& path) { | ||
if (path == "/") | ||
return path; | ||
size_t pos = path.rfind('/'); | ||
CHECK(pos != std::string::npos); | ||
return path.substr(pos + 1); | ||
} | ||
|
||
std::string GetNameForExternalFilePath(const base::FilePath& path) { | ||
const base::FilePath::StringType& file_path = path.value(); | ||
size_t pos = file_path.rfind(base::FilePath::kSeparators[0]); | ||
CHECK(pos != base::FilePath::StringType::npos); | ||
#if defined(OS_WIN) | ||
return WideToUTF8(file_path.substr(pos + 1)); | ||
#elif defined(OS_POSIX) | ||
return file_path.substr(pos + 1); | ||
#else | ||
#error "Unsupported platform." | ||
#endif | ||
} | ||
|
||
bool IsValidInternalPath(const std::string& path) { | ||
// We check that: | ||
// The path starts with '/' | ||
// The path must contain valid UTF-8 characters. | ||
// It must not FilePath::ReferencesParent(). | ||
if (path.empty() || path[0] != '/') | ||
return false; | ||
if (!IsStringUTF8(path)) | ||
return false; | ||
#if defined(OS_WIN) | ||
base::FilePath::StringType path_win(path.begin(), path.end()); | ||
base::FilePath file_path(path_win); | ||
#else | ||
base::FilePath file_path(path); | ||
#endif | ||
if (file_path.ReferencesParent()) | ||
return false; | ||
return true; | ||
} | ||
|
||
void NormalizeInternalPath(std::string* path) { | ||
if (path->size() > 1 && path->at(path->size() - 1) == '/') | ||
path->erase(path->size() - 1, 1); | ||
} | ||
|
||
} // namespace ppapi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2013 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef PPAPI_SHARED_IMPL_FILE_REF_UTIL_H_ | ||
#define PPAPI_SHARED_IMPL_FILE_REF_UTIL_H_ | ||
|
||
#include <string> | ||
|
||
#include "base/files/file_path.h" | ||
|
||
namespace ppapi { | ||
|
||
// Routines to generate display names for internal and external file paths. | ||
std::string GetNameForInternalFilePath(const std::string& path); | ||
std::string GetNameForExternalFilePath(const base::FilePath& path); | ||
|
||
// Determines whether an internal file path is valid. | ||
bool IsValidInternalPath(const std::string& path); | ||
|
||
// If path ends with a slash, normalize it away unless it's the root path. | ||
void NormalizeInternalPath(std::string* path); | ||
|
||
} // namespace ppapi | ||
|
||
#endif // PPAPI_SHARED_IMPL_FILE_REF_UTIL_H_ |