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