constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_WEBSHARE_SHARE_SERVICE_IMPL_H_ |
| 6 | #define CHROME_BROWSER_WEBSHARE_SHARE_SERVICE_IMPL_H_ |
| 7 | |
constantina | 5558f3f3 | 2017-02-13 05:37:54 | [diff] [blame] | 8 | #include <memory> |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 9 | #include <string> |
constantina | 5558f3f3 | 2017-02-13 05:37:54 | [diff] [blame] | 10 | #include <vector> |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 11 | |
constantina | 2cfa55e | 2017-01-13 06:36:55 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
mgiuca | bd4b24d | 2017-02-17 01:40:57 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
mgiuca | 39e4067 | 2017-01-31 04:16:49 | [diff] [blame] | 14 | #include "base/strings/string16.h" |
constantina | 5558f3f3 | 2017-02-13 05:37:54 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser.h" |
| 16 | #include "chrome/browser/ui/browser_dialogs.h" |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 17 | #include "mojo/public/cpp/bindings/interface_request.h" |
ben | a5c972c | 2017-05-04 01:38:43 | [diff] [blame] | 18 | #include "services/service_manager/public/cpp/bind_source_info.h" |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 19 | #include "third_party/WebKit/public/platform/modules/webshare/webshare.mojom.h" |
constantina | 5558f3f3 | 2017-02-13 05:37:54 | [diff] [blame] | 20 | #include "third_party/WebKit/public/platform/site_engagement.mojom.h" |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 21 | |
constantina | 5558f3f3 | 2017-02-13 05:37:54 | [diff] [blame] | 22 | class DictionaryValue; |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 23 | class GURL; |
| 24 | |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 25 | // Desktop implementation of the ShareService Mojo service. |
| 26 | class ShareServiceImpl : public blink::mojom::ShareService { |
| 27 | public: |
constantina | 5558f3f3 | 2017-02-13 05:37:54 | [diff] [blame] | 28 | ShareServiceImpl(); |
| 29 | ~ShareServiceImpl() override; |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 30 | |
ben | a5c972c | 2017-05-04 01:38:43 | [diff] [blame] | 31 | static void Create(const service_manager::BindSourceInfo& source_info, |
| 32 | mojo::InterfaceRequest<ShareService> request); |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 33 | |
constantina | 2cfa55e | 2017-01-13 06:36:55 | [diff] [blame] | 34 | // blink::mojom::ShareService overrides: |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 35 | void Share(const std::string& title, |
| 36 | const std::string& text, |
constantina | 2cfa55e | 2017-01-13 06:36:55 | [diff] [blame] | 37 | const GURL& share_url, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame^] | 38 | ShareCallback callback) override; |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 39 | |
| 40 | private: |
constantina | 2cfa55e | 2017-01-13 06:36:55 | [diff] [blame] | 41 | FRIEND_TEST_ALL_PREFIXES(ShareServiceImplUnittest, ReplacePlaceholders); |
| 42 | |
constantina | 5558f3f3 | 2017-02-13 05:37:54 | [diff] [blame] | 43 | Browser* GetBrowser(); |
| 44 | |
| 45 | // Returns the URL template of the target identified by |target_url| |
mgiuca | f7077234 | 2017-02-14 01:45:42 | [diff] [blame] | 46 | std::string GetTargetTemplate(const std::string& target_url, |
| 47 | const base::DictionaryValue& share_targets); |
constantina | 5558f3f3 | 2017-02-13 05:37:54 | [diff] [blame] | 48 | |
| 49 | // Virtual for testing purposes. |
| 50 | virtual PrefService* GetPrefService(); |
| 51 | |
| 52 | // Returns the site engagement level of the site, |url|, with the user. |
| 53 | // Virtual for testing purposes. |
| 54 | virtual blink::mojom::EngagementLevel GetEngagementLevel(const GURL& url); |
| 55 | |
mgiuca | 39e4067 | 2017-01-31 04:16:49 | [diff] [blame] | 56 | // Shows the share picker dialog with |targets| as the list of applications |
constantina | e4c513e7 | 2017-02-07 02:14:10 | [diff] [blame] | 57 | // presented to the user. Passes the result to |callback|. If the user picks a |
| 58 | // target, the result passed to |callback| is the manifest URL of the chosen |
| 59 | // target, or is null if the user cancelled the share. Virtual for testing. |
mgiuca | 39e4067 | 2017-01-31 04:16:49 | [diff] [blame] | 60 | virtual void ShowPickerDialog( |
constantina | e4c513e7 | 2017-02-07 02:14:10 | [diff] [blame] | 61 | const std::vector<std::pair<base::string16, GURL>>& targets, |
ortuno | 928d1410 | 2017-05-02 00:09:05 | [diff] [blame] | 62 | chrome::WebShareTargetPickerCallback callback); |
mgiuca | 39e4067 | 2017-01-31 04:16:49 | [diff] [blame] | 63 | |
constantina | 2cfa55e | 2017-01-13 06:36:55 | [diff] [blame] | 64 | // Opens a new tab and navigates to |target_url|. |
| 65 | // Virtual for testing purposes. |
| 66 | virtual void OpenTargetURL(const GURL& target_url); |
| 67 | |
constantina | 5558f3f3 | 2017-02-13 05:37:54 | [diff] [blame] | 68 | // Returns all stored Share Targets that have a high enough engagement score |
| 69 | // with the user. |
| 70 | std::vector<std::pair<base::string16, GURL>> |
| 71 | GetTargetsWithSufficientEngagement( |
| 72 | const base::DictionaryValue& share_targets); |
| 73 | |
constantina | 2cfa55e | 2017-01-13 06:36:55 | [diff] [blame] | 74 | // Writes to |url_template_filled|, a copy of |url_template| with all |
| 75 | // instances of "{title}", "{text}", and "{url}" replaced with |
| 76 | // |title|, |text|, and |url| respectively. |
| 77 | // Replaces instances of "{X}" where "X" is any string besides "title", |
| 78 | // "text", and "url", with an empty string, for forwards compatibility. |
| 79 | // Returns false, if there are badly nested placeholders. |
| 80 | // This includes any case in which two "{" occur before a "}", or a "}" |
| 81 | // occurs with no preceding "{". |
| 82 | static bool ReplacePlaceholders(base::StringPiece url_template, |
| 83 | base::StringPiece title, |
| 84 | base::StringPiece text, |
| 85 | const GURL& share_url, |
| 86 | std::string* url_template_filled); |
| 87 | |
constantina | 5558f3f3 | 2017-02-13 05:37:54 | [diff] [blame] | 88 | void OnPickerClosed(std::unique_ptr<base::DictionaryValue> share_targets, |
| 89 | const std::string& title, |
mgiuca | 39e4067 | 2017-01-31 04:16:49 | [diff] [blame] | 90 | const std::string& text, |
| 91 | const GURL& share_url, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame^] | 92 | ShareCallback callback, |
ortuno | 928d1410 | 2017-05-02 00:09:05 | [diff] [blame] | 93 | const base::Optional<std::string>& result); |
mgiuca | 39e4067 | 2017-01-31 04:16:49 | [diff] [blame] | 94 | |
mgiuca | bd4b24d | 2017-02-17 01:40:57 | [diff] [blame] | 95 | base::WeakPtrFactory<ShareServiceImpl> weak_factory_; |
| 96 | |
constantina | c8b2173b | 2016-12-15 05:55:51 | [diff] [blame] | 97 | DISALLOW_COPY_AND_ASSIGN(ShareServiceImpl); |
| 98 | }; |
| 99 | |
| 100 | #endif // CHROME_BROWSER_WEBSHARE_SHARE_SERVICE_IMPL_H_ |