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 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 const base::Callback<void(base::Optional<base::string16>)>& callback) { | 121 const base::Callback<void(base::Optional<base::string16>)>& callback) { |
119 // TODO(mgiuca): Get the browser window as |parent_window|. | 122 // TODO(mgiuca): Get the browser window as |parent_window|. |
120 #if defined(OS_LINUX) || defined(OS_WIN) | 123 #if defined(OS_LINUX) || defined(OS_WIN) |
121 chrome::ShowWebShareTargetPickerDialog(nullptr /* parent_window */, targets, | 124 chrome::ShowWebShareTargetPickerDialog(nullptr /* parent_window */, targets, |
122 callback); | 125 callback); |
123 #else | 126 #else |
124 callback.Run(base::nullopt); | 127 callback.Run(base::nullopt); |
125 #endif | 128 #endif |
126 } | 129 } |
127 | 130 |
131 Browser* ShareServiceImpl::GetBrowser() { | |
132 return BrowserList::GetInstance()->GetLastActive(); | |
133 } | |
134 | |
128 void ShareServiceImpl::OpenTargetURL(const GURL& target_url) { | 135 void ShareServiceImpl::OpenTargetURL(const GURL& target_url) { |
129 // TODO(constantina): Prevent this code from being run/compiled in android. | 136 // TODO(constantina): Prevent this code from being run/compiled in android. |
130 #if defined(OS_LINUX) || defined(OS_WIN) | 137 #if defined(OS_LINUX) || defined(OS_WIN) |
131 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | 138 Browser* browser = GetBrowser(); |
132 chrome::AddTabAt(browser, target_url, | 139 chrome::AddTabAt(browser, target_url, |
133 browser->tab_strip_model()->active_index() + 1, true); | 140 browser->tab_strip_model()->active_index() + 1, true); |
134 #endif | 141 #endif |
135 } | 142 } |
136 | 143 |
144 std::string ShareServiceImpl::GetTargetTemplate(const std::string& target_url) { | |
145 const base::DictionaryValue* share_target_dict = | |
146 GetPrefService()->GetDictionary(prefs::kWebShareVisitedTargets); | |
147 | |
148 const base::DictionaryValue* share_target_info_dict = nullptr; | |
149 share_target_dict->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::string> | |
170 ShareServiceImpl::GetTargetsWithSufficientEngagement() { | |
171 const blink::mojom::EngagementLevel kMinimumEngagementLevel = | |
172 blink::mojom::EngagementLevel::LOW; | |
173 | |
174 const base::DictionaryValue* dict = | |
175 GetPrefService()->GetDictionary(prefs::kWebShareVisitedTargets); | |
176 | |
177 std::vector<std::string> sufficiently_engaged_targets; | |
178 | |
179 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | |
180 GURL manifest_url(it.key()); | |
181 if (GetEngagementLevel(manifest_url) >= kMinimumEngagementLevel) { | |
182 sufficiently_engaged_targets.push_back(it.key()); | |
183 } | |
184 } | |
185 | |
186 return sufficiently_engaged_targets; | |
187 } | |
188 | |
137 void ShareServiceImpl::Share(const std::string& title, | 189 void ShareServiceImpl::Share(const std::string& title, |
138 const std::string& text, | 190 const std::string& text, |
139 const GURL& share_url, | 191 const GURL& share_url, |
140 const ShareCallback& callback) { | 192 const ShareCallback& callback) { |
141 // TODO(constantina): Replace hard-coded manifest URL with the list of | 193 std::vector<std::string> sufficiently_engaged_targets = |
142 // registered targets' manifest URLs. | 194 GetTargetsWithSufficientEngagement(); |
143 constexpr char kManifestURL[] = | 195 |
144 "https://wicg.github.io/web-share-target/demos/manifest.json"; | 196 std::vector<base::string16> targets; |
145 // TODO(constantina): Pass vector of pairs of target names and manifest URLs | 197 for (const std::string& target : sufficiently_engaged_targets) { |
Matt Giuca
2017/02/03 02:40:23
Can be const auto&
constantina
2017/02/08 23:25:16
Gone.
| |
146 // to picker. | 198 targets.push_back(base::UTF8ToUTF16(target)); |
147 std::vector<base::string16> targets{base::ASCIIToUTF16(kManifestURL)}; | 199 } |
148 | 200 |
149 ShowPickerDialog(targets, base::Bind(&ShareServiceImpl::OnPickerClosed, | 201 ShowPickerDialog(targets, base::Bind(&ShareServiceImpl::OnPickerClosed, |
150 base::Unretained(this), title, text, | 202 base::Unretained(this), title, text, |
151 share_url, callback)); | 203 share_url, callback)); |
152 } | 204 } |
153 | 205 |
154 void ShareServiceImpl::OnPickerClosed(const std::string& title, | 206 void ShareServiceImpl::OnPickerClosed(const std::string& title, |
155 const std::string& text, | 207 const std::string& text, |
156 const GURL& share_url, | 208 const GURL& share_url, |
157 const ShareCallback& callback, | 209 const ShareCallback& callback, |
158 base::Optional<base::string16> result) { | 210 base::Optional<base::string16> result) { |
159 if (!result.has_value()) { | 211 if (!result.has_value()) { |
160 callback.Run(base::Optional<std::string>("Share was cancelled")); | 212 callback.Run(base::Optional<std::string>("Share was cancelled")); |
161 return; | 213 return; |
162 } | 214 } |
163 | 215 |
164 // TODO(constantina): use manifest URL in result to look up corresponding URL | 216 std::string chosen_target = base::UTF16ToUTF8(result.value()); |
Matt Giuca
2017/02/03 02:40:23
As discussed, conversion from UTF-8 to UTF-16 (in
constantina
2017/02/08 23:25:16
Gone.
| |
165 // template. | |
166 constexpr char kUrlTemplate[] = | |
167 "https://wicg.github.io/web-share-target/demos/" | |
168 "sharetarget.html?title={title}&text={text}&url={url}"; | |
169 | 217 |
218 std::string url_template = GetTargetTemplate(chosen_target); | |
170 std::string url_template_filled; | 219 std::string url_template_filled; |
171 if (!ReplacePlaceholders(kUrlTemplate, title, text, share_url, | 220 if (!ReplacePlaceholders(url_template, title, text, share_url, |
172 &url_template_filled)) { | 221 &url_template_filled)) { |
173 callback.Run(base::Optional<std::string>( | 222 callback.Run(base::Optional<std::string>( |
174 "Error: unable to replace placeholders in url template")); | 223 "Error: unable to replace placeholders in url template")); |
175 return; | 224 return; |
176 } | 225 } |
177 | 226 |
178 GURL target_url(url_template_filled); | 227 // The template is relative to the manifest URL (minus the filename). |
179 if (!target_url.is_valid()) { | 228 // Concatenate to make an absolute URL. |
229 base::StringPiece url_base( | |
230 chosen_target.data(), | |
231 chosen_target.size() - GURL(chosen_target).ExtractFileName().size()); | |
232 const GURL target(url_base.as_string() + url_template_filled); | |
233 if (!target.is_valid()) { | |
180 callback.Run(base::Optional<std::string>( | 234 callback.Run(base::Optional<std::string>( |
181 "Error: url of share target is not a valid url.")); | 235 "Error: url of share target is not a valid url.")); |
182 return; | 236 return; |
183 } | 237 } |
184 OpenTargetURL(target_url); | 238 OpenTargetURL(target); |
185 | 239 |
186 callback.Run(base::nullopt); | 240 callback.Run(base::nullopt); |
187 } | 241 } |
OLD | NEW |