Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Unified Diff: ios/chrome/common/x_callback_url.cc

Issue 2485573003: Functions to create and manipulate x-callback-url URLs. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/chrome/common/x_callback_url.cc
diff --git a/ios/chrome/common/x_callback_url.cc b/ios/chrome/common/x_callback_url.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c0762ca1d63fe1f00da7b2771f5d1ccef390cc20
--- /dev/null
+++ b/ios/chrome/common/x_callback_url.cc
@@ -0,0 +1,80 @@
+// Copyright 2016 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 "ios/chrome/common/x_callback_url.h"
+
+#include "base/logging.h"
+#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
+#include "net/base/escape.h"
+#include "net/base/url_util.h"
+
+namespace {
+
+const char kXCallbackURLHost[] = "x-callback-url";
+const char kSuccessURLParameterName[] = "x-success";
+const char kErrorURLParameterName[] = "x-error";
+const char kCancelURLParameterName[] = "x-cancel";
+
+} // namespace
+
+bool IsXCallbackURL(const GURL& url) {
+ return url.is_valid() && url.host_piece() == kXCallbackURLHost;
+}
+
+GURL CreateXCallbackURL(base::StringPiece scheme, base::StringPiece action) {
+ return CreateXCallbackURLWithParameters(scheme, action, GURL(), GURL(),
+ GURL(),
+ std::map<std::string, std::string>());
+}
+
+GURL CreateXCallbackURLWithParameters(
+ base::StringPiece scheme,
+ base::StringPiece action,
+ const GURL& success_url,
+ const GURL& error_url,
+ const GURL& cancel_url,
+ const std::map<std::string, std::string>& parameters) {
+ DCHECK(!scheme.empty());
+ GURL url(base::StringPrintf("%s://%s/%s", scheme.data(), kXCallbackURLHost,
rohitrao (ping after 24h) 2016/11/07 17:58:17 Is there any way to do this using GURL primitives,
sdefresne 2016/11/08 07:15:28 I've tried the following: GURL url; GURL::Replace
+ !action.empty() ? action.data() : ""));
+
+ if (success_url.is_valid()) {
+ url = net::AppendQueryParameter(url, kSuccessURLParameterName,
rohitrao (ping after 24h) 2016/11/07 17:58:17 Does AppendQueryParameter handle escaping?
mef 2016/11/07 18:42:10 Yes, Unsafe characters in the name and the value
+ success_url.spec());
+ }
+
+ if (error_url.is_valid()) {
+ url = net::AppendQueryParameter(url, kErrorURLParameterName,
+ error_url.spec());
+ }
+
+ if (cancel_url.is_valid()) {
+ url = net::AppendQueryParameter(url, kCancelURLParameterName,
+ cancel_url.spec());
+ }
+
+ if (!parameters.empty()) {
+ for (const auto& pair : parameters) {
+ url = net::AppendQueryParameter(url, pair.first, pair.second);
+ }
+ }
+
+ return url;
+}
+
+std::map<std::string, std::string> ExtractQueryParametersFromXCallbackURL(
+ const GURL& x_callback_url) {
+ DCHECK(IsXCallbackURL(x_callback_url));
+ std::map<std::string, std::string> parameters;
+
+ net::QueryIterator query_iterator(x_callback_url);
+ while (!query_iterator.IsAtEnd()) {
+ parameters.insert(std::make_pair(query_iterator.GetKey(),
+ query_iterator.GetUnescapedValue()));
+ query_iterator.Advance();
+ }
+
+ return parameters;
+}

Powered by Google App Engine
This is Rietveld 408576698