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