From 7824cf82df8244b5f66a56e663ce6e121c25de88 Mon Sep 17 00:00:00 2001 From: "yhirano@chromium.org" <yhirano@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> Date: Thu, 13 Mar 2014 10:22:57 +0000 Subject: Introduce url::Origin to represent Web Origin. Introduce url::Origin to represent a serialized Web Origin defined in RFC6455. This class wraps a string representation of blink-side SecurityOrigin object. BUG=339373 R=tyoshino@chromium.org, ricea@chromium.org Review URL: https://codereview.chromium.org/170843007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@256789 0039d316-1c4b-4281-b951-d872f2087c98 --- url/origin.cc | 19 +++++++++++++++++++ url/origin.h | 33 +++++++++++++++++++++++++++++++++ url/origin_unittest.cc | 36 ++++++++++++++++++++++++++++++++++++ url/url.gyp | 1 + url/url_srcs.gypi | 2 ++ 5 files changed, 91 insertions(+) create mode 100644 url/origin.cc create mode 100644 url/origin.h create mode 100644 url/origin_unittest.cc (limited to 'url') diff --git a/url/origin.cc b/url/origin.cc new file mode 100644 index 0000000..eb2cf14 --- /dev/null +++ b/url/origin.cc @@ -0,0 +1,19 @@ +// Copyright 2014 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 "url/origin.h" + +#include "base/strings/string_util.h" + +namespace url { + +Origin::Origin() : string_("null") {} + +Origin::Origin(const std::string& origin) : string_(origin) { + DCHECK(origin == "null" || MatchPattern(origin, "?*://?*")); + DCHECK_GT(origin.size(), 0u); + DCHECK_NE(origin[origin.size() - 1], '/'); +} + +} // namespace url diff --git a/url/origin.h b/url/origin.h new file mode 100644 index 0000000..777e4e1e --- /dev/null +++ b/url/origin.h @@ -0,0 +1,33 @@ +// Copyright 2014 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 URL_ORIGIN_H_ +#define URL_ORIGIN_H_ + +#include <string> + +#include "url/url_export.h" + +namespace url { + +// Origin represents a Web Origin serialized to a string. +// See RFC6454 for details. +class URL_EXPORT Origin { + public: + Origin(); + explicit Origin(const std::string& origin); + + const std::string& string() const { return string_; } + + bool IsSameAs(const Origin& that) const { + return string_ == that.string_; + } + + private: + std::string string_; +}; + +} // namespace url + +#endif // URL_ORIGIN_H_ diff --git a/url/origin_unittest.cc b/url/origin_unittest.cc new file mode 100644 index 0000000..d08342e --- /dev/null +++ b/url/origin_unittest.cc @@ -0,0 +1,36 @@ +// Copyright 2014 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 "testing/gtest/include/gtest/gtest.h" +#include "url/origin.h" + +namespace url { + +namespace { + +// Each test examines the Origin is constructed correctly without +// violating DCHECKs. +TEST(OriginTest, constructEmpty) { + Origin origin; + EXPECT_EQ("null", origin.string()); +} + +TEST(OriginTest, constructNull) { + Origin origin("null"); + EXPECT_EQ("null", origin.string()); +} + +TEST(OriginTest, constructValidOrigin) { + Origin origin("http://example.com:8080"); + EXPECT_EQ("http://example.com:8080", origin.string()); +} + +TEST(OriginTest, constructValidOriginWithoutPort) { + Origin origin("wss://example2.com"); + EXPECT_EQ("wss://example2.com", origin.string()); +} + +} // namespace + +} // namespace url diff --git a/url/url.gyp b/url/url.gyp index bb617f1..93bbee2 100644 --- a/url/url.gyp +++ b/url/url.gyp @@ -48,6 +48,7 @@ ], 'sources': [ 'gurl_unittest.cc', + 'origin_unittest.cc', 'url_canon_unittest.cc', 'url_parse_unittest.cc', 'url_test_utils.h', diff --git a/url/url_srcs.gypi b/url/url_srcs.gypi index e26bd75..3114620 100644 --- a/url/url_srcs.gypi +++ b/url/url_srcs.gypi @@ -7,6 +7,8 @@ 'gurl_sources': [ 'gurl.cc', 'gurl.h', + 'origin.cc', + 'origin.h', 'third_party/mozilla/url_parse.cc', 'third_party/mozilla/url_parse.h', 'url_canon.h', -- cgit v1.1