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