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

Side by Side Diff: ios/chrome/common/x_callback_url.cc

Issue 2485573003: Functions to create and manipulate x-callback-url URLs. (Closed)
Patch Set: Address comments 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 unified diff | Download patch
« no previous file with comments | « ios/chrome/common/x_callback_url.h ('k') | ios/chrome/common/x_callback_url_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ios/chrome/common/x_callback_url.h"
6
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "net/base/escape.h"
11 #include "net/base/url_util.h"
12
13 namespace {
14
15 const char kXCallbackURLHost[] = "x-callback-url";
16 const char kSuccessURLParameterName[] = "x-success";
17 const char kErrorURLParameterName[] = "x-error";
18 const char kCancelURLParameterName[] = "x-cancel";
19
20 } // namespace
21
22 bool IsXCallbackURL(const GURL& url) {
23 if (!url.is_valid())
24 return false;
25
26 if (url.IsStandard())
27 return url.host_piece() == kXCallbackURLHost;
28
29 base::StringPiece path_piece = url.path_piece();
30 if (path_piece.starts_with("//"))
31 path_piece = path_piece.substr(2, base::StringPiece::npos);
32
33 size_t pos = path_piece.find('/', 0);
34 if (pos != base::StringPiece::npos)
35 path_piece = path_piece.substr(0, pos);
36
37 return path_piece == kXCallbackURLHost;
38 }
39
40 GURL CreateXCallbackURL(base::StringPiece scheme, base::StringPiece action) {
41 return CreateXCallbackURLWithParameters(scheme, action, GURL(), GURL(),
42 GURL(),
43 std::map<std::string, std::string>());
44 }
45
46 GURL CreateXCallbackURLWithParameters(
47 base::StringPiece scheme,
48 base::StringPiece action,
49 const GURL& success_url,
50 const GURL& error_url,
51 const GURL& cancel_url,
52 const std::map<std::string, std::string>& parameters) {
53 DCHECK(!scheme.empty());
54 GURL url(base::StringPrintf("%s://%s/%s", scheme.data(), kXCallbackURLHost,
55 !action.empty() ? action.data() : ""));
56
57 if (success_url.is_valid()) {
58 url = net::AppendQueryParameter(url, kSuccessURLParameterName,
59 success_url.spec());
60 }
61
62 if (error_url.is_valid()) {
63 url = net::AppendQueryParameter(url, kErrorURLParameterName,
64 error_url.spec());
65 }
66
67 if (cancel_url.is_valid()) {
68 url = net::AppendQueryParameter(url, kCancelURLParameterName,
69 cancel_url.spec());
70 }
71
72 if (!parameters.empty()) {
73 for (const auto& pair : parameters) {
74 url = net::AppendQueryParameter(url, pair.first, pair.second);
75 }
76 }
77
78 DCHECK(IsXCallbackURL(url));
79 return url;
80 }
81
82 std::map<std::string, std::string> ExtractQueryParametersFromXCallbackURL(
83 const GURL& x_callback_url) {
84 DCHECK(IsXCallbackURL(x_callback_url));
85 std::map<std::string, std::string> parameters;
86
87 net::QueryIterator query_iterator(x_callback_url);
88 while (!query_iterator.IsAtEnd()) {
89 parameters.insert(std::make_pair(query_iterator.GetKey(),
90 query_iterator.GetUnescapedValue()));
91 query_iterator.Advance();
92 }
93
94 return parameters;
95 }
OLDNEW
« no previous file with comments | « ios/chrome/common/x_callback_url.h ('k') | ios/chrome/common/x_callback_url_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698