summaryrefslogtreecommitdiffstats
path: root/blimp/engine/feature/engine_render_widget_feature.h
blob: 155ad3e538a95ed1fbe1e10edaa8d232c94f833c (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2015 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 BLIMP_ENGINE_FEATURE_ENGINE_RENDER_WIDGET_FEATURE_H_
#define BLIMP_ENGINE_FEATURE_ENGINE_RENDER_WIDGET_FEATURE_H_

#include <stdint.h>

#include <map>
#include <vector>

#include "base/atomic_sequence_num.h"
#include "base/containers/small_map.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "blimp/net/blimp_message_processor.h"
#include "blimp/net/input_message_converter.h"

namespace blink {
class WebGestureEvent;
}

namespace content {
class RenderWidgetHost;
}

namespace blimp {

// Handles all incoming and outgoing protobuf message types tied to a specific
// RenderWidget. This includes BlimpMessage::INPUT, BlimpMessage::COMPOSITOR,
// and BlimpMessage::RENDER_WIDGET messages.  Delegates can be added to be
// notified of incoming messages. This class automatically handles dropping
// stale BlimpMessage::RENDER_WIDGET messages from the client after a
// RenderWidgetMessage::INITIALIZE message is sent.
class EngineRenderWidgetFeature : public BlimpMessageProcessor {
 public:
  // A delegate to be notified of specific RenderWidget related incoming events.
  class RenderWidgetMessageDelegate {
   public:
    // Called when the client is sending a WebGestureEvent to the engine.
    virtual void OnWebGestureEvent(
        content::RenderWidgetHost* render_widget_host,
        scoped_ptr<blink::WebGestureEvent> event) = 0;

    // Called when the client sent a CompositorMessage.  These messages should
    // be sent to the engine's render process so they can be processed by the
    // RemoteChannel of the compositor.
    virtual void OnCompositorMessageReceived(
        content::RenderWidgetHost* render_widget_host,
        const std::vector<uint8_t>& message) = 0;
  };

  EngineRenderWidgetFeature();
  ~EngineRenderWidgetFeature() override;

  void set_render_widget_message_sender(
      scoped_ptr<BlimpMessageProcessor> message_processor);

  void set_input_message_sender(
      scoped_ptr<BlimpMessageProcessor> message_processor);

  void set_compositor_message_sender(
      scoped_ptr<BlimpMessageProcessor> message_processor);

  // Notifes the client that a new RenderWidget for a particular WebContents has
  // been created. This will trigger the creation of the BlimpCompositor for
  // this widget on the client.
  void OnRenderWidgetCreated(const int tab_id,
                             content::RenderWidgetHost* render_widget_host);

  // Notifies the client that the RenderWidget for a particular WebContents has
  // changed.  When this is sent the native view on the client becomes bound to
  // the BlimpCompositor for this widget.
  // Since the compositor on the client performs the operations of the view for
  // this widget, this will set the visibility and draw state correctly for this
  // widget.
  // Note: This assumes that this is the RenderWidgetHost for the main frame.
  // Only one RenderWidget can be in initialized state for a tab.
  void OnRenderWidgetInitialized(const int tab_id,
                                 content::RenderWidgetHost* render_widget_host);

  void OnRenderWidgetDeleted(const int tab_id,
                             content::RenderWidgetHost* render_widget_host);

  // Sends a CompositorMessage for |tab_id| to the client.
  void SendCompositorMessage(const int tab_id,
                             content::RenderWidgetHost* render_widget_host,
                             const std::vector<uint8_t>& message);

  // Sets a RenderWidgetMessageDelegate to be notified of all incoming
  // RenderWidget related messages for |tab_id| from the client.  There can only
  // be one RenderWidgetMessageDelegate per tab.
  void SetDelegate(const int tab_id, RenderWidgetMessageDelegate* delegate);
  void RemoveDelegate(const int tab_id);

  // BlimpMessageProcessor implementation.
  void ProcessMessage(scoped_ptr<BlimpMessage> message,
                      const net::CompletionCallback& callback) override;

 private:
  typedef base::SmallMap<std::map<int, RenderWidgetMessageDelegate*> >
  DelegateMap;

  typedef base::SmallMap<std::map<content::RenderWidgetHost*, int>>
      RenderWidgetToIdMap;

  typedef base::SmallMap<std::map<int, content::RenderWidgetHost*>>
      IdToRenderWidgetMap;

  typedef std::pair<RenderWidgetToIdMap, IdToRenderWidgetMap> RenderWidgetMaps;

  typedef base::SmallMap<std::map<int, RenderWidgetMaps>> TabMap;

  // Returns nullptr if no delegate is found.
  RenderWidgetMessageDelegate* FindDelegate(const int tab_id);

  // Adds the RenderWidgetHost to the map and return the render_widget_id.
  int AddRenderWidget(const int tab_id,
                      content::RenderWidgetHost* render_widget_host);

  // Deletes the RenderWidgetHost from the map and returns the render_widget_id
  // for the deleted host.
  int DeleteRenderWidget(const int tab_id,
                         content::RenderWidgetHost* render_widget_host);

  // Returns the render_widget_id for the RenderWidgetHost. Will return 0U if
  // the host is not found.
  int GetRenderWidgetId(const int tab_id,
                        content::RenderWidgetHost* render_widget_host);

  // Returns the RenderWidgetHost for the given render_widget_id. Will return
  // nullptr if no host is found.
  content::RenderWidgetHost* GetRenderWidgetHost(const int tab_id,
                                                 const int render_widget_id);

  DelegateMap delegates_;
  TabMap tabs_;

  // A RenderWidgetHost can also be uniquely identified by the
  // <process_id, routing_id> where the process_id is the id for the
  // RenderProcessHost for this widget and the routing_id is the id for the
  // widget.
  // But we generate our own ids to avoid having the render widget protocol tied
  // to always using a combination of these ids, generated by the content layer.
  // By using an AtomicSequenceNumber for identifying render widgets across
  // tabs, we can be sure that there will always be a 1:1 mapping between the
  // render widget and the consumer of the features tied to this widget on the
  // client, which is the BlimpCompositor.
  base::AtomicSequenceNumber next_widget_id_;

  InputMessageConverter input_message_converter_;

  // Outgoing message processors for RENDER_WIDGET, COMPOSITOR and INPUT types.
  scoped_ptr<BlimpMessageProcessor> render_widget_message_sender_;
  scoped_ptr<BlimpMessageProcessor> compositor_message_sender_;
  scoped_ptr<BlimpMessageProcessor> input_message_sender_;

  DISALLOW_COPY_AND_ASSIGN(EngineRenderWidgetFeature);
};

}  // namespace blimp

#endif  // BLIMP_ENGINE_FEATURE_ENGINE_RENDER_WIDGET_FEATURE_H_