blob: 538c661097117a71f273d988105c023e3351c7ac [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"
mgiuca39e40672017-01-31 04:16:4914#include "base/strings/string16.h"
constantina5558f3f32017-02-13 05:37:5415#include "chrome/browser/ui/browser.h"
16#include "chrome/browser/ui/browser_dialogs.h"
constantinac8b2173b2016-12-15 05:55:5117#include "mojo/public/cpp/bindings/interface_request.h"
bena5c972c2017-05-04 01:38:4318#include "services/service_manager/public/cpp/bind_source_info.h"
constantinac8b2173b2016-12-15 05:55:5119#include "third_party/WebKit/public/platform/modules/webshare/webshare.mojom.h"
constantina5558f3f32017-02-13 05:37:5420#include "third_party/WebKit/public/platform/site_engagement.mojom.h"
constantinac8b2173b2016-12-15 05:55:5121
constantina5558f3f32017-02-13 05:37:5422class DictionaryValue;
constantinac8b2173b2016-12-15 05:55:5123class GURL;
24
constantinac8b2173b2016-12-15 05:55:5125// Desktop implementation of the ShareService Mojo service.
26class ShareServiceImpl : public blink::mojom::ShareService {
27 public:
constantina5558f3f32017-02-13 05:37:5428 ShareServiceImpl();
29 ~ShareServiceImpl() override;
constantinac8b2173b2016-12-15 05:55:5130
bena5c972c2017-05-04 01:38:4331 static void Create(const service_manager::BindSourceInfo& source_info,
32 mojo::InterfaceRequest<ShareService> request);
constantinac8b2173b2016-12-15 05:55:5133
constantina2cfa55e2017-01-13 06:36:5534 // blink::mojom::ShareService overrides:
constantinac8b2173b2016-12-15 05:55:5135 void Share(const std::string& title,
36 const std::string& text,
constantina2cfa55e2017-01-13 06:36:5537 const GURL& share_url,
tzikcf7bcd652017-06-15 04:19:3038 ShareCallback callback) override;
constantinac8b2173b2016-12-15 05:55:5139
40 private:
constantina2cfa55e2017-01-13 06:36:5541 FRIEND_TEST_ALL_PREFIXES(ShareServiceImplUnittest, ReplacePlaceholders);
42
constantina5558f3f32017-02-13 05:37:5443 Browser* GetBrowser();
44
45 // Returns the URL template of the target identified by |target_url|
mgiucaf70772342017-02-14 01:45:4246 std::string GetTargetTemplate(const std::string& target_url,
47 const base::DictionaryValue& share_targets);
constantina5558f3f32017-02-13 05:37:5448
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
mgiuca39e40672017-01-31 04:16:4956 // Shows the share picker dialog with |targets| as the list of applications
constantinae4c513e72017-02-07 02:14:1057 // 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.
mgiuca39e40672017-01-31 04:16:4960 virtual void ShowPickerDialog(
constantinae4c513e72017-02-07 02:14:1061 const std::vector<std::pair<base::string16, GURL>>& targets,
ortuno928d14102017-05-02 00:09:0562 chrome::WebShareTargetPickerCallback callback);
mgiuca39e40672017-01-31 04:16:4963
constantina2cfa55e2017-01-13 06:36:5564 // Opens a new tab and navigates to |target_url|.
65 // Virtual for testing purposes.
66 virtual void OpenTargetURL(const GURL& target_url);
67
constantina5558f3f32017-02-13 05:37:5468 // 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
constantina2cfa55e2017-01-13 06:36:5574 // 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
constantina5558f3f32017-02-13 05:37:5488 void OnPickerClosed(std::unique_ptr<base::DictionaryValue> share_targets,
89 const std::string& title,
mgiuca39e40672017-01-31 04:16:4990 const std::string& text,
91 const GURL& share_url,
tzikcf7bcd652017-06-15 04:19:3092 ShareCallback callback,
ortuno928d14102017-05-02 00:09:0593 const base::Optional<std::string>& result);
mgiuca39e40672017-01-31 04:16:4994
mgiucabd4b24d2017-02-17 01:40:5795 base::WeakPtrFactory<ShareServiceImpl> weak_factory_;
96
constantinac8b2173b2016-12-15 05:55:5197 DISALLOW_COPY_AND_ASSIGN(ShareServiceImpl);
98};
99
100#endif // CHROME_BROWSER_WEBSHARE_SHARE_SERVICE_IMPL_H_