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_MIME_HANDLER_VIEW_MIME_HANDLER_VIEW_CONTAINER_H_
6 #define EXTENSIONS_RENDERER_GUEST_VIEW_MIME_HANDLER_VIEW_MIME_HANDLER_VIEW_CONTAINER_H_
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "extensions/renderer/guest_view/guest_view_container.h"
14 #include "third_party/WebKit/public/platform/WebURLLoader.h"
15 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
16 #include "ui/gfx/geometry/size.h"
18 #include "v8/include/v8.h"
20 namespace extensions
{
22 // A container for loading up an extension inside a BrowserPlugin to handle a
23 // MIME type. A request for the URL of the data to load inside the container is
24 // made and a url is sent back in response which points to the URL which the
25 // container should be navigated to. There are two cases for making this URL
26 // request, the case where the plugin is embedded and the case where it is top
28 // 1) In the top level case a URL request for the data to load has already been
29 // made by the renderer on behalf of the plugin. The |DidReceiveData| and
30 // |DidFinishLoading| callbacks (from BrowserPluginDelegate) will be called
31 // when data is received and when it has finished being received,
33 // 2) In the embedded case, no URL request is automatically made by the
34 // renderer. We make a URL request for the data inside the container using
35 // a WebURLLoader. In this case, the |didReceiveData| and |didFinishLoading|
36 // (from WebURLLoaderClient) when data is received and when it has finished
38 class MimeHandlerViewContainer
: public GuestViewContainer
,
39 public blink::WebURLLoaderClient
{
41 MimeHandlerViewContainer(content::RenderFrame
* render_frame
,
42 const std::string
& mime_type
,
43 const GURL
& original_url
);
44 ~MimeHandlerViewContainer() override
;
46 static std::vector
<MimeHandlerViewContainer
*> FromRenderFrame(
47 content::RenderFrame
* render_frame
);
49 // BrowserPluginDelegate implementation.
50 void Ready() override
;
51 void DidFinishLoading() override
;
52 void DidReceiveData(const char* data
, int data_length
) override
;
53 void DidResizeElement(const gfx::Size
& old_size
,
54 const gfx::Size
& new_size
) override
;
55 bool OnMessageReceived(const IPC::Message
& message
) override
;
56 v8::Local
<v8::Object
> V8ScriptableObject(v8::Isolate
*) override
;
58 // WebURLLoaderClient overrides.
59 void didReceiveData(blink::WebURLLoader
* loader
,
62 int encoded_data_length
) override
;
63 void didFinishLoading(blink::WebURLLoader
* loader
,
65 int64_t total_encoded_data_length
) override
;
67 // GuestViewContainer overrides.
68 void OnRenderFrameDestroyed() override
;
70 // Post a JavaScript message to the guest.
71 void PostMessage(v8::Isolate
* isolate
,
72 v8::Handle
<v8::Value
> message
);
74 // Post |message| to the guest.
75 void PostMessageFromValue(const base::Value
& message
);
79 void OnCreateMimeHandlerViewGuestACK(int element_instance_id
);
80 void OnGuestAttached(int element_instance_id
,
81 int guest_proxy_routing_id
);
82 void OnMimeHandlerViewGuestOnLoadCompleted(int element_instance_id
);
84 void CreateMimeHandlerViewGuest();
86 // The MIME type of the plugin.
87 const std::string mime_type_
;
89 // The URL of the extension to navigate to.
92 // Whether the plugin is embedded or not.
95 // The original URL of the plugin.
98 // The RenderView routing ID of the guest.
99 int guest_proxy_routing_id_
;
101 // A URL loader to load the |original_url_| when the plugin is embedded. In
102 // the embedded case, no URL request is made automatically.
103 scoped_ptr
<blink::WebURLLoader
> loader_
;
105 // The scriptable object that backs the plugin.
106 v8::Global
<v8::Object
> scriptable_object_
;
108 // Pending postMessage messages that need to be sent to the guest. These are
109 // queued while the guest is loading and once it is fully loaded they are
110 // delivered so that messages aren't lost.
111 std::vector
<linked_ptr
<v8::Global
<v8::Value
>>> pending_messages_
;
113 // True if the guest page has fully loaded and its JavaScript onload function
117 // The size of the element.
118 gfx::Size element_size_
;
120 base::WeakPtrFactory
<MimeHandlerViewContainer
> weak_factory_
;
122 DISALLOW_COPY_AND_ASSIGN(MimeHandlerViewContainer
);
125 } // namespace extensions
127 #endif // EXTENSIONS_RENDERER_GUEST_VIEW_MIME_HANDLER_VIEW_MIME_HANDLER_VIEW_CONTAINER_H_