OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/webshare/share_service_impl.h" | 5 #include "chrome/browser/webshare/share_service_impl.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include <algorithm> |
8 #include <functional> | |
9 #include <utility> | |
10 | |
11 #include "chrome/browser/ui/browser.h" | |
12 #include "chrome/browser/ui/browser_commands.h" | |
13 #include "chrome/browser/ui/browser_list.h" | |
14 #include "chrome/browser/ui/browser_tabstrip.h" | |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
8 #include "mojo/public/cpp/bindings/strong_binding.h" | 16 #include "mojo/public/cpp/bindings/strong_binding.h" |
17 #include "net/base/escape.h" | |
9 | 18 |
10 // static | 19 // static |
11 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { | 20 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
12 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), | 21 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), |
13 std::move(request)); | 22 std::move(request)); |
14 } | 23 } |
15 | 24 |
25 // static | |
26 bool ShareServiceImpl::SplitTemplate( | |
Sam McNally
2017/01/09 03:15:21
Make this a function in the anonymous namespace.
constantina
2017/01/09 23:51:04
Done.
| |
27 const std::string& url_template, | |
Sam McNally
2017/01/09 03:15:21
StringPiece
constantina
2017/01/09 23:51:04
Done.
| |
28 std::vector<base::StringPiece>& split_template) { | |
Sam McNally
2017/01/09 03:15:21
Use pointers for out parameters.
constantina
2017/01/09 23:51:04
Done.
| |
29 // Find open ("%{") and close ("}") delimeters, and record indices. | |
30 // If two opens occur before a close, or a close occurs with no preceding | |
31 // open, return an error code. | |
32 std::vector<int> delimeter_indices; | |
Sam McNally
2017/01/09 03:15:21
std::vector<size_t> delimiter_indices;
constantina
2017/01/09 23:51:04
Done.
| |
33 bool last_saw_open = false; | |
34 for (size_t i = 0; i < url_template.size(); ++i) { | |
35 if (url_template[i] == '%' && i + 1 < url_template.size() && | |
Sam McNally
2017/01/09 03:15:21
How about url_template.substr(i, 2) == "%{"?
constantina
2017/01/09 23:51:03
Done.
| |
36 url_template[i + 1] == '{') { | |
37 // Error: Saw two opens in a row; previous wasn't closed. | |
38 if (last_saw_open) { | |
39 return true; | |
Sam McNally
2017/01/09 03:15:21
When returning a bool, true usually means success.
constantina
2017/01/09 23:51:04
Swapped true and false.
| |
40 } | |
41 last_saw_open = true; | |
42 delimeter_indices.push_back(i); | |
43 i++; | |
44 } else if (url_template[i] == '}') { | |
45 // Error: Saw close, with no corresponding open. | |
46 if (!last_saw_open) { | |
47 return true; | |
48 } | |
49 last_saw_open = false; | |
50 delimeter_indices.push_back(i + 1); | |
51 } else if (last_saw_open) { | |
52 // Error: Non-identifier character seen after open. | |
53 if (!isdigit(url_template[i]) && !isalpha(url_template[i]) && | |
54 url_template[i] != '_' && url_template[i] != '_') { | |
Sam McNally
2017/01/09 03:15:21
We really like underscores?
constantina
2017/01/09 23:51:04
Fixed. We also love hyphens.
| |
55 return true; | |
56 } | |
57 } | |
58 } | |
59 // Error: Saw open that was never closed. | |
60 if (last_saw_open) { | |
61 return true; | |
62 } | |
63 | |
64 // Split |url_template| at the indices given by |delimeter_indices|. | |
65 split_template.clear(); | |
66 int start_index_to_copy = 0; | |
67 for (size_t i = 0; i < delimeter_indices.size(); ++i) { | |
68 split_template.push_back( | |
Sam McNally
2017/01/09 03:15:21
(assuming |url_template| is a StringPiece)
push_b
constantina
2017/01/09 23:51:04
Done.
| |
69 base::StringPiece(&url_template[0] + start_index_to_copy, | |
70 delimeter_indices[i] - start_index_to_copy)); | |
71 start_index_to_copy = delimeter_indices[i]; | |
72 } | |
73 split_template.push_back( | |
74 base::StringPiece(&url_template[0] + start_index_to_copy, | |
75 url_template.size() - start_index_to_copy)); | |
76 | |
77 // Remove %{ and } from placeholders in |split_template|. | |
Sam McNally
2017/01/09 03:15:21
I wonder if it might be simpler to store a before
Matt Giuca
2017/01/09 04:50:14
What do you mean exactly? Like separate index vars
constantina
2017/01/09 23:51:03
As discussed in person, we will store the StringPi
| |
78 for (size_t i = 1; i < split_template.size(); i += 2) { | |
79 base::StringPiece placeholder = split_template[i]; | |
Sam McNally
2017/01/09 03:15:21
base::StringPiece&
constantina
2017/01/09 23:51:04
Done.
| |
80 split_template[i] = | |
Sam McNally
2017/01/09 03:15:21
placeholder = placeholder.substr(2, placeholder.si
constantina
2017/01/09 23:51:04
Done.
| |
81 base::StringPiece(placeholder.data() + 2, placeholder.size() - 3); | |
82 } | |
83 return 0; | |
Sam McNally
2017/01/09 03:15:21
Don't implicitly convert to bool.
constantina
2017/01/09 23:51:03
Done.
| |
84 } | |
85 | |
86 // static | |
87 bool ShareServiceImpl::ReplacePlaceholders(const std::string& url_template, | |
88 const std::string& title, | |
89 const std::string& text, | |
90 const GURL& share_url, | |
91 std::string* url_template_filled) { | |
92 constexpr char kTitlePlaceholder[] = "title"; | |
93 constexpr char kTextPlaceholder[] = "text"; | |
94 constexpr char kUrlPlaceholder[] = "url"; | |
95 | |
96 std::map<base::StringPiece, std::string> placeholder_to_data; | |
97 placeholder_to_data[kTitlePlaceholder] = | |
98 net::EscapeQueryParamValue(title, false); | |
99 placeholder_to_data[kTextPlaceholder] = | |
100 net::EscapeQueryParamValue(text, false); | |
101 placeholder_to_data[kUrlPlaceholder] = | |
102 net::EscapeQueryParamValue(share_url.spec(), false); | |
103 | |
104 std::vector<base::StringPiece> split_template; | |
105 bool error = SplitTemplate(url_template, split_template); | |
106 if (error) | |
107 return error; | |
Sam McNally
2017/01/09 03:15:21
Be consistent with whether single line ifs have br
constantina
2017/01/09 23:51:04
Done.
| |
108 | |
109 std::string empty_string = ""; | |
Sam McNally
2017/01/09 03:15:21
Remove.
constantina
2017/01/09 23:51:03
Done.
| |
110 // Replace placeholders in |split_template| with share data. | |
111 for (size_t i = 1; i < split_template.size(); i += 2) { | |
112 base::StringPiece placeholder = split_template[i]; | |
113 std::map<base::StringPiece, std::string>::iterator it = | |
Sam McNally
2017/01/09 03:15:21
auto
constantina
2017/01/09 23:51:03
Done.
| |
114 placeholder_to_data.find(placeholder); | |
115 if (it != placeholder_to_data.end()) { | |
116 split_template[i] = base::StringPiece(it->second); | |
Sam McNally
2017/01/09 03:15:21
You shouldn't need an explicit cast here.
constantina
2017/01/09 23:51:03
Done.
| |
117 } else { | |
118 split_template[i] = empty_string; | |
Sam McNally
2017/01/09 03:15:21
.clear();
constantina
2017/01/09 23:51:04
Done.
| |
119 } | |
120 } | |
121 | |
122 // Precalculate size of |url_filled_template|. | |
123 url_template_filled->clear(); | |
124 int filled_size = 0; | |
Sam McNally
2017/01/09 03:15:21
size_t
constantina
2017/01/09 23:51:03
Done.
| |
125 for (const base::StringPiece piece : split_template) { | |
126 filled_size += piece.size(); | |
127 } | |
128 url_template_filled->reserve(filled_size); | |
129 | |
130 // Join |split_template|. | |
131 for (const base::StringPiece piece : split_template) { | |
Sam McNally
2017/01/09 03:15:21
const auto& piece
constantina
2017/01/09 23:51:03
Done.
| |
132 *url_template_filled += piece.as_string(); | |
Sam McNally
2017/01/09 03:15:21
piece.AppendToString(url_template_filled);
as_str
constantina
2017/01/09 23:51:03
Done.
| |
133 } | |
134 return 0; | |
135 } | |
136 | |
137 void ShareServiceImpl::OpenTargetURL(const GURL& target_url) { | |
138 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | |
139 chrome::AddTabAt(browser, target_url, | |
140 browser->tab_strip_model()->active_index() + 1, true); | |
141 } | |
142 | |
16 void ShareServiceImpl::Share(const std::string& title, | 143 void ShareServiceImpl::Share(const std::string& title, |
17 const std::string& text, | 144 const std::string& text, |
18 const GURL& url, | 145 const GURL& share_url, |
19 const ShareCallback& callback) { | 146 const ShareCallback& callback) { |
20 // TODO(constantina): Implement Web Share Target here. | 147 const char kUrlBase[] = "https://wicg.github.io/web-share-target/"; |
21 NOTIMPLEMENTED(); | 148 constexpr char url_template[] = |
22 callback.Run(base::Optional<std::string>("Not implemented: navigator.share")); | 149 "demos/sharetarget.html?title=%{title}&text=%{text}&url=%{url}"; |
150 | |
151 std::string url_template_filled; | |
152 bool error_occured = ReplacePlaceholders(url_template, title, text, share_url, | |
153 &url_template_filled); | |
154 | |
155 if (error_occured) { | |
156 callback.Run(base::Optional<std::string>("Error")); | |
157 return; | |
158 } | |
159 | |
160 GURL target_url(kUrlBase + url_template_filled); | |
Sam McNally
2017/01/09 03:15:21
We should probably return an error if !target_url.
constantina
2017/01/09 23:51:04
Done.
| |
161 OpenTargetURL(target_url); | |
162 | |
163 callback.Run(base::Optional<std::string>()); | |
23 } | 164 } |
OLD | NEW |