Commit e756eda1 authored by Bailey Berro's avatar Bailey Berro Committed by Commit Bot

Add SmbUrl::GetWindowsUNCString method

This change adds a GetWindowsUNCString method to the SmbUrl class so that
Windows style UNC can be displayed rather than the smb:// style URL.

Bug: chromium:889289
Change-Id: If7f9d3235a7cd38ac854d6bd186ea4af257de6be
Reviewed-on: https://chromium-review.googlesource.com/c/1256107Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Commit-Queue: Bailey Berro <baileyberro@chromium.org>
Cr-Commit-Position: refs/heads/master@{#596330}
parent 4411204b
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/chromeos/smb_client/smb_url.h" #include "chrome/browser/chromeos/smb_client/smb_url.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "chrome/browser/chromeos/smb_client/smb_constants.h" #include "chrome/browser/chromeos/smb_client/smb_constants.h"
#include "url/url_canon_stdstring.h" #include "url/url_canon_stdstring.h"
...@@ -13,6 +14,7 @@ namespace smb_client { ...@@ -13,6 +14,7 @@ namespace smb_client {
namespace { namespace {
const char kSingleBackslash[] = "\\";
const char kDoubleBackslash[] = "\\\\"; const char kDoubleBackslash[] = "\\\\";
// Returns true if |url| starts with "smb://" or "\\". // Returns true if |url| starts with "smb://" or "\\".
...@@ -59,6 +61,8 @@ SmbUrl::SmbUrl(const std::string& raw_url) { ...@@ -59,6 +61,8 @@ SmbUrl::SmbUrl(const std::string& raw_url) {
if (ShouldProcessUrl(raw_url)) { if (ShouldProcessUrl(raw_url)) {
// Add "smb://" if |url| starts with "\\" and canonicalize the URL. // Add "smb://" if |url| starts with "\\" and canonicalize the URL.
CanonicalizeSmbUrl(AddSmbSchemeIfMissing(raw_url)); CanonicalizeSmbUrl(AddSmbSchemeIfMissing(raw_url));
// Create the Windows UNC for the url.
CreateWindowsUnc(raw_url);
} }
} }
...@@ -90,6 +94,12 @@ bool SmbUrl::IsValid() const { ...@@ -90,6 +94,12 @@ bool SmbUrl::IsValid() const {
return !url_.empty() && host_.is_valid(); return !url_.empty() && host_.is_valid();
} }
std::string SmbUrl::GetWindowsUNCString() const {
DCHECK(IsValid());
return windows_unc_;
}
void SmbUrl::CanonicalizeSmbUrl(const std::string& url) { void SmbUrl::CanonicalizeSmbUrl(const std::string& url) {
DCHECK(!IsValid()); DCHECK(!IsValid());
DCHECK(ShouldProcessUrl(url)); DCHECK(ShouldProcessUrl(url));
...@@ -135,6 +145,21 @@ void SmbUrl::CanonicalizeSmbUrl(const std::string& url) { ...@@ -135,6 +145,21 @@ void SmbUrl::CanonicalizeSmbUrl(const std::string& url) {
DCHECK_EQ(url_.substr(scheme.begin, scheme.len), kSmbScheme); DCHECK_EQ(url_.substr(scheme.begin, scheme.len), kSmbScheme);
} }
void SmbUrl::CreateWindowsUnc(const std::string& url) {
url::Parsed parsed;
if (!ParseAndValidateUrl(url, &parsed)) {
return;
}
const std::string host = url.substr(parsed.host.begin, parsed.host.len);
std::string path = url.substr(parsed.path.begin, parsed.path.len);
// Turn any forward slashes into escaped backslashes.
base::ReplaceChars(path, "/", kSingleBackslash, &path);
windows_unc_ = base::StrCat({kDoubleBackslash, host, path});
}
void SmbUrl::Reset() { void SmbUrl::Reset() {
host_.reset(); host_.reset();
url_.clear(); url_.clear();
......
...@@ -38,16 +38,26 @@ class SmbUrl { ...@@ -38,16 +38,26 @@ class SmbUrl {
// should be called after the constructor. // should be called after the constructor.
bool IsValid() const; bool IsValid() const;
// Returns |url_| in the format \\server\share.
std::string GetWindowsUNCString() const;
private: private:
// Canonicalize |url| and saves the output as url_ and host_ if successful. // Canonicalize |url| and saves the output as url_ and host_ if successful.
void CanonicalizeSmbUrl(const std::string& url); void CanonicalizeSmbUrl(const std::string& url);
// Parse |url| into a Windows UNC |windows_unc_|.
void CreateWindowsUnc(const std::string& url);
// Resets url_ and parsed_. // Resets url_ and parsed_.
void Reset(); void Reset();
// String form of the canonical url. // String form of the canonical url.
std::string url_; std::string url_;
// String form of the Windows Universal Naming Convention of the url.
// MS-DTYP section 2.2.57
std::string windows_unc_;
// Holds the identified host of the URL. This does not store the host itself. // Holds the identified host of the URL. This does not store the host itself.
url::Component host_; url::Component host_;
......
...@@ -31,6 +31,13 @@ class SmbUrlTest : public testing::Test { ...@@ -31,6 +31,13 @@ class SmbUrlTest : public testing::Test {
EXPECT_EQ(expected_host, smb_url.GetHost()); EXPECT_EQ(expected_host, smb_url.GetHost());
} }
void ExpectValidWindowsUNC(const std::string& url,
const std::string& expected_unc) {
SmbUrl smb_url(url);
EXPECT_TRUE(smb_url.IsValid());
EXPECT_EQ(expected_unc, smb_url.GetWindowsUNCString());
}
private: private:
DISALLOW_COPY_AND_ASSIGN(SmbUrlTest); DISALLOW_COPY_AND_ASSIGN(SmbUrlTest);
}; };
...@@ -103,5 +110,16 @@ TEST_F(SmbUrlTest, ReplacesHost) { ...@@ -103,5 +110,16 @@ TEST_F(SmbUrlTest, ReplacesHost) {
EXPECT_EQ(expected_host, smb_url.GetHost()); EXPECT_EQ(expected_host, smb_url.GetHost());
} }
TEST_F(SmbUrlTest, GetWindowsURL) {
ExpectValidWindowsUNC("smb://server/share", "\\\\server\\share");
ExpectValidWindowsUNC("smb://server/share/long/folder",
"\\\\server\\share\\long\\folder");
ExpectValidWindowsUNC("smb://server/share/folder.with.dots",
"\\\\server\\share\\folder.with.dots");
ExpectValidWindowsUNC("smb://server\\share/mixed\\slashes",
"\\\\server\\share\\mixed\\slashes");
ExpectValidWindowsUNC("\\\\server/share", "\\\\server\\share");
}
} // namespace smb_client } // namespace smb_client
} // namespace chromeos } // namespace chromeos
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment