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..0b93ff0e895dbcd7dda7079b8875c5eca1103739 100644 |
--- a/chrome/browser/webshare/share_service_impl.cc |
+++ b/chrome/browser/webshare/share_service_impl.cc |
@@ -4,8 +4,17 @@ |
#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" |
// static |
void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
@@ -13,11 +22,143 @@ void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
std::move(request)); |
} |
+// static |
+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.
|
+ const std::string& url_template, |
Sam McNally
2017/01/09 03:15:21
StringPiece
constantina
2017/01/09 23:51:04
Done.
|
+ 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.
|
+ // 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. |
+ 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.
|
+ bool last_saw_open = false; |
+ for (size_t i = 0; i < url_template.size(); ++i) { |
+ 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.
|
+ url_template[i + 1] == '{') { |
+ // Error: Saw two opens in a row; previous wasn't closed. |
+ if (last_saw_open) { |
+ 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.
|
+ } |
+ 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 (!isdigit(url_template[i]) && !isalpha(url_template[i]) && |
+ 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.
|
+ 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( |
Sam McNally
2017/01/09 03:15:21
(assuming |url_template| is a StringPiece)
push_b
constantina
2017/01/09 23:51:04
Done.
|
+ base::StringPiece(&url_template[0] + start_index_to_copy, |
+ 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|. |
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
|
+ for (size_t i = 1; i < split_template.size(); i += 2) { |
+ base::StringPiece placeholder = split_template[i]; |
Sam McNally
2017/01/09 03:15:21
base::StringPiece&
constantina
2017/01/09 23:51:04
Done.
|
+ 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.
|
+ base::StringPiece(placeholder.data() + 2, placeholder.size() - 3); |
+ } |
+ return 0; |
Sam McNally
2017/01/09 03:15:21
Don't implicitly convert to bool.
constantina
2017/01/09 23:51:03
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; |
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.
|
+ |
+ std::string empty_string = ""; |
Sam McNally
2017/01/09 03:15:21
Remove.
constantina
2017/01/09 23:51:03
Done.
|
+ // 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 = |
Sam McNally
2017/01/09 03:15:21
auto
constantina
2017/01/09 23:51:03
Done.
|
+ placeholder_to_data.find(placeholder); |
+ if (it != placeholder_to_data.end()) { |
+ 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.
|
+ } else { |
+ split_template[i] = empty_string; |
Sam McNally
2017/01/09 03:15:21
.clear();
constantina
2017/01/09 23:51:04
Done.
|
+ } |
+ } |
+ |
+ // Precalculate size of |url_filled_template|. |
+ url_template_filled->clear(); |
+ int filled_size = 0; |
Sam McNally
2017/01/09 03:15:21
size_t
constantina
2017/01/09 23:51:03
Done.
|
+ 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) { |
Sam McNally
2017/01/09 03:15:21
const auto& piece
constantina
2017/01/09 23:51:03
Done.
|
+ *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.
|
+ } |
+ return 0; |
+} |
+ |
+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); |
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.
|
+ OpenTargetURL(target_url); |
+ |
+ callback.Run(base::Optional<std::string>()); |
} |