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 |
| 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( | |
| 148 const std::string& target_url, | |
| 149 const base::DictionaryValue& share_targets) { | |
| 150 const base::DictionaryValue* share_target_info_dict = nullptr; | |
| 151 share_targets.GetDictionaryWithoutPathExpansion(target_url, | |
| 152 &share_target_info_dict); | |
|
Matt Giuca
2017/02/10 02:16:50
nit: Remove one space from this line (clang format
constantina
2017/02/10 02:28:40
Done!
| |
| 153 | |
| 154 std::string url_template; | |
| 155 share_target_info_dict->GetString("url_template", &url_template); | |
| 156 return url_template; | |
| 157 } | |
| 158 | |
| 159 PrefService* ShareServiceImpl::GetPrefService() { | |
| 160 return GetBrowser()->profile()->GetPrefs(); | |
| 161 } | |
| 162 | |
| 163 blink::mojom::EngagementLevel ShareServiceImpl::GetEngagementLevel( | |
| 164 const GURL& url) { | |
| 165 SiteEngagementService* site_engagement_service = | |
| 166 SiteEngagementService::Get(GetBrowser()->profile()); | |
| 167 return site_engagement_service->GetEngagementLevel(url); | |
| 168 } | |
| 169 | |
| 170 // static | |
| 171 std::vector<std::pair<base::string16, GURL>> | |
| 172 ShareServiceImpl::GetTargetsWithSufficientEngagement( | |
| 173 const base::DictionaryValue& share_targets) { | |
| 174 constexpr blink::mojom::EngagementLevel kMinimumEngagementLevel = | |
| 175 blink::mojom::EngagementLevel::LOW; | |
| 176 | |
| 177 std::vector<std::pair<base::string16, GURL>> sufficiently_engaged_targets; | |
| 178 | |
| 179 for (base::DictionaryValue::Iterator it(share_targets); !it.IsAtEnd(); | |
| 180 it.Advance()) { | |
| 181 GURL manifest_url(it.key()); | |
| 182 if (GetEngagementLevel(manifest_url) >= kMinimumEngagementLevel) { | |
| 183 const base::DictionaryValue* share_target_dict; | |
| 184 bool result = it.value().GetAsDictionary(&share_target_dict); | |
| 185 DCHECK(result); | |
| 186 | |
| 187 std::string name; | |
| 188 share_target_dict->GetString("name", &name); | |
| 189 | |
| 190 sufficiently_engaged_targets.push_back( | |
| 191 make_pair(base::UTF8ToUTF16(name), manifest_url)); | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 return sufficiently_engaged_targets; | |
| 196 } | |
| 197 | |
| 137 void ShareServiceImpl::Share(const std::string& title, | 198 void ShareServiceImpl::Share(const std::string& title, |
| 138 const std::string& text, | 199 const std::string& text, |
| 139 const GURL& share_url, | 200 const GURL& share_url, |
| 140 const ShareCallback& callback) { | 201 const ShareCallback& callback) { |
| 141 // TODO(constantina): Replace hard-coded name and manifest URL with the list | 202 std::unique_ptr<base::DictionaryValue> share_targets = |
| 142 // of registered targets' manifest URLs. | 203 GetPrefService() |
| 143 constexpr char kTargetName[] = "Web Share Target Test App"; | 204 ->GetDictionary(prefs::kWebShareVisitedTargets) |
| 144 constexpr char kManifestURL[] = | 205 ->CreateDeepCopy(); |
| 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 | 206 |
| 151 ShowPickerDialog(targets, base::Bind(&ShareServiceImpl::OnPickerClosed, | 207 std::vector<std::pair<base::string16, GURL>> sufficiently_engaged_targets = |
| 152 base::Unretained(this), title, text, | 208 GetTargetsWithSufficientEngagement(*share_targets); |
| 153 share_url, callback)); | 209 |
| 210 ShowPickerDialog( | |
| 211 sufficiently_engaged_targets, | |
| 212 base::Bind(&ShareServiceImpl::OnPickerClosed, base::Unretained(this), | |
| 213 base::Passed(&share_targets), title, text, share_url, | |
| 214 callback)); | |
| 154 } | 215 } |
| 155 | 216 |
| 156 void ShareServiceImpl::OnPickerClosed(const std::string& title, | 217 void ShareServiceImpl::OnPickerClosed( |
| 157 const std::string& text, | 218 std::unique_ptr<base::DictionaryValue> share_targets, |
| 158 const GURL& share_url, | 219 const std::string& title, |
| 159 const ShareCallback& callback, | 220 const std::string& text, |
| 160 base::Optional<std::string> result) { | 221 const GURL& share_url, |
| 222 const ShareCallback& callback, | |
| 223 base::Optional<std::string> result) { | |
| 161 if (!result.has_value()) { | 224 if (!result.has_value()) { |
| 162 callback.Run(base::Optional<std::string>("Share was cancelled")); | 225 callback.Run(base::Optional<std::string>("Share was cancelled")); |
| 163 return; | 226 return; |
| 164 } | 227 } |
| 165 | 228 |
| 166 // TODO(constantina): use manifest URL in result to look up corresponding URL | 229 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 | 230 |
| 231 std::string url_template = | |
| 232 GetTargetTemplate(chosen_target, *share_targets); | |
| 172 std::string url_template_filled; | 233 std::string url_template_filled; |
| 173 if (!ReplacePlaceholders(kUrlTemplate, title, text, share_url, | 234 if (!ReplacePlaceholders(url_template, title, text, share_url, |
| 174 &url_template_filled)) { | 235 &url_template_filled)) { |
| 175 callback.Run(base::Optional<std::string>( | 236 callback.Run(base::Optional<std::string>( |
| 176 "Error: unable to replace placeholders in url template")); | 237 "Error: unable to replace placeholders in url template")); |
| 177 return; | 238 return; |
| 178 } | 239 } |
| 179 | 240 |
| 180 GURL target_url(url_template_filled); | 241 // The template is relative to the manifest URL (minus the filename). |
| 181 if (!target_url.is_valid()) { | 242 // Concatenate to make an absolute URL. |
| 243 base::StringPiece url_base( | |
| 244 chosen_target.data(), | |
| 245 chosen_target.size() - GURL(chosen_target).ExtractFileName().size()); | |
| 246 const GURL target(url_base.as_string() + url_template_filled); | |
| 247 if (!target.is_valid()) { | |
| 182 callback.Run(base::Optional<std::string>( | 248 callback.Run(base::Optional<std::string>( |
| 183 "Error: url of share target is not a valid url.")); | 249 "Error: url of share target is not a valid url.")); |
| 184 return; | 250 return; |
| 185 } | 251 } |
| 186 OpenTargetURL(target_url); | 252 OpenTargetURL(target); |
| 187 | 253 |
| 188 callback.Run(base::nullopt); | 254 callback.Run(base::nullopt); |
| 189 } | 255 } |
| OLD | NEW |