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 |
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/engagement/site_engagement_service.h" |
14 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/browser/ui/browser_commands.h" | 15 #include "chrome/browser/ui/browser_commands.h" |
15 #include "chrome/browser/ui/browser_dialogs.h" | 16 #include "chrome/browser/ui/browser_dialogs.h" |
16 #include "chrome/browser/ui/browser_list.h" | 17 #include "chrome/browser/ui/browser_list.h" |
17 #include "chrome/browser/ui/browser_tabstrip.h" | 18 #include "chrome/browser/ui/browser_tabstrip.h" |
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
20 #include "chrome/common/pref_names.h" | |
21 #include "components/prefs/pref_service.h" | |
19 #include "mojo/public/cpp/bindings/strong_binding.h" | 22 #include "mojo/public/cpp/bindings/strong_binding.h" |
20 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
21 | 24 |
22 namespace { | 25 namespace { |
23 | 26 |
24 // Determines whether a character is allowed in a URL template placeholder. | 27 // Determines whether a character is allowed in a URL template placeholder. |
25 bool IsIdentifier(char c) { | 28 bool IsIdentifier(char c) { |
26 return base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) || c == '-' || c == '_'; | 29 return base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) || c == '-' || c == '_'; |
27 } | 30 } |
28 | 31 |
29 // Joins a std::vector<base::StringPiece> into a single std::string. | 32 // Joins a std::vector<base::StringPiece> into a single std::string. |
30 // TODO(constantina): Implement a base::JoinString() that takes StringPieces. | 33 // TODO(constantina): Implement a base::JoinString() that takes StringPieces. |
31 // i.e. move this to base/strings/string_util.h, and thoroughly test. | 34 // i.e. move this to base/strings/string_util.h, and thoroughly test. |
32 std::string JoinString(const std::vector<base::StringPiece>& pieces) { | 35 std::string JoinString(const std::vector<base::StringPiece>& pieces) { |
33 size_t total_size = 0; | 36 size_t total_size = 0; |
34 for (const auto& piece : pieces) { | 37 for (const auto& piece : pieces) { |
35 total_size += piece.size(); | 38 total_size += piece.size(); |
36 } | 39 } |
37 std::string joined_pieces; | 40 std::string joined_pieces; |
38 joined_pieces.reserve(total_size); | 41 joined_pieces.reserve(total_size); |
39 | 42 |
40 for (const auto& piece : pieces) { | 43 for (const auto& piece : pieces) { |
41 piece.AppendToString(&joined_pieces); | 44 piece.AppendToString(&joined_pieces); |
42 } | 45 } |
43 return joined_pieces; | 46 return joined_pieces; |
44 } | 47 } |
45 | 48 |
46 } // namespace | 49 } // namespace |
47 | 50 |
51 ShareServiceImpl::ShareServiceImpl() = default; | |
52 ShareServiceImpl::~ShareServiceImpl() = default; | |
53 | |
48 // static | 54 // static |
49 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { | 55 void ShareServiceImpl::Create(blink::mojom::ShareServiceRequest request) { |
50 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), | 56 mojo::MakeStrongBinding(base::MakeUnique<ShareServiceImpl>(), |
51 std::move(request)); | 57 std::move(request)); |
52 } | 58 } |
53 | 59 |
54 // static | 60 // static |
55 bool ShareServiceImpl::ReplacePlaceholders(base::StringPiece url_template, | 61 bool ShareServiceImpl::ReplacePlaceholders(base::StringPiece url_template, |
56 base::StringPiece title, | 62 base::StringPiece title, |
57 base::StringPiece text, | 63 base::StringPiece text, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 split_template.push_back(url_template.substr( | 115 split_template.push_back(url_template.substr( |
110 start_index_to_copy, url_template.size() - start_index_to_copy)); | 116 start_index_to_copy, url_template.size() - start_index_to_copy)); |
111 | 117 |
112 *url_template_filled = JoinString(split_template); | 118 *url_template_filled = JoinString(split_template); |
113 return true; | 119 return true; |
114 } | 120 } |
115 | 121 |
116 void ShareServiceImpl::ShowPickerDialog( | 122 void ShareServiceImpl::ShowPickerDialog( |
117 const std::vector<std::pair<base::string16, GURL>>& targets, | 123 const std::vector<std::pair<base::string16, GURL>>& targets, |
118 const base::Callback<void(base::Optional<std::string>)>& callback) { | 124 const base::Callback<void(base::Optional<std::string>)>& callback) { |
119 // TODO(mgiuca): Get the browser window as |parent_window|. | 125 // TODO(mgiuca): Get the browser window as |parent_window|. |
120 #if defined(OS_LINUX) || defined(OS_WIN) | 126 #if defined(OS_LINUX) || defined(OS_WIN) |
121 chrome::ShowWebShareTargetPickerDialog(nullptr /* parent_window */, targets, | 127 chrome::ShowWebShareTargetPickerDialog(nullptr /* parent_window */, targets, |
122 callback); | 128 callback); |
123 #else | 129 #else |
124 callback.Run(base::nullopt); | 130 callback.Run(base::nullopt); |
125 #endif | 131 #endif |
126 } | 132 } |
127 | 133 |
134 Browser* ShareServiceImpl::GetBrowser() { | |
135 return BrowserList::GetInstance()->GetLastActive(); | |
136 } | |
137 | |
128 void ShareServiceImpl::OpenTargetURL(const GURL& target_url) { | 138 void ShareServiceImpl::OpenTargetURL(const GURL& target_url) { |
129 // TODO(constantina): Prevent this code from being run/compiled in android. | 139 // TODO(constantina): Prevent this code from being run/compiled in android. |
130 #if defined(OS_LINUX) || defined(OS_WIN) | 140 #if defined(OS_LINUX) || defined(OS_WIN) |
131 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | 141 Browser* browser = GetBrowser(); |
132 chrome::AddTabAt(browser, target_url, | 142 chrome::AddTabAt(browser, target_url, |
133 browser->tab_strip_model()->active_index() + 1, true); | 143 browser->tab_strip_model()->active_index() + 1, true); |
134 #endif | 144 #endif |
135 } | 145 } |
136 | 146 |
147 std::string ShareServiceImpl::GetTargetTemplate(const std::string& target_url) { | |
148 const base::DictionaryValue* share_target_info_dict = nullptr; | |
149 share_targets->GetDictionaryWithoutPathExpansion(target_url, | |
150 &share_target_info_dict); | |
151 | |
152 std::string url_template; | |
153 share_target_info_dict->GetString("url_template", &url_template); | |
154 return url_template; | |
155 } | |
156 | |
157 PrefService* ShareServiceImpl::GetPrefService() { | |
158 return GetBrowser()->profile()->GetPrefs(); | |
159 } | |
160 | |
161 blink::mojom::EngagementLevel ShareServiceImpl::GetEngagementLevel( | |
162 const GURL& url) { | |
163 SiteEngagementService* site_engagement_service = | |
164 SiteEngagementService::Get(GetBrowser()->profile()); | |
165 return site_engagement_service->GetEngagementLevel(url); | |
166 } | |
167 | |
168 // static | |
169 std::vector<std::pair<base::string16, GURL>> | |
170 ShareServiceImpl::GetTargetsWithSufficientEngagement() { | |
171 constexpr blink::mojom::EngagementLevel kMinimumEngagementLevel = | |
172 blink::mojom::EngagementLevel::LOW; | |
173 | |
174 share_targets = GetPrefService() | |
Matt Giuca
2017/02/09 05:47:16
Move this line into ::Share (so it isn't a side-ef
constantina
2017/02/10 00:43:09
Done.
| |
175 ->GetDictionary(prefs::kWebShareVisitedTargets) | |
176 ->CreateDeepCopy(); | |
177 | |
178 std::vector<std::pair<base::string16, GURL>> sufficiently_engaged_targets; | |
179 | |
180 for (base::DictionaryValue::Iterator it(*share_targets); !it.IsAtEnd(); | |
181 it.Advance()) { | |
182 GURL manifest_url(it.key()); | |
183 if (GetEngagementLevel(manifest_url) >= kMinimumEngagementLevel) { | |
184 const base::DictionaryValue* share_target_dict; | |
185 bool result = it.value().GetAsDictionary(&share_target_dict); | |
186 DCHECK(result); | |
187 | |
188 std::string name; | |
189 share_target_dict->GetString("name", &name); | |
190 | |
191 sufficiently_engaged_targets.push_back( | |
192 make_pair(base::UTF8ToUTF16(name), manifest_url)); | |
193 } | |
194 } | |
195 | |
196 return sufficiently_engaged_targets; | |
197 } | |
198 | |
137 void ShareServiceImpl::Share(const std::string& title, | 199 void ShareServiceImpl::Share(const std::string& title, |
138 const std::string& text, | 200 const std::string& text, |
139 const GURL& share_url, | 201 const GURL& share_url, |
140 const ShareCallback& callback) { | 202 const ShareCallback& callback) { |
141 // TODO(constantina): Replace hard-coded name and manifest URL with the list | 203 std::vector<std::pair<base::string16, GURL>> targets = |
142 // of registered targets' manifest URLs. | 204 GetTargetsWithSufficientEngagement(); |
143 constexpr char kTargetName[] = "Web Share Target Test App"; | |
144 constexpr char kManifestURL[] = | |
145 "https://wicg.github.io/web-share-target/demos/manifest.json"; | |
146 // TODO(constantina): Pass vector of pairs of target names and manifest URLs | |
147 // to picker. | |
148 std::vector<std::pair<base::string16, GURL>> targets{make_pair( | |
149 base::ASCIIToUTF16(kTargetName), GURL(kManifestURL))}; | |
150 | 205 |
151 ShowPickerDialog(targets, base::Bind(&ShareServiceImpl::OnPickerClosed, | 206 ShowPickerDialog(targets, base::Bind(&ShareServiceImpl::OnPickerClosed, |
152 base::Unretained(this), title, text, | 207 base::Unretained(this), title, text, |
153 share_url, callback)); | 208 share_url, callback)); |
154 } | 209 } |
155 | 210 |
156 void ShareServiceImpl::OnPickerClosed(const std::string& title, | 211 void ShareServiceImpl::OnPickerClosed(const std::string& title, |
157 const std::string& text, | 212 const std::string& text, |
158 const GURL& share_url, | 213 const GURL& share_url, |
159 const ShareCallback& callback, | 214 const ShareCallback& callback, |
160 base::Optional<std::string> result) { | 215 base::Optional<std::string> result) { |
161 if (!result.has_value()) { | 216 if (!result.has_value()) { |
162 callback.Run(base::Optional<std::string>("Share was cancelled")); | 217 callback.Run(base::Optional<std::string>("Share was cancelled")); |
163 return; | 218 return; |
164 } | 219 } |
165 | 220 |
166 // TODO(constantina): use manifest URL in result to look up corresponding URL | 221 std::string chosen_target = result.value(); |
167 // template. | |
168 constexpr char kUrlTemplate[] = | |
169 "https://wicg.github.io/web-share-target/demos/" | |
170 "sharetarget.html?title={title}&text={text}&url={url}"; | |
171 | 222 |
223 std::string url_template = GetTargetTemplate(chosen_target); | |
172 std::string url_template_filled; | 224 std::string url_template_filled; |
173 if (!ReplacePlaceholders(kUrlTemplate, title, text, share_url, | 225 if (!ReplacePlaceholders(url_template, title, text, share_url, |
174 &url_template_filled)) { | 226 &url_template_filled)) { |
175 callback.Run(base::Optional<std::string>( | 227 callback.Run(base::Optional<std::string>( |
176 "Error: unable to replace placeholders in url template")); | 228 "Error: unable to replace placeholders in url template")); |
177 return; | 229 return; |
178 } | 230 } |
179 | 231 |
180 GURL target_url(url_template_filled); | 232 // The template is relative to the manifest URL (minus the filename). |
181 if (!target_url.is_valid()) { | 233 // Concatenate to make an absolute URL. |
234 base::StringPiece url_base( | |
235 chosen_target.data(), | |
236 chosen_target.size() - GURL(chosen_target).ExtractFileName().size()); | |
237 const GURL target(url_base.as_string() + url_template_filled); | |
238 if (!target.is_valid()) { | |
182 callback.Run(base::Optional<std::string>( | 239 callback.Run(base::Optional<std::string>( |
183 "Error: url of share target is not a valid url.")); | 240 "Error: url of share target is not a valid url.")); |
184 return; | 241 return; |
185 } | 242 } |
186 OpenTargetURL(target_url); | 243 OpenTargetURL(target); |
187 | 244 |
188 callback.Run(base::nullopt); | 245 callback.Run(base::nullopt); |
189 } | 246 } |
OLD | NEW |