Chromium Code Reviews| Index: chrome/browser/webshare/share_service_impl.cc |
| diff --git a/chrome/browser/webshare/share_service_impl.cc b/chrome/browser/webshare/share_service_impl.cc |
| index a5d57427c5664c09a77b40fbc93ae8dbd9b3f655..4c8e150a2f20d439df2a548036beae803d8fb44f 100644 |
| --- a/chrome/browser/webshare/share_service_impl.cc |
| +++ b/chrome/browser/webshare/share_service_impl.cc |
| @@ -4,8 +4,104 @@ |
| #include "chrome/browser/webshare/share_service_impl.h" |
| -#include "base/memory/ptr_util.h" |
| +#include <algorithm> |
| +#include <functional> |
| +#include <utility> |
| + |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_commands.h" |
| +#include "chrome/browser/ui/browser_list.h" |
| +#include "chrome/browser/ui/browser_tabstrip.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| #include "mojo/public/cpp/bindings/strong_binding.h" |
| +#include "net/base/escape.h" |
| + |
| +namespace { |
| +bool IsAlNum(char c) { |
| + 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.
|
| + (c >= '0' && c <= '9')); |
| +} |
| + |
| +// Splits |url_template| on "{" and "}", and stores in |split_template|. |
| +// Returns true if it successfully split the template, and false if an error |
| +// occurred. That is, if two "{" occur before a "}", a "{" occurs with no |
| +// following "}", or a "{" occurs with no preceding "%}". |
| +// Ensures elements at odd indices of |split_template| are the substrings |
| +// between "{" and "}" in the |url_template|. Thus, if "}" was immediately |
| +// followed by "{", an empty string is stored, or if there are no characters |
| +// before the first delimiter, the first element is "" (similar applies to the |
| +// last element). For example: |
| +// Input: "abc{def}{ghi}" |
| +// Output: {"abc", "def", "", "ghi", ""}. |
| +// This means that the returned value is always of odd size. |
| +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
|
| + std::vector<base::StringPiece>* split_template) { |
| + // Find open ("{") and close ("}") delimiters, and record indices. |
| + // If two opens occur before a close, or a close occurs with no preceding |
| + // open, return an error. |
| + std::vector<size_t> delimiter_indices; |
| + bool last_saw_open = false; |
| + for (size_t i = 0; i < url_template.size(); ++i) { |
| + if (last_saw_open) { |
| + if (url_template[i] == '}') { |
| + last_saw_open = false; |
| + delimiter_indices.push_back(i + 1); |
| + } else if (!IsAlNum(url_template[i]) && url_template[i] != '_' && |
| + url_template[i] != '-') { |
| + // Error: Non-identifier character seen after open. |
| + return false; |
| + } |
| + } else if (!last_saw_open) { |
| + if (url_template[i] == '}') { |
| + // Error: Saw close, with no corresponding open. |
| + return false; |
| + } else if (url_template[i] == '{') { |
| + last_saw_open = true; |
| + delimiter_indices.push_back(i); |
| + } |
| + } |
| + } |
| + if (last_saw_open) { |
| + // Error: Saw open that was never closed. |
| + return false; |
| + } |
| + |
| + // Split |url_template| at the indices given by |delimiter_indices|. |
| + split_template->clear(); |
| + int start_index_to_copy = 0; |
|
Sam McNally
2017/01/10 06:53:50
size_t
constantina
2017/01/12 04:04:41
Gone.
|
| + 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.
|
| + split_template->push_back(url_template.substr( |
| + start_index_to_copy, delimiter_indices[i] - start_index_to_copy)); |
| + start_index_to_copy = delimiter_indices[i]; |
| + } |
| + split_template->push_back(url_template.substr( |
| + start_index_to_copy, url_template.size() - start_index_to_copy)); |
| + |
| + // Remove { and } from placeholders (odd-indexed elements) in |
| + // |*split_template|. |
| + for (size_t i = 1; i < split_template->size(); i += 2) { |
| + base::StringPiece& placeholder = (*split_template)[i]; |
| + placeholder = placeholder.substr(1, placeholder.size() - 2); |
| + } |
| + return true; |
| +} |
| + |
| +// Joins a std::vector<base::StringPiece> into a single std::string. |
| +std::string JoinString(const std::vector<base::StringPiece>& pieces) { |
| + 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.
|
| + |
| + size_t total_size = 0; |
| + for (const base::StringPiece piece : pieces) { |
|
Sam McNally
2017/01/10 06:53:50
const auto&
constantina
2017/01/12 04:04:41
Done.
|
| + total_size += piece.size(); |
| + } |
| + joined_pieces.reserve(total_size); |
| + |
| + for (const auto& piece : pieces) { |
| + piece.AppendToString(&joined_pieces); |
| + } |
| + return joined_pieces; |
| +} |
| +} // namespace |
| // static |
| void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
| @@ -13,11 +109,75 @@ void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
| std::move(request)); |
| } |
| +// static |
| +bool ShareServiceImpl::ReplacePlaceholders(const std::string& url_template, |
| + const std::string& title, |
| + const std::string& text, |
| + const GURL& share_url, |
| + std::string* url_template_filled) { |
| + constexpr char kTitlePlaceholder[] = "title"; |
| + constexpr char kTextPlaceholder[] = "text"; |
| + constexpr char kUrlPlaceholder[] = "url"; |
| + |
| + std::map<base::StringPiece, std::string> placeholder_to_data; |
| + placeholder_to_data[kTitlePlaceholder] = |
| + net::EscapeQueryParamValue(title, false); |
| + placeholder_to_data[kTextPlaceholder] = |
| + net::EscapeQueryParamValue(text, false); |
| + placeholder_to_data[kUrlPlaceholder] = |
| + net::EscapeQueryParamValue(share_url.spec(), false); |
| + |
| + std::vector<base::StringPiece> split_template; |
| + 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.
|
| + if (!succeeded) { |
| + return false; |
| + } |
| + |
| + // Replace placeholders in |split_template| with share data. |
| + for (size_t i = 1; i < split_template.size(); i += 2) { |
| + base::StringPiece placeholder = split_template[i]; |
| + auto it = placeholder_to_data.find(placeholder); |
| + if (it != placeholder_to_data.end()) { |
| + split_template[i] = it->second; |
| + } else { |
| + split_template[i].clear(); |
| + } |
| + } |
| + |
| + *url_template_filled = JoinString(split_template); |
| + return true; |
| +} |
| + |
| +void ShareServiceImpl::OpenTargetURL(const GURL& target_url) { |
| + Browser* browser = BrowserList::GetInstance()->GetLastActive(); |
| + chrome::AddTabAt(browser, target_url, |
| + browser->tab_strip_model()->active_index() + 1, true); |
| +} |
| + |
| void ShareServiceImpl::Share(const std::string& title, |
| const std::string& text, |
| - const GURL& url, |
| + const GURL& share_url, |
| const ShareCallback& callback) { |
| - // TODO(constantina): Implement Web Share Target here. |
| - NOTIMPLEMENTED(); |
| - callback.Run(base::Optional<std::string>("Not implemented: navigator.share")); |
| + 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.
|
| + constexpr char kUrlTemplate[] = |
| + "demos/sharetarget.html?title={title}&text={text}&url={url}"; |
| + |
| + std::string url_template_filled; |
| + 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.
|
| + &url_template_filled); |
| + if (!succeeded) { |
| + callback.Run(base::Optional<std::string>( |
| + "Error: unable to replace placeholders in url template")); |
| + return; |
| + } |
| + |
| + GURL target_url(kUrlBase + url_template_filled); |
| + if (!succeeded || !target_url.is_valid()) { |
| + callback.Run(base::Optional<std::string>( |
| + "Error: url of share target is not a valid url.")); |
| + return; |
| + } |
| + OpenTargetURL(target_url); |
| + |
| + callback.Run(base::Optional<std::string>()); |
| } |