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 "base/strings/string_util.h" | |
| 12 #include "chrome/browser/ui/browser.h" | |
| 13 #include "chrome/browser/ui/browser_commands.h" | |
| 14 #include "chrome/browser/ui/browser_list.h" | |
| 15 #include "chrome/browser/ui/browser_tabstrip.h" | |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 8 #include "mojo/public/cpp/bindings/strong_binding.h" | 17 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 18 #include "net/base/escape.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Determines whether a character is allowed in a URL template placeholder. | |
| 23 bool IsIdentifier(char c) { | |
| 24 return base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) || c == '-' || c == '_'; | |
| 25 } | |
| 26 | |
| 27 // Joins a std::vector<base::StringPiece> into a single std::string. | |
| 28 std::string JoinString(const std::vector<base::StringPiece>& pieces) { | |
|
kenrb
2017/01/12 14:53:37
Is there a reason you are implementing this rather
constantina
2017/01/13 00:40:06
It is simply because base::JoinString doesn't take
| |
| 29 size_t total_size = 0; | |
| 30 for (const auto& piece : pieces) { | |
| 31 total_size += piece.size(); | |
| 32 } | |
| 33 std::string joined_pieces; | |
| 34 joined_pieces.reserve(total_size); | |
| 35 | |
| 36 for (const auto& piece : pieces) { | |
| 37 piece.AppendToString(&joined_pieces); | |
| 38 } | |
| 39 return joined_pieces; | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 9 | 43 |
| 10 // static | 44 // static |
| 11 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { | 45 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
| 12 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), | 46 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), |
| 13 std::move(request)); | 47 std::move(request)); |
| 14 } | 48 } |
| 15 | 49 |
| 50 // static | |
| 51 bool ShareServiceImpl::ReplacePlaceholders(base::StringPiece url_template, | |
| 52 base::StringPiece title, | |
| 53 base::StringPiece text, | |
| 54 const GURL& share_url, | |
| 55 std::string* url_template_filled) { | |
| 56 constexpr char kTitlePlaceholder[] = "title"; | |
| 57 constexpr char kTextPlaceholder[] = "text"; | |
| 58 constexpr char kUrlPlaceholder[] = "url"; | |
| 59 | |
| 60 std::map<base::StringPiece, std::string> placeholder_to_data; | |
| 61 placeholder_to_data[kTitlePlaceholder] = | |
| 62 net::EscapeQueryParamValue(title, false); | |
| 63 placeholder_to_data[kTextPlaceholder] = | |
| 64 net::EscapeQueryParamValue(text, false); | |
| 65 placeholder_to_data[kUrlPlaceholder] = | |
| 66 net::EscapeQueryParamValue(share_url.spec(), false); | |
| 67 | |
| 68 std::vector<base::StringPiece> split_template; | |
| 69 bool last_saw_open = false; | |
| 70 size_t start_index_to_copy = 0; | |
| 71 for (size_t i = 0; i < url_template.size(); ++i) { | |
| 72 if (last_saw_open) { | |
| 73 if (url_template[i] == '}') { | |
| 74 base::StringPiece placeholder = url_template.substr( | |
| 75 start_index_to_copy + 1, i - 1 - start_index_to_copy); | |
| 76 auto it = placeholder_to_data.find(placeholder); | |
| 77 if (it != placeholder_to_data.end()) { | |
| 78 // Replace the placeholder text with the parameter value. | |
| 79 split_template.push_back(it->second); | |
| 80 } | |
| 81 | |
| 82 last_saw_open = false; | |
| 83 start_index_to_copy = i + 1; | |
| 84 } else if (!IsIdentifier(url_template[i])) { | |
| 85 // Error: Non-identifier character seen after open. | |
| 86 return false; | |
| 87 } | |
| 88 } else { | |
| 89 if (url_template[i] == '}') { | |
| 90 // Error: Saw close, with no corresponding open. | |
| 91 return false; | |
| 92 } else if (url_template[i] == '{') { | |
| 93 split_template.push_back( | |
| 94 url_template.substr(start_index_to_copy, i - start_index_to_copy)); | |
| 95 | |
| 96 last_saw_open = true; | |
| 97 start_index_to_copy = i; | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 if (last_saw_open) { | |
| 102 // Error: Saw open that was never closed. | |
| 103 return false; | |
| 104 } | |
| 105 split_template.push_back(url_template.substr( | |
| 106 start_index_to_copy, url_template.size() - start_index_to_copy)); | |
| 107 | |
| 108 *url_template_filled = JoinString(split_template); | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 void ShareServiceImpl::OpenTargetURL(const GURL& target_url) { | |
| 113 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | |
| 114 chrome::AddTabAt(browser, target_url, | |
| 115 browser->tab_strip_model()->active_index() + 1, true); | |
| 116 } | |
| 117 | |
| 16 void ShareServiceImpl::Share(const std::string& title, | 118 void ShareServiceImpl::Share(const std::string& title, |
| 17 const std::string& text, | 119 const std::string& text, |
| 18 const GURL& url, | 120 const GURL& share_url, |
| 19 const ShareCallback& callback) { | 121 const ShareCallback& callback) { |
| 20 // TODO(constantina): Implement Web Share Target here. | 122 // TODO(constantina): replace hard-coded URL with one from user-chosen site. |
| 21 NOTIMPLEMENTED(); | 123 constexpr char kUrlBase[] = "https://wicg.github.io/web-share-target/"; |
| 22 callback.Run(base::Optional<std::string>("Not implemented: navigator.share")); | 124 constexpr char kUrlTemplate[] = |
| 125 "demos/sharetarget.html?title={title}&text={text}&url={url}"; | |
| 126 | |
| 127 std::string url_template_filled; | |
| 128 if (!ReplacePlaceholders(kUrlTemplate, title, text, share_url, | |
| 129 &url_template_filled)) { | |
| 130 callback.Run(base::Optional<std::string>( | |
| 131 "Error: unable to replace placeholders in url template")); | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 GURL target_url(kUrlBase + url_template_filled); | |
| 136 if (!target_url.is_valid()) { | |
| 137 callback.Run(base::Optional<std::string>( | |
| 138 "Error: url of share target is not a valid url.")); | |
| 139 return; | |
| 140 } | |
| 141 OpenTargetURL(target_url); | |
| 142 | |
| 143 callback.Run(base::nullopt); | |
| 23 } | 144 } |
| OLD | NEW |