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..3de68d653c0e0b177ae1f0748469bdcc47f2c1df 100644 |
| --- a/chrome/browser/webshare/share_service_impl.cc |
| +++ b/chrome/browser/webshare/share_service_impl.cc |
| @@ -4,8 +4,18 @@ |
| #include "chrome/browser/webshare/share_service_impl.h" |
| -#include "base/memory/ptr_util.h" |
| +#include <algorithm> |
| +#include <cctype> |
| +#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" |
| // static |
| void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
| @@ -13,11 +23,143 @@ void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
| std::move(request)); |
| } |
| +// static |
| +bool ShareServiceImpl::SplitTemplate( |
| + const std::string& url_template, |
| + std::vector<base::StringPiece>& split_template) { |
| + // Find open ("%{") and close ("}") delimeters, and record indices. |
| + // If two opens occur before a close, or a close occurs with no preceding |
| + // open, return an error code. |
|
Matt Giuca
2017/01/09 03:56:54
Drop "code" (since we're just returning a bool).
constantina
2017/01/09 23:51:04
Done.
|
| + std::vector<int> delimeter_indices; |
|
Matt Giuca
2017/01/09 03:56:54
Spelling: delimiter, not delimeter.
constantina
2017/01/09 23:51:04
Done.
|
| + bool last_saw_open = false; |
| + for (size_t i = 0; i < url_template.size(); ++i) { |
| + if (url_template[i] == '%' && i + 1 < url_template.size() && |
| + url_template[i + 1] == '{') { |
| + // Error: Saw two opens in a row; previous wasn't closed. |
| + if (last_saw_open) { |
|
Matt Giuca
2017/01/09 03:56:54
Note: You don't *have* to include the braces if yo
constantina
2017/01/09 23:51:04
Done.
|
| + return true; |
| + } |
| + last_saw_open = true; |
| + delimeter_indices.push_back(i); |
| + i++; |
| + } else if (url_template[i] == '}') { |
| + // Error: Saw close, with no corresponding open. |
| + if (!last_saw_open) { |
| + return true; |
| + } |
| + last_saw_open = false; |
| + delimeter_indices.push_back(i + 1); |
| + } else if (last_saw_open) { |
| + // Error: Non-identifier character seen after open. |
| + if (!isalnum(url_template[i]) && url_template[i] != '_' && |
|
Matt Giuca
2017/01/09 03:56:54
You repeated url_template[i] != '_' twice... did y
constantina
2017/01/09 23:51:04
Done.
|
| + url_template[i] != '_') { |
| + return true; |
| + } |
| + } |
| + } |
| + // Error: Saw open that was never closed. |
| + if (last_saw_open) { |
| + return true; |
| + } |
| + |
| + // Split |url_template| at the indices given by |delimeter_indices|. |
| + split_template.clear(); |
| + int start_index_to_copy = 0; |
| + for (size_t i = 0; i < delimeter_indices.size(); ++i) { |
| + split_template.push_back( |
| + base::StringPiece(&url_template[0] + start_index_to_copy, |
|
Matt Giuca
2017/01/09 03:56:54
Replace "&url_template[0]" with "url_template.data
constantina
2017/01/09 23:51:04
Done.
|
| + delimeter_indices[i] - start_index_to_copy)); |
| + start_index_to_copy = delimeter_indices[i]; |
| + } |
| + split_template.push_back( |
| + base::StringPiece(&url_template[0] + start_index_to_copy, |
| + url_template.size() - start_index_to_copy)); |
| + |
| + // Remove %{ and } from placeholders in |split_template|. |
|
Matt Giuca
2017/01/09 03:56:54
..."from placeholders (odd-numbered elements)"...
constantina
2017/01/09 23:51:04
Done. Used odd-indexed
|
| + for (size_t i = 1; i < split_template.size(); i += 2) { |
| + base::StringPiece placeholder = split_template[i]; |
| + split_template[i] = |
| + base::StringPiece(placeholder.data() + 2, placeholder.size() - 3); |
| + } |
| + return 0; |
|
Matt Giuca
2017/01/09 03:56:54
false
constantina
2017/01/09 23:51:04
Done.
|
| +} |
| + |
| +// 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 error = SplitTemplate(url_template, split_template); |
| + if (error) |
| + return error; |
| + |
| + std::string empty_string = ""; |
| + // 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]; |
| + std::map<base::StringPiece, std::string>::iterator it = |
| + placeholder_to_data.find(placeholder); |
| + if (it != placeholder_to_data.end()) { |
| + split_template[i] = base::StringPiece(it->second); |
| + } else { |
| + split_template[i] = empty_string; |
| + } |
| + } |
| + |
| + // Precalculate size of |url_filled_template|. |
|
Matt Giuca
2017/01/09 03:56:54
From here to the end of the function can be very n
constantina
2017/01/09 23:51:04
Done.
|
| + url_template_filled->clear(); |
| + int filled_size = 0; |
| + for (const base::StringPiece piece : split_template) { |
| + filled_size += piece.size(); |
| + } |
| + url_template_filled->reserve(filled_size); |
| + |
| + // Join |split_template|. |
| + for (const base::StringPiece piece : split_template) { |
| + *url_template_filled += piece.as_string(); |
| + } |
| + return 0; |
|
Matt Giuca
2017/01/09 03:56:54
false.
constantina
2017/01/09 23:51:04
Done.
|
| +} |
| + |
| +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/"; |
| + constexpr char url_template[] = |
| + "demos/sharetarget.html?title=%{title}&text=%{text}&url=%{url}"; |
| + |
| + std::string url_template_filled; |
| + bool error_occured = ReplacePlaceholders(url_template, title, text, share_url, |
| + &url_template_filled); |
| + |
| + if (error_occured) { |
| + callback.Run(base::Optional<std::string>("Error")); |
| + return; |
| + } |
| + |
| + GURL target_url(kUrlBase + url_template_filled); |
| + OpenTargetURL(target_url); |
| + |
| + callback.Run(base::Optional<std::string>()); |
| } |