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 "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_commands.h" | 14 #include "chrome/browser/ui/browser_commands.h" |
| 15 #include "chrome/browser/ui/browser_dialogs.h" | |
| 14 #include "chrome/browser/ui/browser_list.h" | 16 #include "chrome/browser/ui/browser_list.h" |
| 15 #include "chrome/browser/ui/browser_tabstrip.h" | 17 #include "chrome/browser/ui/browser_tabstrip.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "mojo/public/cpp/bindings/strong_binding.h" | 19 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 18 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 // Determines whether a character is allowed in a URL template placeholder. | 24 // Determines whether a character is allowed in a URL template placeholder. |
| 23 bool IsIdentifier(char c) { | 25 bool IsIdentifier(char c) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | 119 Browser* browser = BrowserList::GetInstance()->GetLastActive(); |
| 118 chrome::AddTabAt(browser, target_url, | 120 chrome::AddTabAt(browser, target_url, |
| 119 browser->tab_strip_model()->active_index() + 1, true); | 121 browser->tab_strip_model()->active_index() + 1, true); |
| 120 #endif | 122 #endif |
| 121 } | 123 } |
| 122 | 124 |
| 123 void ShareServiceImpl::Share(const std::string& title, | 125 void ShareServiceImpl::Share(const std::string& title, |
| 124 const std::string& text, | 126 const std::string& text, |
| 125 const GURL& share_url, | 127 const GURL& share_url, |
| 126 const ShareCallback& callback) { | 128 const ShareCallback& callback) { |
| 129 // TODO(constantina): Replace hard-coded name with the registered target list. | |
|
constantina
2017/01/20 05:23:44
👍
| |
| 130 constexpr wchar_t kTargetName[] = L"Web Share Target Test App"; | |
| 131 std::vector<base::string16> targets{base::WideToUTF16(kTargetName)}; | |
| 132 | |
| 133 // TODO(mgiuca): Get the browser window as |parent_window|. | |
| 134 gfx::NativeWindow browser_window = nullptr; | |
| 135 chrome::ShowWebShareTargetPickerDialog( | |
| 136 browser_window, targets, | |
| 137 base::Bind(&ShareServiceImpl::OnPickerClosed, base::Unretained(this), | |
| 138 title, text, share_url, callback)); | |
| 139 } | |
| 140 | |
| 141 void ShareServiceImpl::OnPickerClosed(const std::string& title, | |
| 142 const std::string& text, | |
| 143 const GURL& share_url, | |
| 144 const ShareCallback& callback, | |
| 145 bool picked) { | |
| 146 if (!picked) { | |
|
constantina
2017/01/20 05:23:44
👍
| |
| 147 callback.Run(base::Optional<std::string>("Share was cancelled")); | |
| 148 return; | |
| 149 } | |
| 150 | |
| 127 // TODO(constantina): replace hard-coded URL with one from user-chosen site. | 151 // TODO(constantina): replace hard-coded URL with one from user-chosen site. |
| 128 constexpr char kUrlBase[] = "https://wicg.github.io/web-share-target/"; | 152 constexpr char kUrlBase[] = "https://wicg.github.io/web-share-target/"; |
| 129 constexpr char kUrlTemplate[] = | 153 constexpr char kUrlTemplate[] = |
| 130 "demos/sharetarget.html?title={title}&text={text}&url={url}"; | 154 "demos/sharetarget.html?title={title}&text={text}&url={url}"; |
| 131 | 155 |
| 132 std::string url_template_filled; | 156 std::string url_template_filled; |
| 133 if (!ReplacePlaceholders(kUrlTemplate, title, text, share_url, | 157 if (!ReplacePlaceholders(kUrlTemplate, title, text, share_url, |
| 134 &url_template_filled)) { | 158 &url_template_filled)) { |
| 135 callback.Run(base::Optional<std::string>( | 159 callback.Run(base::Optional<std::string>( |
| 136 "Error: unable to replace placeholders in url template")); | 160 "Error: unable to replace placeholders in url template")); |
| 137 return; | 161 return; |
| 138 } | 162 } |
| 139 | 163 |
| 140 GURL target_url(kUrlBase + url_template_filled); | 164 GURL target_url(kUrlBase + url_template_filled); |
| 141 if (!target_url.is_valid()) { | 165 if (!target_url.is_valid()) { |
| 142 callback.Run(base::Optional<std::string>( | 166 callback.Run(base::Optional<std::string>( |
| 143 "Error: url of share target is not a valid url.")); | 167 "Error: url of share target is not a valid url.")); |
| 144 return; | 168 return; |
| 145 } | 169 } |
| 146 OpenTargetURL(target_url); | 170 OpenTargetURL(target_url); |
| 147 | 171 |
| 148 callback.Run(base::nullopt); | 172 callback.Run(base::nullopt); |
| 149 } | 173 } |
| OLD | NEW |