OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/guest_view/mime_handler_view/mime_handler_view_guest.h" | |
6 | |
7 #include "base/strings/stringprintf.h" | |
8 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" | |
9 #include "chrome/browser/extensions/extension_renderer_state.h" | |
10 #include "chrome/browser/guest_view/guest_view_manager.h" | |
11 #include "chrome/browser/guest_view/web_view/web_view_renderer_state.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "content/public/browser/render_process_host.h" | |
14 #include "content/public/common/url_constants.h" | |
15 | |
16 #include "ipc/ipc_message_macros.h" | |
17 #include "extensions/common/extension_messages.h" | |
18 | |
19 using content::WebContents; | |
20 | |
21 // static | |
22 const char MimeHandlerViewGuest::Type[] = "mimehandler"; | |
23 | |
24 MimeHandlerViewGuest::MimeHandlerViewGuest( | |
25 content::BrowserContext* browser_context, | |
26 int guest_instance_id) | |
27 : GuestView<MimeHandlerViewGuest>( | |
28 browser_context, guest_instance_id) { | |
29 } | |
30 | |
31 MimeHandlerViewGuest::~MimeHandlerViewGuest() { | |
32 } | |
33 | |
34 void MimeHandlerViewGuest::CreateWebContents( | |
35 const std::string& embedder_extension_id, | |
36 int embedder_render_process_id, | |
37 const base::DictionaryValue& create_params, | |
38 const WebContentsCreatedCallback& callback) { | |
39 content::RenderProcessHost* embedder_render_process_host = | |
40 content::RenderProcessHost::FromID(embedder_render_process_id); | |
41 | |
42 std::string emburl; | |
43 if (!create_params.GetString("emburl", &emburl)) { | |
44 emburl = embedder_extension_id; | |
45 } else { | |
46 printf("Found emburl: %s\n", emburl.c_str()); | |
47 } | |
48 GURL guest_site(base::StringPrintf("%s://%s/", | |
49 content::kGuestScheme, | |
50 //embedder_extension_id.c_str(), | |
51 emburl.c_str())); | |
52 | |
53 // If we already have a webview tag in the same app using the same storage | |
54 // partition, we should use the same SiteInstance so the existing tag and | |
55 // the new tag can script each other. | |
56 GuestViewManager* guest_view_manager = | |
57 GuestViewManager::FromBrowserContext( | |
58 embedder_render_process_host->GetBrowserContext()); | |
59 content::SiteInstance* guest_site_instance = | |
60 guest_view_manager->GetGuestSiteInstance(guest_site); | |
61 if (!guest_site_instance) { | |
62 // Create the SiteInstance in a new BrowsingInstance, which will ensure | |
63 // that webview tags are also not allowed to send messages across | |
64 // different partitions. | |
65 guest_site_instance = content::SiteInstance::CreateForURL( | |
66 embedder_render_process_host->GetBrowserContext(), guest_site); | |
67 } | |
68 WebContents::CreateParams params( | |
69 embedder_render_process_host->GetBrowserContext(), | |
70 guest_site_instance); | |
71 params.guest_delegate = this; | |
72 callback.Run(WebContents::Create(params)); | |
73 } | |
74 | |
75 void MimeHandlerViewGuest::DidAttachToEmbedder() { | |
76 //CHECK(0); | |
77 std::string src; | |
78 if (extra_params()->GetString("src", &src) && !src.empty()) { | |
79 NavigateGuest(src); | |
Fady Samuel
2014/07/09 14:54:17
We don't expect MIME type handlers to navigate awa
lazyboy
2014/07/10 04:11:00
We still need to navigate once.
So we need one Nav
Fady Samuel
2014/07/10 15:13:42
Can we grab the URL from the browser process so th
| |
80 printf("NONEMPTY!!!!"); | |
81 } | |
82 printf("src...... %s\n", src.c_str()); | |
83 DCHECK(!GetOpener()); | |
84 } | |
85 | |
86 void MimeHandlerViewGuest::NavigateGuest(const std::string& src) { | |
87 GURL url = ResolveURL(src); | |
88 | |
89 // // Do not allow navigating a guest to schemes other than known safe schemes. | |
90 // // This will block the embedder trying to load unwanted schemes, e.g. | |
91 // // chrome://settings. | |
92 // bool scheme_is_blocked = | |
93 // (!content::ChildProcessSecurityPolicy::GetInstance()->IsWebSafeScheme( | |
94 // url.scheme()) && | |
95 // !url.SchemeIs(url::kAboutScheme)) || | |
96 // url.SchemeIs(url::kJavaScriptScheme); | |
97 // if (scheme_is_blocked || !url.is_valid()) { | |
98 // std::string error_type(net::ErrorToString(net::ERR_ABORTED)); | |
99 // DCHECK(StartsWithASCII(error_type, "net::", true)); | |
100 // error_type.erase(0, 5); | |
101 // LoadAbort(true /* is_top_level */, url, error_type); | |
102 // return; | |
103 // } | |
104 | |
105 GURL validated_url(url); | |
106 guest_web_contents()->GetRenderProcessHost()-> | |
107 FilterURL(false, &validated_url); | |
108 // As guests do not swap processes on navigation, only navigations to | |
109 // normal web URLs are supported. No protocol handlers are installed for | |
110 // other schemes (e.g., WebUI or extensions), and no permissions or bindings | |
111 // can be granted to the guest process. | |
112 LoadURLWithParams(validated_url, | |
113 content::Referrer(), | |
114 content::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
115 guest_web_contents()); | |
116 } | |
117 | |
118 GURL MimeHandlerViewGuest::ResolveURL(const std::string& src) { | |
119 return GURL(src); | |
120 } | |
121 | |
122 void MimeHandlerViewGuest::LoadURLWithParams( | |
123 const GURL& url, | |
124 const content::Referrer& referrer, | |
125 content::PageTransition transition_type, | |
126 content::WebContents* web_contents) { | |
127 content::NavigationController::LoadURLParams load_url_params(url); | |
128 load_url_params.referrer = referrer; | |
129 load_url_params.transition_type = transition_type; | |
130 load_url_params.extra_headers = std::string(); | |
131 web_contents->GetController().LoadURLWithParams(load_url_params); | |
132 } | |
133 | |
134 void MimeHandlerViewGuest::WillAttachToEmbedder() { | |
135 // We must install the mapping from guests to WebViews prior to resuming | |
136 // suspended resource loads so that the WebRequest API will catch resource | |
137 // requests. | |
138 //AddMimeHandlerViewToExtensionRendererState(); | |
139 PushMimeHandlerViewStateToIOThread(); | |
140 } | |
141 | |
142 void MimeHandlerViewGuest::PushMimeHandlerViewStateToIOThread() { | |
143 printf("+++++ %s\n", __PRETTY_FUNCTION__); | |
144 ////const GURL& site_url = guest_web_contents()->GetSiteInstance()->GetSiteURL (); | |
145 ////std::string partition_domain; | |
146 ////std::string partition_id; | |
147 ////bool in_memory; | |
148 ////if (!GetGuestPartitionConfigForSite( | |
149 //// site_url, &partition_domain, &partition_id, &in_memory)) { | |
150 //// NOTREACHED(); | |
151 //// return; | |
152 ////} | |
153 //DCHECK(embedder_extension_id() == partition_domain); | |
154 | |
155 WebViewRendererState::WebViewInfo web_view_info; | |
156 web_view_info.embedder_process_id = embedder_render_process_id(); | |
157 web_view_info.instance_id = view_instance_id(); | |
158 web_view_info.embedder_extension_id = embedder_extension_id(); | |
159 | |
160 printf("Post tasking AddGuest\n"); | |
161 content::BrowserThread::PostTask( | |
162 content::BrowserThread::IO, | |
163 FROM_HERE, | |
164 base::Bind(&WebViewRendererState::AddGuest, | |
165 base::Unretained(WebViewRendererState::GetInstance()), | |
166 guest_web_contents()->GetRenderProcessHost()->GetID(), | |
167 guest_web_contents()->GetRoutingID(), | |
168 web_view_info)); | |
169 } | |
170 | |
171 void MimeHandlerViewGuest::AttachWebContentsHelpers( | |
172 content::WebContents* contents) { | |
173 // Required for ExtensionHostMsg_PostMessage. | |
174 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( | |
175 contents); | |
176 } | |
177 | |
178 void MimeHandlerViewGuest::DidInitialize() { | |
179 printf("++++ %s\n", __PRETTY_FUNCTION__); | |
180 extension_function_dispatcher_.reset( | |
181 new extensions::ExtensionFunctionDispatcher( | |
182 browser_context(), this)); | |
183 AttachWebContentsHelpers(guest_web_contents()); | |
184 } | |
185 | |
186 extensions::WindowController* MimeHandlerViewGuest::GetExtensionWindowController () const { | |
187 return NULL; | |
188 } | |
189 | |
190 content::WebContents* MimeHandlerViewGuest::GetAssociatedWebContents() const { | |
191 return guest_web_contents(); | |
192 } | |
193 | |
194 bool MimeHandlerViewGuest::OnMessageReceived(const IPC::Message& message) { | |
195 printf("+++++ %s\n", __PRETTY_FUNCTION__); | |
196 bool handled = true; | |
197 printf("type: %d\n", message.type()); | |
198 printf("ExtensionHostMsg_Request::ID: %d\n", | |
199 ExtensionHostMsg_Request::ID); | |
200 IPC_BEGIN_MESSAGE_MAP(MimeHandlerViewGuest, message) | |
201 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | |
202 IPC_MESSAGE_UNHANDLED(handled = false) | |
203 IPC_END_MESSAGE_MAP() | |
204 return handled; | |
205 } | |
206 | |
207 void MimeHandlerViewGuest::OnRequest( | |
208 const ExtensionHostMsg_Request_Params& params) { | |
209 CHECK(0); | |
Fady Samuel
2014/07/09 14:54:17
We don't expect this to get called?
lazyboy
2014/07/10 04:11:00
No, we only need postMessage (chrome.runtime.sendM
| |
210 extension_function_dispatcher_->Dispatch( | |
211 params, guest_web_contents()->GetRenderViewHost()); | |
212 } | |
OLD | NEW |