Chromium Code Reviews| 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" | |
| 18 | |
| 19 namespace { | |
| 20 bool IsAlNum(char c) { | |
| 21 return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | |
|
Sam McNally
2017/01/10 06:53:50
return base::IsAsciiAlpha(c) || base::IsAsciiDigit
constantina
2017/01/12 04:04:40
Done.
| |
| 22 (c >= '0' && c <= '9')); | |
| 23 } | |
| 24 | |
| 25 // Splits |url_template| on "{" and "}", and stores in |split_template|. | |
| 26 // Returns true if it successfully split the template, and false if an error | |
| 27 // occurred. That is, if two "{" occur before a "}", a "{" occurs with no | |
| 28 // following "}", or a "{" occurs with no preceding "%}". | |
| 29 // Ensures elements at odd indices of |split_template| are the substrings | |
| 30 // between "{" and "}" in the |url_template|. Thus, if "}" was immediately | |
| 31 // followed by "{", an empty string is stored, or if there are no characters | |
| 32 // before the first delimiter, the first element is "" (similar applies to the | |
| 33 // last element). For example: | |
| 34 // Input: "abc{def}{ghi}" | |
| 35 // Output: {"abc", "def", "", "ghi", ""}. | |
| 36 // This means that the returned value is always of odd size. | |
| 37 bool SplitTemplate(const base::StringPiece& url_template, | |
|
Sam McNally
2017/01/10 06:53:50
Take StringPiece by value.
constantina
2017/01/12 04:04:41
func is gone
| |
| 38 std::vector<base::StringPiece>* split_template) { | |
| 39 // Find open ("{") and close ("}") delimiters, and record indices. | |
| 40 // If two opens occur before a close, or a close occurs with no preceding | |
| 41 // open, return an error. | |
| 42 std::vector<size_t> delimiter_indices; | |
| 43 bool last_saw_open = false; | |
| 44 for (size_t i = 0; i < url_template.size(); ++i) { | |
| 45 if (last_saw_open) { | |
| 46 if (url_template[i] == '}') { | |
| 47 last_saw_open = false; | |
| 48 delimiter_indices.push_back(i + 1); | |
| 49 } else if (!IsAlNum(url_template[i]) && url_template[i] != '_' && | |
| 50 url_template[i] != '-') { | |
| 51 // Error: Non-identifier character seen after open. | |
| 52 return false; | |
| 53 } | |
| 54 } else if (!last_saw_open) { | |
| 55 if (url_template[i] == '}') { | |
| 56 // Error: Saw close, with no corresponding open. | |
| 57 return false; | |
| 58 } else if (url_template[i] == '{') { | |
| 59 last_saw_open = true; | |
| 60 delimiter_indices.push_back(i); | |
| 61 } | |
| 62 } | |
| 63 } | |
| 64 if (last_saw_open) { | |
| 65 // Error: Saw open that was never closed. | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 // Split |url_template| at the indices given by |delimiter_indices|. | |
| 70 split_template->clear(); | |
| 71 int start_index_to_copy = 0; | |
|
Sam McNally
2017/01/10 06:53:50
size_t
constantina
2017/01/12 04:04:41
Gone.
| |
| 72 for (size_t i = 0; i < delimiter_indices.size(); ++i) { | |
|
Sam McNally
2017/01/10 06:53:50
for (auto delimiter_index : delimiter_indices) {
constantina
2017/01/12 04:04:41
Gone.
| |
| 73 split_template->push_back(url_template.substr( | |
| 74 start_index_to_copy, delimiter_indices[i] - start_index_to_copy)); | |
| 75 start_index_to_copy = delimiter_indices[i]; | |
| 76 } | |
| 77 split_template->push_back(url_template.substr( | |
| 78 start_index_to_copy, url_template.size() - start_index_to_copy)); | |
| 79 | |
| 80 // Remove { and } from placeholders (odd-indexed elements) in | |
| 81 // |*split_template|. | |
| 82 for (size_t i = 1; i < split_template->size(); i += 2) { | |
| 83 base::StringPiece& placeholder = (*split_template)[i]; | |
| 84 placeholder = placeholder.substr(1, placeholder.size() - 2); | |
| 85 } | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 // Joins a std::vector<base::StringPiece> into a single std::string. | |
| 90 std::string JoinString(const std::vector<base::StringPiece>& pieces) { | |
| 91 std::string joined_pieces; | |
|
Sam McNally
2017/01/10 06:53:50
Declare this closer to where it's used.
constantina
2017/01/12 04:04:41
Done.
| |
| 92 | |
| 93 size_t total_size = 0; | |
| 94 for (const base::StringPiece piece : pieces) { | |
|
Sam McNally
2017/01/10 06:53:50
const auto&
constantina
2017/01/12 04:04:41
Done.
| |
| 95 total_size += piece.size(); | |
| 96 } | |
| 97 joined_pieces.reserve(total_size); | |
| 98 | |
| 99 for (const auto& piece : pieces) { | |
| 100 piece.AppendToString(&joined_pieces); | |
| 101 } | |
| 102 return joined_pieces; | |
| 103 } | |
| 104 } // namespace | |
| 9 | 105 |
| 10 // static | 106 // static |
| 11 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { | 107 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
| 12 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), | 108 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), |
| 13 std::move(request)); | 109 std::move(request)); |
| 14 } | 110 } |
| 15 | 111 |
| 112 // static | |
| 113 bool ShareServiceImpl::ReplacePlaceholders(const std::string& url_template, | |
| 114 const std::string& title, | |
| 115 const std::string& text, | |
| 116 const GURL& share_url, | |
| 117 std::string* url_template_filled) { | |
| 118 constexpr char kTitlePlaceholder[] = "title"; | |
| 119 constexpr char kTextPlaceholder[] = "text"; | |
| 120 constexpr char kUrlPlaceholder[] = "url"; | |
| 121 | |
| 122 std::map<base::StringPiece, std::string> placeholder_to_data; | |
| 123 placeholder_to_data[kTitlePlaceholder] = | |
| 124 net::EscapeQueryParamValue(title, false); | |
| 125 placeholder_to_data[kTextPlaceholder] = | |
| 126 net::EscapeQueryParamValue(text, false); | |
| 127 placeholder_to_data[kUrlPlaceholder] = | |
| 128 net::EscapeQueryParamValue(share_url.spec(), false); | |
| 129 | |
| 130 std::vector<base::StringPiece> split_template; | |
| 131 bool succeeded = SplitTemplate(url_template, &split_template); | |
|
Sam McNally
2017/01/10 06:53:50
if (!SplitTemplate(...)) {
return false;
}
constantina
2017/01/12 04:04:40
Gone.
| |
| 132 if (!succeeded) { | |
| 133 return false; | |
| 134 } | |
| 135 | |
| 136 // Replace placeholders in |split_template| with share data. | |
| 137 for (size_t i = 1; i < split_template.size(); i += 2) { | |
| 138 base::StringPiece placeholder = split_template[i]; | |
| 139 auto it = placeholder_to_data.find(placeholder); | |
| 140 if (it != placeholder_to_data.end()) { | |
| 141 split_template[i] = it->second; | |
| 142 } else { | |
| 143 split_template[i].clear(); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 *url_template_filled = JoinString(split_template); | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 void ShareServiceImpl::OpenTargetURL(const GURL& target_url) { | |
| 152 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | |
| 153 chrome::AddTabAt(browser, target_url, | |
| 154 browser->tab_strip_model()->active_index() + 1, true); | |
| 155 } | |
| 156 | |
| 16 void ShareServiceImpl::Share(const std::string& title, | 157 void ShareServiceImpl::Share(const std::string& title, |
| 17 const std::string& text, | 158 const std::string& text, |
| 18 const GURL& url, | 159 const GURL& share_url, |
| 19 const ShareCallback& callback) { | 160 const ShareCallback& callback) { |
| 20 // TODO(constantina): Implement Web Share Target here. | 161 const char kUrlBase[] = "https://wicg.github.io/web-share-target/"; |
|
Sam McNally
2017/01/10 06:53:50
constexpr
constantina
2017/01/12 04:04:41
Done.
| |
| 21 NOTIMPLEMENTED(); | 162 constexpr char kUrlTemplate[] = |
| 22 callback.Run(base::Optional<std::string>("Not implemented: navigator.share")); | 163 "demos/sharetarget.html?title={title}&text={text}&url={url}"; |
| 164 | |
| 165 std::string url_template_filled; | |
| 166 bool succeeded = ReplacePlaceholders(kUrlTemplate, title, text, share_url, | |
|
Sam McNally
2017/01/10 06:53:50
if (!ReplacePlaceholders(...)) {
constantina
2017/01/12 04:04:41
Done.
| |
| 167 &url_template_filled); | |
| 168 if (!succeeded) { | |
| 169 callback.Run(base::Optional<std::string>( | |
| 170 "Error: unable to replace placeholders in url template")); | |
| 171 return; | |
| 172 } | |
| 173 | |
| 174 GURL target_url(kUrlBase + url_template_filled); | |
| 175 if (!succeeded || !target_url.is_valid()) { | |
| 176 callback.Run(base::Optional<std::string>( | |
| 177 "Error: url of share target is not a valid url.")); | |
| 178 return; | |
| 179 } | |
| 180 OpenTargetURL(target_url); | |
| 181 | |
| 182 callback.Run(base::Optional<std::string>()); | |
| 23 } | 183 } |
| OLD | NEW |