| 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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 // Error: Saw open that was never closed. | 106 // Error: Saw open that was never closed. |
| 105 return false; | 107 return false; |
| 106 } | 108 } |
| 107 split_template.push_back(url_template.substr( | 109 split_template.push_back(url_template.substr( |
| 108 start_index_to_copy, url_template.size() - start_index_to_copy)); | 110 start_index_to_copy, url_template.size() - start_index_to_copy)); |
| 109 | 111 |
| 110 *url_template_filled = JoinString(split_template); | 112 *url_template_filled = JoinString(split_template); |
| 111 return true; | 113 return true; |
| 112 } | 114 } |
| 113 | 115 |
| 116 void ShareServiceImpl::ShowPickerDialog( |
| 117 const std::vector<base::string16>& targets, |
| 118 const base::Callback<void(SharePickerResult)>& callback) { |
| 119 // TODO(mgiuca): Get the browser window as |parent_window|. |
| 120 #if defined(OS_LINUX) || defined(OS_WIN) |
| 121 chrome::ShowWebShareTargetPickerDialog(nullptr /* parent_window */, targets, |
| 122 callback); |
| 123 #else |
| 124 callback.Run(SharePickerResult::CANCEL); |
| 125 #endif |
| 126 } |
| 127 |
| 114 void ShareServiceImpl::OpenTargetURL(const GURL& target_url) { | 128 void ShareServiceImpl::OpenTargetURL(const GURL& target_url) { |
| 115 // TODO(constantina): Prevent this code from being run/compiled in android. | 129 // TODO(constantina): Prevent this code from being run/compiled in android. |
| 116 #if defined(OS_LINUX) || defined(OS_WIN) | 130 #if defined(OS_LINUX) || defined(OS_WIN) |
| 117 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | 131 Browser* browser = BrowserList::GetInstance()->GetLastActive(); |
| 118 chrome::AddTabAt(browser, target_url, | 132 chrome::AddTabAt(browser, target_url, |
| 119 browser->tab_strip_model()->active_index() + 1, true); | 133 browser->tab_strip_model()->active_index() + 1, true); |
| 120 #endif | 134 #endif |
| 121 } | 135 } |
| 122 | 136 |
| 123 void ShareServiceImpl::Share(const std::string& title, | 137 void ShareServiceImpl::Share(const std::string& title, |
| 124 const std::string& text, | 138 const std::string& text, |
| 125 const GURL& share_url, | 139 const GURL& share_url, |
| 126 const ShareCallback& callback) { | 140 const ShareCallback& callback) { |
| 141 // TODO(constantina): Replace hard-coded name with the registered target list. |
| 142 constexpr char kTargetName[] = "Web Share Target Test App"; |
| 143 std::vector<base::string16> targets{base::ASCIIToUTF16(kTargetName)}; |
| 144 |
| 145 ShowPickerDialog(targets, base::Bind(&ShareServiceImpl::OnPickerClosed, |
| 146 base::Unretained(this), title, text, |
| 147 share_url, callback)); |
| 148 } |
| 149 |
| 150 void ShareServiceImpl::OnPickerClosed(const std::string& title, |
| 151 const std::string& text, |
| 152 const GURL& share_url, |
| 153 const ShareCallback& callback, |
| 154 SharePickerResult result) { |
| 155 if (result == SharePickerResult::CANCEL) { |
| 156 callback.Run(base::Optional<std::string>("Share was cancelled")); |
| 157 return; |
| 158 } |
| 159 |
| 127 // TODO(constantina): replace hard-coded URL with one from user-chosen site. | 160 // TODO(constantina): replace hard-coded URL with one from user-chosen site. |
| 128 constexpr char kUrlBase[] = "https://wicg.github.io/web-share-target/"; | 161 constexpr char kUrlBase[] = "https://wicg.github.io/web-share-target/"; |
| 129 constexpr char kUrlTemplate[] = | 162 constexpr char kUrlTemplate[] = |
| 130 "demos/sharetarget.html?title={title}&text={text}&url={url}"; | 163 "demos/sharetarget.html?title={title}&text={text}&url={url}"; |
| 131 | 164 |
| 132 std::string url_template_filled; | 165 std::string url_template_filled; |
| 133 if (!ReplacePlaceholders(kUrlTemplate, title, text, share_url, | 166 if (!ReplacePlaceholders(kUrlTemplate, title, text, share_url, |
| 134 &url_template_filled)) { | 167 &url_template_filled)) { |
| 135 callback.Run(base::Optional<std::string>( | 168 callback.Run(base::Optional<std::string>( |
| 136 "Error: unable to replace placeholders in url template")); | 169 "Error: unable to replace placeholders in url template")); |
| 137 return; | 170 return; |
| 138 } | 171 } |
| 139 | 172 |
| 140 GURL target_url(kUrlBase + url_template_filled); | 173 GURL target_url(kUrlBase + url_template_filled); |
| 141 if (!target_url.is_valid()) { | 174 if (!target_url.is_valid()) { |
| 142 callback.Run(base::Optional<std::string>( | 175 callback.Run(base::Optional<std::string>( |
| 143 "Error: url of share target is not a valid url.")); | 176 "Error: url of share target is not a valid url.")); |
| 144 return; | 177 return; |
| 145 } | 178 } |
| 146 OpenTargetURL(target_url); | 179 OpenTargetURL(target_url); |
| 147 | 180 |
| 148 callback.Run(base::nullopt); | 181 callback.Run(base::nullopt); |
| 149 } | 182 } |
| OLD | NEW |