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