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 <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" | |
| 7 #include "mojo/public/cpp/bindings/strong_binding.h" | 16 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 17 #include "net/base/escape.h" | |
| 8 | 18 |
| 9 // static | 19 // static |
| 10 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { | 20 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
| 11 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), | 21 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), |
| 12 std::move(request)); | 22 std::move(request)); |
| 13 } | 23 } |
| 14 | 24 |
| 25 // static | |
| 26 std::string ShareServiceImpl::replacePlaceholders( | |
|
Matt Giuca
2016/12/21 07:13:49
As Sam suggested, you should just search (in one p
constantina
2017/01/04 07:01:44
Done.
| |
| 27 const std::string url_template, | |
| 28 const std::string title, | |
| 29 const std::string text, | |
| 30 const GURL& share_url) { | |
| 31 std::string title_escaped = net::EscapeQueryParamValue(title, false); | |
| 32 std::string text_escaped = net::EscapeQueryParamValue(text, false); | |
| 33 std::string share_url_escaped = | |
| 34 net::EscapeQueryParamValue(share_url.spec(), false); | |
| 35 | |
| 36 std::string title_placeholder = "%{title}"; | |
|
Matt Giuca
2016/12/21 07:13:49
Nit: kTitlePlaceholder, kTextPlaceholder and kUrlP
Sam McNally
2016/12/22 07:03:50
Better yet, use constexpr char kTitlePlaceholder[]
constantina
2017/01/04 07:01:44
Done.
constantina
2017/01/04 07:01:44
Done.
Matt Giuca
2017/01/05 03:09:24
Sam: Is your advice just to always use constexpr i
Sam McNally
2017/01/05 04:08:37
Yes, if constexpr is supported for the type.
| |
| 37 std::string text_placeholder = "%{text}"; | |
| 38 std::string url_placeholder = "%{url}"; | |
| 39 | |
| 40 std::vector<std::string> placeholders; | |
| 41 placeholders.push_back(title_placeholder); | |
| 42 placeholders.push_back(text_placeholder); | |
| 43 placeholders.push_back(url_placeholder); | |
| 44 | |
| 45 std::string url_template_filled = url_template; | |
| 46 | |
| 47 // Fill placeholder_positions with pair of position of ith placeholder in | |
| 48 // |placeholders|, and ith placeholder in |placeholders|. | |
| 49 std::vector<std::pair<int, std::string>> placeholder_positions; | |
|
Sam McNally
2016/12/22 07:03:50
Prefer base::StringPiece when you can refer to par
constantina
2017/01/04 07:01:44
Done. Also changed |placeholders|.
| |
| 50 for (std::string placeholder : placeholders) { | |
|
Matt Giuca
2016/12/21 07:13:49
const std::string&
or const auto&
constantina
2017/01/04 07:01:44
Done.
| |
| 51 std::size_t found = url_template_filled.find(placeholder); | |
| 52 while (found != std::string::npos) { | |
| 53 placeholder_positions.push_back( | |
| 54 std::pair<int, std::string>(found, placeholder)); | |
| 55 found = url_template_filled.find(placeholder, found + 1); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 // Sort placeholders by their index in the url template, in descending order. | |
| 60 // Processing in descending order means that when the latter placeholders are | |
| 61 // replaced, the positions of the earlier placeholders are not affected. | |
| 62 std::sort(placeholder_positions.begin(), placeholder_positions.end()); | |
| 63 std::reverse(placeholder_positions.begin(), placeholder_positions.end()); | |
| 64 | |
| 65 for (std::pair<int, std::string> placeholder : placeholder_positions) { | |
|
Matt Giuca
2016/12/21 07:13:49
For the second pass, you can avoid doing replaceme
constantina
2017/01/04 07:01:44
Done.
| |
| 66 int found = placeholder.first; | |
| 67 // Replace the placeholder with its corresponding share datum. | |
| 68 if (placeholder.second == title_placeholder) { | |
| 69 url_template_filled.replace(found, placeholder.second.size(), | |
| 70 title_escaped); | |
| 71 } else if (placeholder.second == text_placeholder) { | |
| 72 url_template_filled.replace(found, placeholder.second.size(), | |
| 73 text_escaped); | |
| 74 } else if (placeholder.second == url_placeholder) { | |
| 75 url_template_filled.replace(found, placeholder.second.size(), | |
| 76 share_url_escaped); | |
| 77 } | |
| 78 } | |
| 79 return url_template_filled; | |
| 80 } | |
| 81 | |
| 82 void ShareServiceImpl::openTargetURL(GURL target_url) { | |
| 83 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | |
| 84 chrome::AddTabAt(browser, target_url, | |
| 85 browser->tab_strip_model()->active_index() + 1, true); | |
| 86 } | |
| 87 | |
| 15 void ShareServiceImpl::Share(const std::string& title, | 88 void ShareServiceImpl::Share(const std::string& title, |
| 16 const std::string& text, | 89 const std::string& text, |
| 17 const GURL& url, | 90 const GURL& share_url, |
| 18 const ShareCallback& callback) { | 91 const ShareCallback& callback) { |
| 19 // TODO(constantina): Implement Web Share Target here. | 92 std::string url_base = "https://wicg.github.io/web-share-target/"; |
|
Matt Giuca
2016/12/21 07:13:49
const char kUrlBase[]
constantina
2017/01/04 07:01:44
Done.
| |
| 20 NOTIMPLEMENTED(); | 93 std::string url_template = |
| 21 callback.Run(base::Optional<std::string>("Not implemented: navigator.share")); | 94 "demos/sharetarget.html?title=%{title}&text=%{text}&url=%{url}"; |
| 95 | |
| 96 std::string url_template_filled = | |
| 97 replacePlaceholders(url_template, title, text, share_url); | |
| 98 | |
| 99 GURL target_url(url_base + url_template_filled); | |
| 100 openTargetURL(target_url); | |
| 101 | |
| 102 // TODO(constantina): Check for errors. | |
|
Matt Giuca
2016/12/21 07:13:49
You should do this in this CL. If you don't have a
constantina
2017/01/04 07:01:44
Done.
| |
| 103 bool error_occured = false; | |
| 104 if (error_occured) { | |
| 105 callback.Run(base::Optional<std::string>("Error")); | |
| 106 } else { | |
| 107 callback.Run(base::Optional<std::string>()); | |
| 108 } | |
| 22 } | 109 } |
| OLD | NEW |