Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: chrome/browser/webshare/share_service_impl.cc

Issue 2691193002: Added StringPiece overloads for base::JoinString. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_commands.h" 14 #include "chrome/browser/ui/browser_commands.h"
15 #include "chrome/browser/ui/browser_dialogs.h" 15 #include "chrome/browser/ui/browser_dialogs.h"
16 #include "chrome/browser/ui/browser_list.h" 16 #include "chrome/browser/ui/browser_list.h"
17 #include "chrome/browser/ui/browser_tabstrip.h" 17 #include "chrome/browser/ui/browser_tabstrip.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" 18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "mojo/public/cpp/bindings/strong_binding.h" 19 #include "mojo/public/cpp/bindings/strong_binding.h"
20 #include "net/base/escape.h" 20 #include "net/base/escape.h"
21 21
22 namespace { 22 namespace {
23 23
24 // Determines whether a character is allowed in a URL template placeholder. 24 // Determines whether a character is allowed in a URL template placeholder.
25 bool IsIdentifier(char c) { 25 bool IsIdentifier(char c) {
26 return base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) || c == '-' || c == '_'; 26 return base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) || c == '-' || c == '_';
27 } 27 }
28 28
29 // Joins a std::vector<base::StringPiece> into a single std::string.
30 // TODO(constantina): Implement a base::JoinString() that takes StringPieces.
31 // i.e. move this to base/strings/string_util.h, and thoroughly test.
32 std::string JoinString(const std::vector<base::StringPiece>& pieces) {
33 size_t total_size = 0;
34 for (const auto& piece : pieces) {
35 total_size += piece.size();
36 }
37 std::string joined_pieces;
38 joined_pieces.reserve(total_size);
39
40 for (const auto& piece : pieces) {
41 piece.AppendToString(&joined_pieces);
42 }
43 return joined_pieces;
44 }
45
46 } // namespace 29 } // namespace
47 30
48 // static 31 // static
49 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { 32 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) {
50 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), 33 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(),
51 std::move(request)); 34 std::move(request));
52 } 35 }
53 36
54 // static 37 // static
55 bool ShareServiceImpl::ReplacePlaceholders(base::StringPiece url_template, 38 bool ShareServiceImpl::ReplacePlaceholders(base::StringPiece url_template,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 } 85 }
103 } 86 }
104 } 87 }
105 if (last_saw_open) { 88 if (last_saw_open) {
106 // Error: Saw open that was never closed. 89 // Error: Saw open that was never closed.
107 return false; 90 return false;
108 } 91 }
109 split_template.push_back(url_template.substr( 92 split_template.push_back(url_template.substr(
110 start_index_to_copy, url_template.size() - start_index_to_copy)); 93 start_index_to_copy, url_template.size() - start_index_to_copy));
111 94
112 *url_template_filled = JoinString(split_template); 95 *url_template_filled = base::JoinStringPiece(split_template, "");
113 return true; 96 return true;
114 } 97 }
115 98
116 void ShareServiceImpl::ShowPickerDialog( 99 void ShareServiceImpl::ShowPickerDialog(
117 const std::vector<std::pair<base::string16, GURL>>& targets, 100 const std::vector<std::pair<base::string16, GURL>>& targets,
118 const base::Callback<void(base::Optional<std::string>)>& callback) { 101 const base::Callback<void(base::Optional<std::string>)>& callback) {
119 // TODO(mgiuca): Get the browser window as |parent_window|. 102 // TODO(mgiuca): Get the browser window as |parent_window|.
120 #if defined(OS_LINUX) || defined(OS_WIN) 103 #if defined(OS_LINUX) || defined(OS_WIN)
121 chrome::ShowWebShareTargetPickerDialog(nullptr /* parent_window */, targets, 104 chrome::ShowWebShareTargetPickerDialog(nullptr /* parent_window */, targets,
122 callback); 105 callback);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 GURL target_url(url_template_filled); 163 GURL target_url(url_template_filled);
181 if (!target_url.is_valid()) { 164 if (!target_url.is_valid()) {
182 callback.Run(base::Optional<std::string>( 165 callback.Run(base::Optional<std::string>(
183 "Error: url of share target is not a valid url.")); 166 "Error: url of share target is not a valid url."));
184 return; 167 return;
185 } 168 }
186 OpenTargetURL(target_url); 169 OpenTargetURL(target_url);
187 170
188 callback.Run(base::nullopt); 171 callback.Run(base::nullopt);
189 } 172 }
OLDNEW
« base/strings/string_util.cc ('K') | « base/strings/string_util_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698