1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_RENDERER_GUEST_VIEW_EXTENSIONS_GUEST_VIEW_CONTAINER_H_
6 #define EXTENSIONS_RENDERER_GUEST_VIEW_EXTENSIONS_GUEST_VIEW_CONTAINER_H_
10 #include "base/memory/linked_ptr.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h"
13 #include "extensions/renderer/guest_view/guest_view_container.h"
14 #include "v8/include/v8.h"
20 namespace extensions
{
22 class ExtensionsGuestViewContainer
: public GuestViewContainer
{
27 Request(GuestViewContainer
* container
,
28 v8::Handle
<v8::Function
> callback
,
29 v8::Isolate
* isolate
);
32 virtual void PerformRequest() = 0;
33 virtual void HandleResponse(const IPC::Message
& message
) = 0;
35 void ExecuteCallbackIfAvailable(int argc
,
36 scoped_ptr
<v8::Handle
<v8::Value
>[]> argv
);
38 GuestViewContainer
* container() const { return container_
; }
40 bool HasCallback() const;
42 v8::Handle
<v8::Function
> GetCallback() const;
44 v8::Isolate
* isolate() const { return isolate_
; }
47 GuestViewContainer
* container_
;
48 v8::Global
<v8::Function
> callback_
;
49 v8::Isolate
* const isolate_
;
52 // This class represents an AttachGuest request from Javascript. It includes
53 // the input parameters and the callback function. The Attach operation may
54 // not execute immediately, if the container is not ready or if there are
55 // other attach operations in flight.
56 class AttachRequest
: public Request
{
58 AttachRequest(GuestViewContainer
* container
,
59 int guest_instance_id
,
60 scoped_ptr
<base::DictionaryValue
> params
,
61 v8::Handle
<v8::Function
> callback
,
62 v8::Isolate
* isolate
);
63 ~AttachRequest() override
;
65 void PerformRequest() override
;
66 void HandleResponse(const IPC::Message
& message
) override
;
69 const int guest_instance_id_
;
70 scoped_ptr
<base::DictionaryValue
> params_
;
73 class DetachRequest
: public Request
{
75 DetachRequest(GuestViewContainer
* container
,
76 v8::Handle
<v8::Function
> callback
,
77 v8::Isolate
* isolate
);
78 ~DetachRequest() override
;
80 void PerformRequest() override
;
81 void HandleResponse(const IPC::Message
& message
) override
;
84 explicit ExtensionsGuestViewContainer(content::RenderFrame
* render_frame
);
85 ~ExtensionsGuestViewContainer() override
;
87 static ExtensionsGuestViewContainer
* FromID(int element_instance_id
);
89 void IssueRequest(linked_ptr
<Request
> request
);
90 void RegisterDestructionCallback(v8::Handle
<v8::Function
> callback
,
91 v8::Isolate
* isolate
);
92 void RegisterElementResizeCallback(v8::Handle
<v8::Function
> callback
,
93 v8::Isolate
* isolate
);
95 // BrowserPluginDelegate implementation.
96 bool OnMessageReceived(const IPC::Message
& message
) override
;
97 void SetElementInstanceID(int element_instance_id
) override
;
98 void Ready() override
;
99 void DidResizeElement(const gfx::Size
& old_size
,
100 const gfx::Size
& new_size
) override
;
103 void OnHandleCallback(const IPC::Message
& message
);
105 void CallElementResizeCallback(const gfx::Size
& old_size
,
106 const gfx::Size
& new_size
);
107 void EnqueueRequest(linked_ptr
<Request
> request
);
108 void PerformPendingRequest();
109 void HandlePendingResponseCallback(const IPC::Message
& message
);
113 std::deque
<linked_ptr
<Request
> > pending_requests_
;
114 linked_ptr
<Request
> pending_response_
;
116 v8::Global
<v8::Function
> destruction_callback_
;
117 v8::Isolate
* destruction_isolate_
;
119 v8::Global
<v8::Function
> element_resize_callback_
;
120 v8::Isolate
* element_resize_isolate_
;
122 // Weak pointer factory used for calling the element resize callback.
123 base::WeakPtrFactory
<ExtensionsGuestViewContainer
> weak_ptr_factory_
;
125 DISALLOW_COPY_AND_ASSIGN(ExtensionsGuestViewContainer
);
128 } // namespace extensions
130 #endif // EXTENSIONS_RENDERER_GUEST_VIEW_EXTENSIONS_GUEST_VIEW_CONTAINER_H_