Skip to content

Commit

Permalink
Pepper: Create FileRef utility routines.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ppapi/ppapi_shared.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
'shared_impl/file_io_state_manager.h',
'shared_impl/file_path.cc',
'shared_impl/file_path.h',
'shared_impl/file_ref_util.cc',
'shared_impl/file_ref_util.h',
'shared_impl/file_type_conversion.cc',
'shared_impl/file_type_conversion.h',
'shared_impl/flash_clipboard_format_registry.cc',
Expand Down
58 changes: 58 additions & 0 deletions ppapi/shared_impl/file_ref_util.cc
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
26 changes: 26 additions & 0 deletions ppapi/shared_impl/file_ref_util.h
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_

0 comments on commit bf8961a

Please sign in to comment.