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 "extensions/renderer/scoped_persistent.h"
15 #include "third_party/WebKit/public/platform/WebURLLoader.h"
16 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
17 #include "ui/gfx/geometry/size.h"
19 #include "v8/include/v8.h"
21 namespace extensions
{
23 // A container for loading up an extension inside a BrowserPlugin to handle a
24 // MIME type. A request for the URL of the data to load inside the container is
25 // made and a url is sent back in response which points to the URL which the
26 // container should be navigated to. There are two cases for making this URL
27 // request, the case where the plugin is embedded and the case where it is top
29 // 1) In the top level case a URL request for the data to load has already been
30 // made by the renderer on behalf of the plugin. The |DidReceiveData| and
31 // |DidFinishLoading| callbacks (from BrowserPluginDelegate) will be called
32 // when data is received and when it has finished being received,
34 // 2) In the embedded case, no URL request is automatically made by the
35 // renderer. We make a URL request for the data inside the container using
36 // a WebURLLoader. In this case, the |didReceiveData| and |didFinishLoading|
37 // (from WebURLLoaderClient) when data is received and when it has finished
39 class MimeHandlerViewContainer
: public GuestViewContainer
,
40 public blink::WebURLLoaderClient
{
42 MimeHandlerViewContainer(content::RenderFrame
* render_frame
,
43 const std::string
& mime_type
,
44 const GURL
& original_url
);
45 ~MimeHandlerViewContainer() override
;
47 static std::vector
<MimeHandlerViewContainer
*> FromRenderFrame(
48 content::RenderFrame
* render_frame
);
50 // BrowserPluginDelegate implementation.
51 void Ready() override
;
52 void DidFinishLoading() override
;
53 void DidReceiveData(const char* data
, int data_length
) override
;
54 void DidResizeElement(const gfx::Size
& old_size
,
55 const gfx::Size
& new_size
) override
;
56 bool OnMessageReceived(const IPC::Message
& message
) override
;
57 v8::Local
<v8::Object
> V8ScriptableObject(v8::Isolate
*) override
;
59 // WebURLLoaderClient overrides.
60 void didReceiveData(blink::WebURLLoader
* loader
,
63 int encoded_data_length
) override
;
64 void didFinishLoading(blink::WebURLLoader
* loader
,
66 int64_t total_encoded_data_length
) override
;
68 // GuestViewContainer overrides.
69 void OnRenderFrameDestroyed() override
;
71 // Post a JavaScript message to the guest.
72 void PostMessage(v8::Isolate
* isolate
,
73 v8::Handle
<v8::Value
> message
);
75 // Post |message| to the guest.
76 void PostMessageFromValue(const base::Value
& message
);
80 void OnCreateMimeHandlerViewGuestACK(int element_instance_id
);
81 void OnGuestAttached(int element_instance_id
,
82 int guest_proxy_routing_id
);
83 void OnMimeHandlerViewGuestOnLoadCompleted(int element_instance_id
);
85 void CreateMimeHandlerViewGuest();
87 // The MIME type of the plugin.
88 const std::string mime_type_
;
90 // The URL of the extension to navigate to.
93 // Whether the plugin is embedded or not.
96 // The original URL of the plugin.
99 // The RenderView routing ID of the guest.
100 int guest_proxy_routing_id_
;
102 // A URL loader to load the |original_url_| when the plugin is embedded. In
103 // the embedded case, no URL request is made automatically.
104 scoped_ptr
<blink::WebURLLoader
> loader_
;
106 // The scriptable object that backs the plugin.
107 ScopedPersistent
<v8::Object
> scriptable_object_
;
109 // Pending postMessage messages that need to be sent to the guest. These are
110 // queued while the guest is loading and once it is fully loaded they are
111 // delivered so that messages aren't lost.
112 std::vector
<linked_ptr
<ScopedPersistent
<v8::Value
>>> pending_messages_
;
114 // True if the guest page has fully loaded and its JavaScript onload function
118 // The size of the element.
119 gfx::Size element_size_
;
121 base::WeakPtrFactory
<MimeHandlerViewContainer
> weak_factory_
;
123 DISALLOW_COPY_AND_ASSIGN(MimeHandlerViewContainer
);
126 } // namespace extensions
128 #endif // EXTENSIONS_RENDERER_GUEST_VIEW_MIME_HANDLER_VIEW_MIME_HANDLER_VIEW_CONTAINER_H_