Chromium Code Reviews| 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 chrome::AddTabAt(GetBrowser(), target_url, |
|
Matt Giuca
2017/02/01 07:30:20
You should still assign this to a Browser* local v
constantina
2017/02/02 00:43:47
Done.
| |
| 132 chrome::AddTabAt(browser, target_url, | 139 GetBrowser()->tab_strip_model()->active_index() + 1, true); |
| 133 browser->tab_strip_model()->active_index() + 1, true); | |
| 134 #endif | 140 #endif |
| 135 } | 141 } |
| 136 | 142 |
| 143 std::string ShareServiceImpl::GetTargetTemplate(std::string target_url) { | |
| 144 const base::DictionaryValue* share_target_dict = | |
| 145 GetPrefService()->GetDictionary(prefs::kWebShareVisitedTargets); | |
| 146 | |
| 147 const base::DictionaryValue* share_target_info_dict = nullptr; | |
| 148 share_target_dict->GetDictionaryWithoutPathExpansion(target_url, | |
| 149 &share_target_info_dict); | |
| 150 | |
| 151 std::string url_template; | |
| 152 share_target_info_dict->GetString("url_template", &url_template); | |
| 153 return url_template; | |
| 154 } | |
| 155 | |
| 156 PrefService* ShareServiceImpl::GetPrefService() { | |
| 157 return GetBrowser()->profile()->GetPrefs(); | |
| 158 } | |
| 159 | |
| 160 blink::mojom::EngagementLevel ShareServiceImpl::GetEngagementLevel(GURL url) { | |
| 161 SiteEngagementService* site_engagement_service = | |
| 162 SiteEngagementService::Get(GetBrowser()->profile()); | |
| 163 return site_engagement_service->GetEngagementLevel(url); | |
| 164 } | |
| 165 | |
| 166 // static | |
| 167 std::vector<std::string> | |
| 168 ShareServiceImpl::GetTargetsWithSufficientEngagement() { | |
| 169 const base::DictionaryValue* dict = | |
| 170 GetPrefService()->GetDictionary(prefs::kWebShareVisitedTargets); | |
| 171 | |
| 172 std::vector<std::string> most_engaged_targets; | |
|
Matt Giuca
2017/02/01 07:30:20
Not really the right name (it implies that you'll
constantina
2017/02/02 00:43:47
Done.
| |
| 173 | |
| 174 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | |
|
Matt Giuca
2017/02/01 07:30:20
auto?
Also, what a strange iterator protocol!
constantina
2017/02/02 00:43:48
Compiler cries.
Ikr!
Matt Giuca
2017/02/02 02:57:10
Oh, right, you can't call auto because you're actu
| |
| 175 GURL manifest_url(it.key()); | |
|
Matt Giuca
2017/02/01 07:30:20
This can probably be a const GURL& yes?
constantina
2017/02/02 00:43:47
Done. Just const, no "&". The key is a string, not
Matt Giuca
2017/02/02 02:57:10
Oh OK, then could just remove the const as well (I
constantina
2017/02/02 05:57:07
Removed
| |
| 176 if (GetEngagementLevel(manifest_url) >= | |
| 177 blink::mojom::EngagementLevel::LOW) { | |
|
Matt Giuca
2017/02/01 07:30:20
Can this be a constant (e.g., kMinimumEngagementLe
constantina
2017/02/02 00:43:47
I moved it to the top of the function.
Matt Giuca
2017/02/02 02:57:10
Acknowledged.
| |
| 178 most_engaged_targets.push_back(it.key()); | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 return most_engaged_targets; | |
| 183 } | |
| 184 | |
| 137 void ShareServiceImpl::Share(const std::string& title, | 185 void ShareServiceImpl::Share(const std::string& title, |
| 138 const std::string& text, | 186 const std::string& text, |
| 139 const GURL& share_url, | 187 const GURL& share_url, |
| 140 const ShareCallback& callback) { | 188 const ShareCallback& callback) { |
| 141 // TODO(constantina): Replace hard-coded name with the registered target list. | 189 std::vector<std::string> most_engaged_targets = |
|
Matt Giuca
2017/02/01 07:30:20
Same name as previous function.
constantina
2017/02/02 00:43:47
Done.
| |
| 142 constexpr char kUrlBase[] = "https://wicg.github.io/web-share-target/demos/"; | 190 GetTargetsWithSufficientEngagement(); |
| 143 std::vector<base::string16> targets{base::ASCIIToUTF16(kUrlBase)}; | 191 |
| 192 std::vector<base::string16> targets; | |
| 193 for (const std::string& target : most_engaged_targets) { | |
| 194 targets.push_back(base::ASCIIToUTF16(target)); | |
|
Matt Giuca
2017/02/01 07:30:20
UTF8ToUTF16
You should never use ASCIIToUTF16 unl
constantina
2017/02/02 00:43:47
Done.
| |
| 195 } | |
| 144 | 196 |
| 145 ShowPickerDialog(targets, base::Bind(&ShareServiceImpl::OnPickerClosed, | 197 ShowPickerDialog(targets, base::Bind(&ShareServiceImpl::OnPickerClosed, |
| 146 base::Unretained(this), title, text, | 198 base::Unretained(this), title, text, |
| 147 share_url, callback)); | 199 share_url, callback)); |
| 148 } | 200 } |
| 149 | 201 |
| 150 void ShareServiceImpl::OnPickerClosed(const std::string& title, | 202 void ShareServiceImpl::OnPickerClosed(const std::string& title, |
| 151 const std::string& text, | 203 const std::string& text, |
| 152 const GURL& share_url, | 204 const GURL& share_url, |
| 153 const ShareCallback& callback, | 205 const ShareCallback& callback, |
| 154 base::Optional<base::string16> result) { | 206 base::Optional<base::string16> result) { |
| 155 if (!result.has_value()) { | 207 if (!result.has_value()) { |
| 156 callback.Run(base::Optional<std::string>("Share was cancelled")); | 208 callback.Run(base::Optional<std::string>("Share was cancelled")); |
| 157 return; | 209 return; |
| 158 } | 210 } |
| 159 | 211 |
| 160 constexpr char kUrlTemplate[] = | 212 std::string chosen_target = base::UTF16ToASCII(result.value()); |
|
Matt Giuca
2017/02/01 07:30:20
UTF16ToUTF8
constantina
2017/02/02 00:43:48
Done.
| |
| 161 "sharetarget.html?title={title}&text={text}&url={url}"; | |
| 162 | 213 |
| 214 std::string url_template = GetTargetTemplate(chosen_target); | |
|
Matt Giuca
2017/02/01 07:30:20
I'm a big fan of this approach versus what we disc
constantina
2017/02/02 00:43:47
:D It has turned out to be quite neat; I like that
| |
| 163 std::string url_template_filled; | 215 std::string url_template_filled; |
| 164 if (!ReplacePlaceholders(kUrlTemplate, title, text, share_url, | 216 if (!ReplacePlaceholders(url_template, title, text, share_url, |
| 165 &url_template_filled)) { | 217 &url_template_filled)) { |
| 166 callback.Run(base::Optional<std::string>( | 218 callback.Run( |
| 167 "Error: unable to replace placeholders in url template")); | 219 base::Optional<std::string>("Error: placeholders in share target's url " |
|
Matt Giuca
2017/02/01 07:30:20
Is there a reason to change this string?
constantina
2017/02/02 00:43:48
No, I just lost it in a rebase, wrote something si
| |
| 220 "template could not be replaced.")); | |
| 168 return; | 221 return; |
| 169 } | 222 } |
| 170 | 223 |
| 171 std::string url_base = base::UTF16ToASCII(result.value()); | 224 base::StringPiece url_base( |
|
Matt Giuca
2017/02/01 07:30:20
// The template is relative to the manifest URL (m
constantina
2017/02/02 00:43:47
Done.
| |
| 172 GURL target_url(url_base + url_template_filled); | 225 chosen_target.data(), |
| 173 if (!target_url.is_valid()) { | 226 chosen_target.size() - GURL(chosen_target).ExtractFileName().size()); |
| 174 callback.Run(base::Optional<std::string>( | 227 const GURL target(url_base.as_string() + url_template_filled); |
| 175 "Error: url of share target is not a valid url.")); | 228 if (!target.is_valid()) { |
| 229 callback.Run( | |
| 230 base::Optional<std::string>("Error: chosen share target's url template " | |
| 231 "does not result in a valid url.")); | |
| 176 return; | 232 return; |
| 177 } | 233 } |
| 178 OpenTargetURL(target_url); | 234 OpenTargetURL(target); |
| 179 | 235 |
| 180 callback.Run(base::nullopt); | 236 callback.Run(base::nullopt); |
| 181 } | 237 } |
| OLD | NEW |