Skip to content

Commit

Permalink
net: extract GetDirectoryListingXXX functions into directory_listing.*
Browse files Browse the repository at this point in the history
This patch moves these function from net_util into a new component
(directory_listing.*) as it seems a more suitable place than the generic
net_util.h header.

BUG=488531
[email protected]
[email protected],[email protected],[email protected]

Review URL: https://codereview.chromium.org/1548503002

Cr-Commit-Position: refs/heads/master@{#367926}
  • Loading branch information
tfarina authored and Commit bot committed Jan 6, 2016
1 parent 3a62956 commit 43a416b
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 92 deletions.
2 changes: 1 addition & 1 deletion content/child/ftp_directory_listing_response_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "content/child/weburlresponse_extradata_impl.h"
#include "net/base/directory_listing.h"
#include "net/base/escape.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/ftp/ftp_directory_listing_parser.h"
#include "third_party/WebKit/public/platform/WebURL.h"
#include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
Expand Down
1 change: 0 additions & 1 deletion net/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ component("net") {
sources += [
"base/filename_util_icu.cc",
"base/net_string_util_icu.cc",
"base/net_util_icu.cc",
]

# Brotli support.
Expand Down
11 changes: 5 additions & 6 deletions net/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ specific_include_rules = {
"+base/i18n",
],

# Functions largely not used by the rest of net.
"directory_listing\.cc": [
"+base/i18n",
],

# Within net, only used by file: requests.
"filename_util_icu\.cc": [
"+base/i18n/file_util_icu.h",
],

# Functions largely not used by the rest of net.
"net_util_icu\.cc": [
"+base/i18n",
"+third_party/icu",
],

# Consolidated string functions that depend on icu.
"net_string_util_icu\.cc": [
"+base/i18n/i18n_constants.h",
Expand Down
26 changes: 24 additions & 2 deletions net/base/net_util_icu.cc → net/base/directory_listing.cc
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Copyright 2015 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 "net/base/net_util.h"
#include "net/base/directory_listing.h"

#include "base/i18n/time_formatting.h"
#include "base/json/string_escape.h"
#include "base/logging.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "net/base/escape.h"
#include "net/base/net_module.h"
#include "net/grit/net_resources.h"

namespace net {

std::string GetDirectoryListingHeader(const base::string16& title) {
static const base::StringPiece header(
NetModule::GetResource(IDR_DIR_HEADER_HTML));
// This can be null in unit tests.
DLOG_IF(WARNING, header.empty())
<< "Missing resource: directory listing header";

std::string result;
if (!header.empty())
result.assign(header.data(), header.size());

result.append("<script>start(");
base::EscapeJSONString(title, true, &result);
result.append(");</script>\n");

return result;
}

std::string GetDirectoryListingEntry(const base::string16& name,
const std::string& raw_bytes,
bool is_dir,
Expand Down
44 changes: 44 additions & 0 deletions net/base/directory_listing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2015 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 NET_BASE_DIRECTORY_LISTING_H_
#define NET_BASE_DIRECTORY_LISTING_H_

#include <stdint.h>
#include <string>

#include "base/strings/string16.h"
#include "net/base/net_export.h"

namespace base {
class Time;
}

namespace net {

// Call these functions to get the html snippet for a directory listing.
// The return values of both functions are in UTF-8.
NET_EXPORT std::string GetDirectoryListingHeader(const base::string16& title);

// Given the name of a file in a directory (ftp or local) and
// other information (is_dir, size, modification time), it returns
// the html snippet to add the entry for the file to the directory listing.
// Currently, it's a script tag containing a call to a Javascript function
// |addRow|.
//
// |name| is the file name to be displayed. |raw_bytes| will be used
// as the actual target of the link (so for example, ftp links should use
// server's encoding). If |raw_bytes| is an empty string, UTF-8 encoded |name|
// will be used.
//
// Both |name| and |raw_bytes| are escaped internally.
NET_EXPORT std::string GetDirectoryListingEntry(const base::string16& name,
const std::string& raw_bytes,
bool is_dir,
int64_t size,
base::Time modified);

} // namespace net

#endif // NET_BASE_DIRECTORY_LISTING_H_
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/net_util.h"

#include <stdint.h>

#include <string>
#include "net/base/directory_listing.h"

#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace net {

Expand All @@ -26,7 +21,7 @@ struct GetDirectoryListingEntryCase {
const char* const expected;
};

TEST(NetUtilTest, GetDirectoryListingEntry) {
TEST(DirectoryListingTest, GetDirectoryListingEntry) {
const GetDirectoryListingEntryCase test_cases[] = {
{L"Foo",
"",
Expand Down
21 changes: 0 additions & 21 deletions net/base/net_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <unistd.h>
#endif // defined(OS_POSIX)

#include "base/json/string_escape.h"
#include "base/logging.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
Expand All @@ -34,10 +33,8 @@
#include "net/base/address_list.h"
#include "net/base/escape.h"
#include "net/base/ip_address_number.h"
#include "net/base/net_module.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/base/url_util.h"
#include "net/grit/net_resources.h"
#include "url/gurl.h"
#include "url/third_party/mozilla/url_parse.h"
#include "url/url_canon.h"
Expand Down Expand Up @@ -97,24 +94,6 @@ std::string CanonicalizeHost(const std::string& host,
return canon_host;
}

std::string GetDirectoryListingHeader(const base::string16& title) {
static const base::StringPiece header(
NetModule::GetResource(IDR_DIR_HEADER_HTML));
// This can be null in unit tests.
DLOG_IF(WARNING, header.empty()) <<
"Missing resource: directory listing header";

std::string result;
if (!header.empty())
result.assign(header.data(), header.size());

result.append("<script>start(");
base::EscapeJSONString(title, true, &result);
result.append(");</script>\n");

return result;
}

inline bool IsHostCharAlphanumeric(char c) {
// We can just check lowercase because uppercase characters have already been
// normalized.
Expand Down
22 changes: 0 additions & 22 deletions net/base/net_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,6 @@ NET_EXPORT std::string CanonicalizeHost(const std::string& host,
// CanonicalizeHost(), or you may not get accurate results.
NET_EXPORT bool IsCanonicalizedHostCompliant(const std::string& host);

// Call these functions to get the html snippet for a directory listing.
// The return values of both functions are in UTF-8.
NET_EXPORT std::string GetDirectoryListingHeader(const base::string16& title);

// Given the name of a file in a directory (ftp or local) and
// other information (is_dir, size, modification time), it returns
// the html snippet to add the entry for the file to the directory listing.
// Currently, it's a script tag containing a call to a Javascript function
// |addRow|.
//
// |name| is the file name to be displayed. |raw_bytes| will be used
// as the actual target of the link (so for example, ftp links should use
// server's encoding). If |raw_bytes| is an empty string, UTF-8 encoded |name|
// will be used.
//
// Both |name| and |raw_bytes| are escaped internally.
NET_EXPORT std::string GetDirectoryListingEntry(const base::string16& name,
const std::string& raw_bytes,
bool is_dir,
int64_t size,
base::Time modified);

// Strip the portions of |url| that aren't core to the network request.
// - user name / password
// - reference section
Expand Down
1 change: 0 additions & 1 deletion net/net.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
'sources': [
'base/filename_util_icu.cc',
'base/net_string_util_icu.cc',
'base/net_util_icu.cc',
'filter/brotli_filter.cc',
],
'includes': [ 'net_common.gypi' ],
Expand Down
Loading

0 comments on commit 43a416b

Please sign in to comment.