blob: bcc2a5575544da2e6dd01c0c0d4a2a1b935206f1 [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
8#include <string>
9
constantina2cfa55e2017-01-13 06:36:5510#include "base/gtest_prod_util.h"
mgiuca39e40672017-01-31 04:16:4911#include "base/strings/string16.h"
constantinac8b2173b2016-12-15 05:55:5112#include "mojo/public/cpp/bindings/interface_request.h"
13#include "third_party/WebKit/public/platform/modules/webshare/webshare.mojom.h"
14
15class GURL;
16
mgiuca39e40672017-01-31 04:16:4917enum class SharePickerResult {
18 CANCEL,
19 SHARE
20};
21
constantinac8b2173b2016-12-15 05:55:5122// Desktop implementation of the ShareService Mojo service.
23class ShareServiceImpl : public blink::mojom::ShareService {
24 public:
25 ShareServiceImpl() = default;
26 ~ShareServiceImpl() override = default;
27
28 static void Create(mojo::InterfaceRequest<ShareService> request);
29
constantina2cfa55e2017-01-13 06:36:5530 // blink::mojom::ShareService overrides:
constantinac8b2173b2016-12-15 05:55:5131 void Share(const std::string& title,
32 const std::string& text,
constantina2cfa55e2017-01-13 06:36:5533 const GURL& share_url,
constantinac8b2173b2016-12-15 05:55:5134 const ShareCallback& callback) override;
35
36 private:
constantina2cfa55e2017-01-13 06:36:5537 FRIEND_TEST_ALL_PREFIXES(ShareServiceImplUnittest, ReplacePlaceholders);
38
mgiuca39e40672017-01-31 04:16:4939 // Shows the share picker dialog with |targets| as the list of applications
40 // presented to the user. Passes the result to |callback|. Virtual for
41 // testing.
42 virtual void ShowPickerDialog(
43 const std::vector<base::string16>& targets,
44 const base::Callback<void(SharePickerResult)>& callback);
45
constantina2cfa55e2017-01-13 06:36:5546 // Opens a new tab and navigates to |target_url|.
47 // Virtual for testing purposes.
48 virtual void OpenTargetURL(const GURL& target_url);
49
50 // Writes to |url_template_filled|, a copy of |url_template| with all
51 // instances of "{title}", "{text}", and "{url}" replaced with
52 // |title|, |text|, and |url| respectively.
53 // Replaces instances of "{X}" where "X" is any string besides "title",
54 // "text", and "url", with an empty string, for forwards compatibility.
55 // Returns false, if there are badly nested placeholders.
56 // This includes any case in which two "{" occur before a "}", or a "}"
57 // occurs with no preceding "{".
58 static bool ReplacePlaceholders(base::StringPiece url_template,
59 base::StringPiece title,
60 base::StringPiece text,
61 const GURL& share_url,
62 std::string* url_template_filled);
63
mgiuca39e40672017-01-31 04:16:4964 void OnPickerClosed(const std::string& title,
65 const std::string& text,
66 const GURL& share_url,
67 const ShareCallback& callback,
68 SharePickerResult result);
69
constantinac8b2173b2016-12-15 05:55:5170 DISALLOW_COPY_AND_ASSIGN(ShareServiceImpl);
71};
72
73#endif // CHROME_BROWSER_WEBSHARE_SHARE_SERVICE_IMPL_H_