summaryrefslogtreecommitdiffstats
path: root/extensions/renderer/guest_view/extensions_guest_view_container.h
blob: 1fbe126203c9726d49536451ccf694a5a4d6f022 (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
// Copyright 2014 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 EXTENSIONS_RENDERER_GUEST_VIEW_EXTENSIONS_GUEST_VIEW_CONTAINER_H_
#define EXTENSIONS_RENDERER_GUEST_VIEW_EXTENSIONS_GUEST_VIEW_CONTAINER_H_

#include <queue>

#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "extensions/renderer/guest_view/guest_view_container.h"
#include "extensions/renderer/scoped_persistent.h"

namespace extensions {

class ExtensionsGuestViewContainer : public GuestViewContainer {
 public:

  class Request {
   public:
    Request(GuestViewContainer* container,
            v8::Handle<v8::Function> callback,
            v8::Isolate* isolate);
    virtual ~Request();

    virtual void PerformRequest() = 0;
    virtual void HandleResponse(const IPC::Message& message) = 0;

    GuestViewContainer* container() const { return container_; }

    bool HasCallback() const;

    v8::Handle<v8::Function> GetCallback() const;

    v8::Isolate* isolate() const { return isolate_; }

   private:
    GuestViewContainer* container_;
    ScopedPersistent<v8::Function> callback_;
    v8::Isolate* const isolate_;
  };

  // This class represents an AttachGuest request from Javascript. It includes
  // the input parameters and the callback function. The Attach operation may
  // not execute immediately, if the container is not ready or if there are
  // other attach operations in flight.
  class AttachRequest : public Request {
   public:
    AttachRequest(GuestViewContainer* container,
                  int guest_instance_id,
                  scoped_ptr<base::DictionaryValue> params,
                  v8::Handle<v8::Function> callback,
                  v8::Isolate* isolate);
    ~AttachRequest() override;

    void PerformRequest() override;
    void HandleResponse(const IPC::Message& message) override;

   private:
    const int guest_instance_id_;
    scoped_ptr<base::DictionaryValue> params_;
  };

  class DetachRequest : public Request {
   public:
    DetachRequest(GuestViewContainer* container,
                  v8::Handle<v8::Function> callback,
                  v8::Isolate* isolate);
    ~DetachRequest() override;

    void PerformRequest() override;
    void HandleResponse(const IPC::Message& message) override;
  };

  explicit ExtensionsGuestViewContainer(content::RenderFrame* render_frame);
  ~ExtensionsGuestViewContainer() override;

  static ExtensionsGuestViewContainer* FromID(int element_instance_id);

  void IssueRequest(linked_ptr<Request> request);
  void RegisterDestructionCallback(v8::Handle<v8::Function> callback,
                                   v8::Isolate* isolate);

  // BrowserPluginDelegate implementation.
  bool OnMessageReceived(const IPC::Message& message) override;
  void SetElementInstanceID(int element_instance_id) override;
  void Ready() override;

 private:
  void OnHandleCallback(const IPC::Message& message);

  void EnqueueRequest(linked_ptr<Request> request);
  void PerformPendingRequest();
  void HandlePendingResponseCallback(const IPC::Message& message);

  bool ready_;

  std::deque<linked_ptr<Request> > pending_requests_;
  linked_ptr<Request> pending_response_;

  ScopedPersistent<v8::Function> destruction_callback_;
  v8::Isolate* destruction_isolate_;

  DISALLOW_COPY_AND_ASSIGN(ExtensionsGuestViewContainer);
};

}  // namespace extensions

#endif  // EXTENSIONS_RENDERER_GUEST_VIEW_EXTENSIONS_GUEST_VIEW_CONTAINER_H_