blob: ed1cde8113f86719a26aefc1cd29e1dd36056790 [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"
mgiuca39e40672017-01-31 04:16:4913#include "base/strings/string16.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"
17#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;
22
constantinac8b2173b2016-12-15 05:55:5123// Desktop implementation of the ShareService Mojo service.
24class ShareServiceImpl : public blink::mojom::ShareService {
25 public:
constantina5558f3f32017-02-13 05:37:5426 ShareServiceImpl();
27 ~ShareServiceImpl() override;
constantinac8b2173b2016-12-15 05:55:5128
29 static void Create(mojo::InterfaceRequest<ShareService> request);
30
constantina2cfa55e2017-01-13 06:36:5531 // blink::mojom::ShareService overrides:
constantinac8b2173b2016-12-15 05:55:5132 void Share(const std::string& title,
33 const std::string& text,
constantina2cfa55e2017-01-13 06:36:5534 const GURL& share_url,
constantinac8b2173b2016-12-15 05:55:5135 const ShareCallback& callback) override;
36
37 private:
constantina2cfa55e2017-01-13 06:36:5538 FRIEND_TEST_ALL_PREFIXES(ShareServiceImplUnittest, ReplacePlaceholders);
39
constantina5558f3f32017-02-13 05:37:5440 Browser* GetBrowser();
41
42 // Returns the URL template of the target identified by |target_url|
mgiucaf70772342017-02-14 01:45:4243 std::string GetTargetTemplate(const std::string& target_url,
44 const base::DictionaryValue& share_targets);
constantina5558f3f32017-02-13 05:37:5445
46 // Virtual for testing purposes.
47 virtual PrefService* GetPrefService();
48
49 // Returns the site engagement level of the site, |url|, with the user.
50 // Virtual for testing purposes.
51 virtual blink::mojom::EngagementLevel GetEngagementLevel(const GURL& url);
52
mgiuca39e40672017-01-31 04:16:4953 // Shows the share picker dialog with |targets| as the list of applications
constantinae4c513e72017-02-07 02:14:1054 // presented to the user. Passes the result to |callback|. If the user picks a
55 // target, the result passed to |callback| is the manifest URL of the chosen
56 // target, or is null if the user cancelled the share. Virtual for testing.
mgiuca39e40672017-01-31 04:16:4957 virtual void ShowPickerDialog(
constantinae4c513e72017-02-07 02:14:1058 const std::vector<std::pair<base::string16, GURL>>& targets,
59 const base::Callback<void(base::Optional<std::string>)>& 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.
67 std::vector<std::pair<base::string16, GURL>>
68 GetTargetsWithSufficientEngagement(
69 const base::DictionaryValue& share_targets);
70
constantina2cfa55e2017-01-13 06:36:5571 // Writes to |url_template_filled|, a copy of |url_template| with all
72 // instances of "{title}", "{text}", and "{url}" replaced with
73 // |title|, |text|, and |url| respectively.
74 // Replaces instances of "{X}" where "X" is any string besides "title",
75 // "text", and "url", with an empty string, for forwards compatibility.
76 // Returns false, if there are badly nested placeholders.
77 // This includes any case in which two "{" occur before a "}", or a "}"
78 // occurs with no preceding "{".
79 static bool ReplacePlaceholders(base::StringPiece url_template,
80 base::StringPiece title,
81 base::StringPiece text,
82 const GURL& share_url,
83 std::string* url_template_filled);
84
constantina5558f3f32017-02-13 05:37:5485 void OnPickerClosed(std::unique_ptr<base::DictionaryValue> share_targets,
86 const std::string& title,
mgiuca39e40672017-01-31 04:16:4987 const std::string& text,
88 const GURL& share_url,
89 const ShareCallback& callback,
constantinae4c513e72017-02-07 02:14:1090 base::Optional<std::string> result);
mgiuca39e40672017-01-31 04:16:4991
constantinac8b2173b2016-12-15 05:55:5192 DISALLOW_COPY_AND_ASSIGN(ShareServiceImpl);
93};
94
95#endif // CHROME_BROWSER_WEBSHARE_SHARE_SERVICE_IMPL_H_