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> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "mojo/public/cpp/bindings/strong_binding.h" | 22 #include "mojo/public/cpp/bindings/strong_binding.h" |
23 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 // Determines whether a character is allowed in a URL template placeholder. | 27 // Determines whether a character is allowed in a URL template placeholder. |
28 bool IsIdentifier(char c) { | 28 bool IsIdentifier(char c) { |
29 return base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) || c == '-' || c == '_'; | 29 return base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) || c == '-' || c == '_'; |
30 } | 30 } |
31 | 31 |
32 // Joins a std::vector<base::StringPiece> into a single std::string. | |
33 // TODO(constantina): Implement a base::JoinString() that takes StringPieces. | |
34 // i.e. move this to base/strings/string_util.h, and thoroughly test. | |
35 std::string JoinString(const std::vector<base::StringPiece>& pieces) { | |
36 size_t total_size = 0; | |
37 for (const auto& piece : pieces) { | |
38 total_size += piece.size(); | |
39 } | |
40 std::string joined_pieces; | |
41 joined_pieces.reserve(total_size); | |
42 | |
43 for (const auto& piece : pieces) { | |
44 piece.AppendToString(&joined_pieces); | |
45 } | |
46 return joined_pieces; | |
47 } | |
48 | |
49 } // namespace | 32 } // namespace |
50 | 33 |
51 ShareServiceImpl::ShareServiceImpl() : weak_factory_(this) {} | 34 ShareServiceImpl::ShareServiceImpl() : weak_factory_(this) {} |
52 ShareServiceImpl::~ShareServiceImpl() = default; | 35 ShareServiceImpl::~ShareServiceImpl() = default; |
53 | 36 |
54 // static | 37 // static |
55 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { | 38 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
56 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), | 39 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), |
57 std::move(request)); | 40 std::move(request)); |
58 } | 41 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 } | 91 } |
109 } | 92 } |
110 } | 93 } |
111 if (last_saw_open) { | 94 if (last_saw_open) { |
112 // Error: Saw open that was never closed. | 95 // Error: Saw open that was never closed. |
113 return false; | 96 return false; |
114 } | 97 } |
115 split_template.push_back(url_template.substr( | 98 split_template.push_back(url_template.substr( |
116 start_index_to_copy, url_template.size() - start_index_to_copy)); | 99 start_index_to_copy, url_template.size() - start_index_to_copy)); |
117 | 100 |
118 *url_template_filled = JoinString(split_template); | 101 *url_template_filled = base::JoinString(split_template, ""); |
119 return true; | 102 return true; |
120 } | 103 } |
121 | 104 |
122 void ShareServiceImpl::ShowPickerDialog( | 105 void ShareServiceImpl::ShowPickerDialog( |
123 const std::vector<std::pair<base::string16, GURL>>& targets, | 106 const std::vector<std::pair<base::string16, GURL>>& targets, |
124 const base::Callback<void(base::Optional<std::string>)>& callback) { | 107 const base::Callback<void(base::Optional<std::string>)>& callback) { |
125 // TODO(mgiuca): Get the browser window as |parent_window|. | 108 // TODO(mgiuca): Get the browser window as |parent_window|. |
126 chrome::ShowWebShareTargetPickerDialog(nullptr /* parent_window */, targets, | 109 chrome::ShowWebShareTargetPickerDialog(nullptr /* parent_window */, targets, |
127 callback); | 110 callback); |
128 } | 111 } |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 const GURL target(url_base.as_string() + url_template_filled); | 222 const GURL target(url_base.as_string() + url_template_filled); |
240 if (!target.is_valid()) { | 223 if (!target.is_valid()) { |
241 callback.Run(base::Optional<std::string>( | 224 callback.Run(base::Optional<std::string>( |
242 "Error: url of share target is not a valid url.")); | 225 "Error: url of share target is not a valid url.")); |
243 return; | 226 return; |
244 } | 227 } |
245 OpenTargetURL(target); | 228 OpenTargetURL(target); |
246 | 229 |
247 callback.Run(base::nullopt); | 230 callback.Run(base::nullopt); |
248 } | 231 } |
OLD | NEW |