summaryrefslogtreecommitdiff
path: root/chromium/headless/public/headless_tab_socket.h
blob: 51cb7d391bfda7269c74b36821d3e55f161b2bcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef HEADLESS_PUBLIC_HEADLESS_TAB_SOCKET_H_
#define HEADLESS_PUBLIC_HEADLESS_TAB_SOCKET_H_

#include <string>

#include "base/macros.h"
#include "base/optional.h"
#include "headless/public/headless_export.h"

namespace headless {

// A bidirectional communications channel between C++ and JS.
class HEADLESS_EXPORT HeadlessTabSocket {
 public:
  class HEADLESS_EXPORT Listener {
   public:
    Listener() {}
    virtual ~Listener() {}

    // The |message| and |v8_execution_context_id| may be potentially sent by
    // untrusted web content so it should be validated carefully.
    virtual void OnMessageFromContext(const std::string& message,
                                      int v8_execution_context_id) = 0;

   private:
    DISALLOW_COPY_AND_ASSIGN(Listener);
  };

  // Installs headless tab socket bindings into the specified execution context.
  // If the bindings are successfully installed then the |callback| is run with
  // |success| = true, otherwise with |success| = false.
  virtual void InstallHeadlessTabSocketBindings(
      int v8_execution_context_id,
      base::OnceCallback<void(bool success)> callback) = 0;

  // Installs headless tab socket bindings into the main frame main world. If
  // the bindings were installed correctly then |callback| is run with a
  // non-empty base::Optional<int> containing the main world
  // v8_execution_context_id, otherwise |callback| is run with an empty
  // base::Optional<int>.
  virtual void InstallMainFrameMainWorldHeadlessTabSocketBindings(
      base::OnceCallback<void(base::Optional<int> v8_execution_context_id)>
          callback) = 0;

  // Note this will fail unless the bindings have been installed.
  virtual void SendMessageToContext(const std::string& message,
                                    int v8_execution_context_id) = 0;

  virtual void SetListener(Listener* listener) = 0;

 protected:
  HeadlessTabSocket() {}
  virtual ~HeadlessTabSocket() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(HeadlessTabSocket);
};

}  // namespace headless

#endif  // HEADLESS_PUBLIC_HEADLESS_TAB_SOCKET_H_