blob: b9b2453908ffd7595b6f6e1a67ef165d4d79f38c [file] [log] [blame]
constantinac8b2173b2016-12-15 05:55:511// 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
constantina5558f3f32017-02-13 05:37:548#include <memory>
constantinac8b2173b2016-12-15 05:55:519#include <string>
constantina5558f3f32017-02-13 05:37:5410#include <vector>
constantinac8b2173b2016-12-15 05:55:5111
constantina2cfa55e2017-01-13 06:36:5512#include "base/gtest_prod_util.h"
mgiucabd4b24d2017-02-17 01:40:5713#include "base/memory/weak_ptr.h"
constantina5558f3f32017-02-13 05:37:5414#include "chrome/browser/ui/browser.h"
15#include "chrome/browser/ui/browser_dialogs.h"
constantinac8b2173b2016-12-15 05:55:5116#include "mojo/public/cpp/bindings/interface_request.h"
constantinac8b2173b2016-12-15 05:55:5117#include "third_party/WebKit/public/platform/modules/webshare/webshare.mojom.h"
constantina5558f3f32017-02-13 05:37:5418#include "third_party/WebKit/public/platform/site_engagement.mojom.h"
constantinac8b2173b2016-12-15 05:55:5119
constantina5558f3f32017-02-13 05:37:5420class DictionaryValue;
constantinac8b2173b2016-12-15 05:55:5121class GURL;
Giovanni Ortuño Urquidi97f8bd522017-06-16 04:04:3722class WebShareTarget;
constantinac8b2173b2016-12-15 05:55:5123
constantinac8b2173b2016-12-15 05:55:5124// Desktop implementation of the ShareService Mojo service.
25class ShareServiceImpl : public blink::mojom::ShareService {
26 public:
constantina5558f3f32017-02-13 05:37:5427 ShareServiceImpl();
28 ~ShareServiceImpl() override;
constantinac8b2173b2016-12-15 05:55:5129
Ben Goodger21ada1e2017-07-19 14:53:0130 static void Create(mojo::InterfaceRequest<ShareService> request);
constantinac8b2173b2016-12-15 05:55:5131
constantina2cfa55e2017-01-13 06:36:5532 // blink::mojom::ShareService overrides:
constantinac8b2173b2016-12-15 05:55:5133 void Share(const std::string& title,
34 const std::string& text,
constantina2cfa55e2017-01-13 06:36:5535 const GURL& share_url,
tzikcf7bcd652017-06-15 04:19:3036 ShareCallback callback) override;
constantinac8b2173b2016-12-15 05:55:5137
38 private:
constantina2cfa55e2017-01-13 06:36:5539 FRIEND_TEST_ALL_PREFIXES(ShareServiceImplUnittest, ReplacePlaceholders);
40
constantina5558f3f32017-02-13 05:37:5441 Browser* GetBrowser();
42
43 // Returns the URL template of the target identified by |target_url|
mgiucaf70772342017-02-14 01:45:4244 std::string GetTargetTemplate(const std::string& target_url,
45 const base::DictionaryValue& share_targets);
constantina5558f3f32017-02-13 05:37:5446
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
mgiuca39e40672017-01-31 04:16:4954 // Shows the share picker dialog with |targets| as the list of applications
constantinae4c513e72017-02-07 02:14:1055 // 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 Urquidi97f8bd522017-06-16 04:04:3758 virtual void ShowPickerDialog(std::vector<WebShareTarget> targets,
59 chrome::WebShareTargetPickerCallback callback);
mgiuca39e40672017-01-31 04:16:4960
constantina2cfa55e2017-01-13 06:36:5561 // Opens a new tab and navigates to |target_url|.
62 // Virtual for testing purposes.
63 virtual void OpenTargetURL(const GURL& target_url);
64
constantina5558f3f32017-02-13 05:37:5465 // Returns all stored Share Targets that have a high enough engagement score
66 // with the user.
Giovanni Ortuño Urquidi97f8bd522017-06-16 04:04:3767 std::vector<WebShareTarget> GetTargetsWithSufficientEngagement();
constantina5558f3f32017-02-13 05:37:5468
constantina2cfa55e2017-01-13 06:36:5569 // 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 Urquidi97f8bd522017-06-16 04:04:3783 void OnPickerClosed(const std::string& title,
mgiuca39e40672017-01-31 04:16:4984 const std::string& text,
85 const GURL& share_url,
tzikcf7bcd652017-06-15 04:19:3086 ShareCallback callback,
Giovanni Ortuño Urquidi97f8bd522017-06-16 04:04:3787 const WebShareTarget* result);
mgiuca39e40672017-01-31 04:16:4988
mgiucabd4b24d2017-02-17 01:40:5789 base::WeakPtrFactory<ShareServiceImpl> weak_factory_;
90
constantinac8b2173b2016-12-15 05:55:5191 DISALLOW_COPY_AND_ASSIGN(ShareServiceImpl);
92};
93
94#endif // CHROME_BROWSER_WEBSHARE_SHARE_SERVICE_IMPL_H_