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 <cctype> | |
| 9 #include <functional> | |
| 10 #include <utility> | |
| 11 | |
| 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" | |
| 9 | 19 |
| 10 // static | 20 // static |
| 11 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { | 21 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
| 12 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), | 22 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), |
| 13 std::move(request)); | 23 std::move(request)); |
| 14 } | 24 } |
| 15 | 25 |
| 26 // static | |
| 27 bool ShareServiceImpl::SplitTemplate( | |
| 28 const std::string& url_template, | |
| 29 std::vector<base::StringPiece>& split_template) { | |
| 30 // Find open ("%{") and close ("}") delimeters, and record indices. | |
| 31 // If two opens occur before a close, or a close occurs with no preceding | |
| 32 // 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.
| |
| 33 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.
| |
| 34 bool last_saw_open = false; | |
| 35 for (size_t i = 0; i < url_template.size(); ++i) { | |
| 36 if (url_template[i] == '%' && i + 1 < url_template.size() && | |
| 37 url_template[i + 1] == '{') { | |
| 38 // Error: Saw two opens in a row; previous wasn't closed. | |
| 39 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.
| |
| 40 return true; | |
| 41 } | |
| 42 last_saw_open = true; | |
| 43 delimeter_indices.push_back(i); | |
| 44 i++; | |
| 45 } else if (url_template[i] == '}') { | |
| 46 // Error: Saw close, with no corresponding open. | |
| 47 if (!last_saw_open) { | |
| 48 return true; | |
| 49 } | |
| 50 last_saw_open = false; | |
| 51 delimeter_indices.push_back(i + 1); | |
| 52 } else if (last_saw_open) { | |
| 53 // Error: Non-identifier character seen after open. | |
| 54 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.
| |
| 55 url_template[i] != '_') { | |
| 56 return true; | |
| 57 } | |
| 58 } | |
| 59 } | |
| 60 // Error: Saw open that was never closed. | |
| 61 if (last_saw_open) { | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 // Split |url_template| at the indices given by |delimeter_indices|. | |
| 66 split_template.clear(); | |
| 67 int start_index_to_copy = 0; | |
| 68 for (size_t i = 0; i < delimeter_indices.size(); ++i) { | |
| 69 split_template.push_back( | |
| 70 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.
| |
| 71 delimeter_indices[i] - start_index_to_copy)); | |
| 72 start_index_to_copy = delimeter_indices[i]; | |
| 73 } | |
| 74 split_template.push_back( | |
| 75 base::StringPiece(&url_template[0] + start_index_to_copy, | |
| 76 url_template.size() - start_index_to_copy)); | |
| 77 | |
| 78 // 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
| |
| 79 for (size_t i = 1; i < split_template.size(); i += 2) { | |
| 80 base::StringPiece placeholder = split_template[i]; | |
| 81 split_template[i] = | |
| 82 base::StringPiece(placeholder.data() + 2, placeholder.size() - 3); | |
| 83 } | |
| 84 return 0; | |
|
Matt Giuca
2017/01/09 03:56:54
false
constantina
2017/01/09 23:51:04
Done.
| |
| 85 } | |
| 86 | |
| 87 // static | |
| 88 bool ShareServiceImpl::ReplacePlaceholders(const std::string& url_template, | |
| 89 const std::string& title, | |
| 90 const std::string& text, | |
| 91 const GURL& share_url, | |
| 92 std::string* url_template_filled) { | |
| 93 constexpr char kTitlePlaceholder[] = "title"; | |
| 94 constexpr char kTextPlaceholder[] = "text"; | |
| 95 constexpr char kUrlPlaceholder[] = "url"; | |
| 96 | |
| 97 std::map<base::StringPiece, std::string> placeholder_to_data; | |
| 98 placeholder_to_data[kTitlePlaceholder] = | |
| 99 net::EscapeQueryParamValue(title, false); | |
| 100 placeholder_to_data[kTextPlaceholder] = | |
| 101 net::EscapeQueryParamValue(text, false); | |
| 102 placeholder_to_data[kUrlPlaceholder] = | |
| 103 net::EscapeQueryParamValue(share_url.spec(), false); | |
| 104 | |
| 105 std::vector<base::StringPiece> split_template; | |
| 106 bool error = SplitTemplate(url_template, split_template); | |
| 107 if (error) | |
| 108 return error; | |
| 109 | |
| 110 std::string empty_string = ""; | |
| 111 // Replace placeholders in |split_template| with share data. | |
| 112 for (size_t i = 1; i < split_template.size(); i += 2) { | |
| 113 base::StringPiece placeholder = split_template[i]; | |
| 114 std::map<base::StringPiece, std::string>::iterator it = | |
| 115 placeholder_to_data.find(placeholder); | |
| 116 if (it != placeholder_to_data.end()) { | |
| 117 split_template[i] = base::StringPiece(it->second); | |
| 118 } else { | |
| 119 split_template[i] = empty_string; | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 // 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.
| |
| 124 url_template_filled->clear(); | |
| 125 int filled_size = 0; | |
| 126 for (const base::StringPiece piece : split_template) { | |
| 127 filled_size += piece.size(); | |
| 128 } | |
| 129 url_template_filled->reserve(filled_size); | |
| 130 | |
| 131 // Join |split_template|. | |
| 132 for (const base::StringPiece piece : split_template) { | |
| 133 *url_template_filled += piece.as_string(); | |
| 134 } | |
| 135 return 0; | |
|
Matt Giuca
2017/01/09 03:56:54
false.
constantina
2017/01/09 23:51:04
Done.
| |
| 136 } | |
| 137 | |
| 138 void ShareServiceImpl::OpenTargetURL(const GURL& target_url) { | |
| 139 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | |
| 140 chrome::AddTabAt(browser, target_url, | |
| 141 browser->tab_strip_model()->active_index() + 1, true); | |
| 142 } | |
| 143 | |
| 16 void ShareServiceImpl::Share(const std::string& title, | 144 void ShareServiceImpl::Share(const std::string& title, |
| 17 const std::string& text, | 145 const std::string& text, |
| 18 const GURL& url, | 146 const GURL& share_url, |
| 19 const ShareCallback& callback) { | 147 const ShareCallback& callback) { |
| 20 // TODO(constantina): Implement Web Share Target here. | 148 const char kUrlBase[] = "https://wicg.github.io/web-share-target/"; |
| 21 NOTIMPLEMENTED(); | 149 constexpr char url_template[] = |
| 22 callback.Run(base::Optional<std::string>("Not implemented: navigator.share")); | 150 "demos/sharetarget.html?title=%{title}&text=%{text}&url=%{url}"; |
| 151 | |
| 152 std::string url_template_filled; | |
| 153 bool error_occured = ReplacePlaceholders(url_template, title, text, share_url, | |
| 154 &url_template_filled); | |
| 155 | |
| 156 if (error_occured) { | |
| 157 callback.Run(base::Optional<std::string>("Error")); | |
| 158 return; | |
| 159 } | |
| 160 | |
| 161 GURL target_url(kUrlBase + url_template_filled); | |
| 162 OpenTargetURL(target_url); | |
| 163 | |
| 164 callback.Run(base::Optional<std::string>()); | |
| 23 } | 165 } |
| OLD | NEW |