OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | 5 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 int webview_instance_id) { | 195 int webview_instance_id) { |
196 bool is_web_view = embedder_process_id && webview_instance_id; | 196 bool is_web_view = embedder_process_id && webview_instance_id; |
197 if (!is_web_view) | 197 if (!is_web_view) |
198 return RulesRegistryService::kDefaultRulesRegistryID; | 198 return RulesRegistryService::kDefaultRulesRegistryID; |
199 | 199 |
200 WebViewKey key = std::make_pair(embedder_process_id, webview_instance_id); | 200 WebViewKey key = std::make_pair(embedder_process_id, webview_instance_id); |
201 auto it = web_view_key_to_id_map.Get().find(key); | 201 auto it = web_view_key_to_id_map.Get().find(key); |
202 if (it != web_view_key_to_id_map.Get().end()) | 202 if (it != web_view_key_to_id_map.Get().end()) |
203 return it->second; | 203 return it->second; |
204 | 204 |
205 content::RenderProcessHost* rph = | 205 auto rph = content::RenderProcessHost::FromID(embedder_process_id); |
206 content::RenderProcessHost::FromID(embedder_process_id); | |
207 int rules_registry_id = | 206 int rules_registry_id = |
208 RulesRegistryService::Get(rph->GetBrowserContext())-> | 207 RulesRegistryService::Get(rph->GetBrowserContext())-> |
209 GetNextRulesRegistryID(); | 208 GetNextRulesRegistryID(); |
210 web_view_key_to_id_map.Get()[key] = rules_registry_id; | 209 web_view_key_to_id_map.Get()[key] = rules_registry_id; |
211 return rules_registry_id; | 210 return rules_registry_id; |
212 } | 211 } |
213 | 212 |
214 // static | 213 // static |
215 int WebViewGuest::GetViewInstanceId(WebContents* contents) { | 214 int WebViewGuest::GetViewInstanceId(WebContents* contents) { |
216 WebViewGuest* guest = FromWebContents(contents); | 215 auto guest = FromWebContents(contents); |
217 if (!guest) | 216 if (!guest) |
218 return guestview::kInstanceIDNone; | 217 return guestview::kInstanceIDNone; |
219 | 218 |
220 return guest->view_instance_id(); | 219 return guest->view_instance_id(); |
221 } | 220 } |
222 | 221 |
223 const char* WebViewGuest::GetAPINamespace() const { | 222 const char* WebViewGuest::GetAPINamespace() const { |
224 return webview::kAPINamespace; | 223 return webview::kAPINamespace; |
225 } | 224 } |
226 | 225 |
(...skipping 26 matching lines...) Expand all Loading... |
253 std::string partition_domain = GetOwnerSiteURL().host(); | 252 std::string partition_domain = GetOwnerSiteURL().host(); |
254 GURL guest_site(base::StringPrintf("%s://%s/%s?%s", | 253 GURL guest_site(base::StringPrintf("%s://%s/%s?%s", |
255 content::kGuestScheme, | 254 content::kGuestScheme, |
256 partition_domain.c_str(), | 255 partition_domain.c_str(), |
257 persist_storage ? "persist" : "", | 256 persist_storage ? "persist" : "", |
258 url_encoded_partition.c_str())); | 257 url_encoded_partition.c_str())); |
259 | 258 |
260 // If we already have a webview tag in the same app using the same storage | 259 // If we already have a webview tag in the same app using the same storage |
261 // partition, we should use the same SiteInstance so the existing tag and | 260 // partition, we should use the same SiteInstance so the existing tag and |
262 // the new tag can script each other. | 261 // the new tag can script each other. |
263 GuestViewManager* guest_view_manager = | 262 auto guest_view_manager = GuestViewManager::FromBrowserContext( |
264 GuestViewManager::FromBrowserContext( | 263 owner_render_process_host->GetBrowserContext()); |
265 owner_render_process_host->GetBrowserContext()); | |
266 content::SiteInstance* guest_site_instance = | 264 content::SiteInstance* guest_site_instance = |
267 guest_view_manager->GetGuestSiteInstance(guest_site); | 265 guest_view_manager->GetGuestSiteInstance(guest_site); |
268 if (!guest_site_instance) { | 266 if (!guest_site_instance) { |
269 // Create the SiteInstance in a new BrowsingInstance, which will ensure | 267 // Create the SiteInstance in a new BrowsingInstance, which will ensure |
270 // that webview tags are also not allowed to send messages across | 268 // that webview tags are also not allowed to send messages across |
271 // different partitions. | 269 // different partitions. |
272 guest_site_instance = content::SiteInstance::CreateForURL( | 270 guest_site_instance = content::SiteInstance::CreateForURL( |
273 owner_render_process_host->GetBrowserContext(), guest_site); | 271 owner_render_process_host->GetBrowserContext(), guest_site); |
274 } | 272 } |
275 WebContents::CreateParams params( | 273 WebContents::CreateParams params( |
(...skipping 714 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
990 // We need to set the background opaque flag after navigation to ensure that | 988 // We need to set the background opaque flag after navigation to ensure that |
991 // there is a RenderWidgetHostView available. | 989 // there is a RenderWidgetHostView available. |
992 SetAllowTransparency(allow_transparency); | 990 SetAllowTransparency(allow_transparency); |
993 | 991 |
994 bool is_pending_new_window = false; | 992 bool is_pending_new_window = false; |
995 if (GetOpener()) { | 993 if (GetOpener()) { |
996 // We need to do a navigation here if the target URL has changed between | 994 // We need to do a navigation here if the target URL has changed between |
997 // the time the WebContents was created and the time it was attached. | 995 // the time the WebContents was created and the time it was attached. |
998 // We also need to do an initial navigation if a RenderView was never | 996 // We also need to do an initial navigation if a RenderView was never |
999 // created for the new window in cases where there is no referrer. | 997 // created for the new window in cases where there is no referrer. |
1000 PendingWindowMap::iterator it = | 998 auto it = GetOpener()->pending_new_windows_.find(this); |
1001 GetOpener()->pending_new_windows_.find(this); | |
1002 if (it != GetOpener()->pending_new_windows_.end()) { | 999 if (it != GetOpener()->pending_new_windows_.end()) { |
1003 const NewWindowInfo& new_window_info = it->second; | 1000 const NewWindowInfo& new_window_info = it->second; |
1004 if (new_window_info.changed || !web_contents()->HasOpener()) | 1001 if (new_window_info.changed || !web_contents()->HasOpener()) |
1005 NavigateGuest(new_window_info.url.spec(), false /* force_navigation */); | 1002 NavigateGuest(new_window_info.url.spec(), false /* force_navigation */); |
1006 | 1003 |
1007 // Once a new guest is attached to the DOM of the embedder page, then the | 1004 // Once a new guest is attached to the DOM of the embedder page, then the |
1008 // lifetime of the new guest is no longer managed by the opener guest. | 1005 // lifetime of the new guest is no longer managed by the opener guest. |
1009 GetOpener()->pending_new_windows_.erase(this); | 1006 GetOpener()->pending_new_windows_.erase(this); |
1010 | 1007 |
1011 is_pending_new_window = true; | 1008 is_pending_new_window = true; |
(...skipping 18 matching lines...) Expand all Loading... |
1030 | 1027 |
1031 void WebViewGuest::SetName(const std::string& name) { | 1028 void WebViewGuest::SetName(const std::string& name) { |
1032 if (name_ == name) | 1029 if (name_ == name) |
1033 return; | 1030 return; |
1034 name_ = name; | 1031 name_ = name; |
1035 | 1032 |
1036 Send(new ExtensionMsg_SetFrameName(routing_id(), name_)); | 1033 Send(new ExtensionMsg_SetFrameName(routing_id(), name_)); |
1037 } | 1034 } |
1038 | 1035 |
1039 void WebViewGuest::SetZoom(double zoom_factor) { | 1036 void WebViewGuest::SetZoom(double zoom_factor) { |
1040 ui_zoom::ZoomController* zoom_controller = | 1037 auto zoom_controller = |
1041 ui_zoom::ZoomController::FromWebContents(web_contents()); | 1038 ui_zoom::ZoomController::FromWebContents(web_contents()); |
1042 DCHECK(zoom_controller); | 1039 DCHECK(zoom_controller); |
1043 double zoom_level = content::ZoomFactorToZoomLevel(zoom_factor); | 1040 double zoom_level = content::ZoomFactorToZoomLevel(zoom_factor); |
1044 zoom_controller->SetZoomLevel(zoom_level); | 1041 zoom_controller->SetZoomLevel(zoom_level); |
1045 | 1042 |
1046 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 1043 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
1047 args->SetDouble(webview::kOldZoomFactor, current_zoom_factor_); | 1044 args->SetDouble(webview::kOldZoomFactor, current_zoom_factor_); |
1048 args->SetDouble(webview::kNewZoomFactor, zoom_factor); | 1045 args->SetDouble(webview::kNewZoomFactor, zoom_factor); |
1049 DispatchEventToEmbedder( | 1046 DispatchEventToEmbedder( |
1050 new GuestViewBase::Event(webview::kEventZoomChange, args.Pass())); | 1047 new GuestViewBase::Event(webview::kEventZoomChange, args.Pass())); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1130 content::WebContents* WebViewGuest::OpenURLFromTab( | 1127 content::WebContents* WebViewGuest::OpenURLFromTab( |
1131 content::WebContents* source, | 1128 content::WebContents* source, |
1132 const content::OpenURLParams& params) { | 1129 const content::OpenURLParams& params) { |
1133 // If the guest wishes to navigate away prior to attachment then we save the | 1130 // If the guest wishes to navigate away prior to attachment then we save the |
1134 // navigation to perform upon attachment. Navigation initializes a lot of | 1131 // navigation to perform upon attachment. Navigation initializes a lot of |
1135 // state that assumes an embedder exists, such as RenderWidgetHostViewGuest. | 1132 // state that assumes an embedder exists, such as RenderWidgetHostViewGuest. |
1136 // Navigation also resumes resource loading which we don't want to allow | 1133 // Navigation also resumes resource loading which we don't want to allow |
1137 // until attachment. | 1134 // until attachment. |
1138 if (!attached()) { | 1135 if (!attached()) { |
1139 WebViewGuest* opener = GetOpener(); | 1136 WebViewGuest* opener = GetOpener(); |
1140 PendingWindowMap::iterator it = | 1137 auto it = opener->pending_new_windows_.find(this); |
1141 opener->pending_new_windows_.find(this); | |
1142 if (it == opener->pending_new_windows_.end()) | 1138 if (it == opener->pending_new_windows_.end()) |
1143 return NULL; | 1139 return NULL; |
1144 const NewWindowInfo& info = it->second; | 1140 const NewWindowInfo& info = it->second; |
1145 NewWindowInfo new_window_info(params.url, info.name); | 1141 NewWindowInfo new_window_info(params.url, info.name); |
1146 new_window_info.changed = new_window_info.url != info.url; | 1142 new_window_info.changed = new_window_info.url != info.url; |
1147 it->second = new_window_info; | 1143 it->second = new_window_info; |
1148 return NULL; | 1144 return NULL; |
1149 } | 1145 } |
1150 if (params.disposition == CURRENT_TAB) { | 1146 if (params.disposition == CURRENT_TAB) { |
1151 // This can happen for cross-site redirects. | 1147 // This can happen for cross-site redirects. |
1152 LoadURLWithParams(params.url, params.referrer, params.transition, source); | 1148 LoadURLWithParams(params.url, params.referrer, params.transition, source); |
1153 return source; | 1149 return source; |
1154 } | 1150 } |
1155 | 1151 |
1156 CreateNewGuestWebViewWindow(params); | 1152 CreateNewGuestWebViewWindow(params); |
1157 return NULL; | 1153 return NULL; |
1158 } | 1154 } |
1159 | 1155 |
1160 void WebViewGuest::WebContentsCreated(WebContents* source_contents, | 1156 void WebViewGuest::WebContentsCreated(WebContents* source_contents, |
1161 int opener_render_frame_id, | 1157 int opener_render_frame_id, |
1162 const base::string16& frame_name, | 1158 const base::string16& frame_name, |
1163 const GURL& target_url, | 1159 const GURL& target_url, |
1164 content::WebContents* new_contents) { | 1160 content::WebContents* new_contents) { |
1165 WebViewGuest* guest = WebViewGuest::FromWebContents(new_contents); | 1161 auto guest = WebViewGuest::FromWebContents(new_contents); |
1166 CHECK(guest); | 1162 CHECK(guest); |
1167 guest->SetOpener(this); | 1163 guest->SetOpener(this); |
1168 std::string guest_name = base::UTF16ToUTF8(frame_name); | 1164 std::string guest_name = base::UTF16ToUTF8(frame_name); |
1169 guest->name_ = guest_name; | 1165 guest->name_ = guest_name; |
1170 pending_new_windows_.insert( | 1166 pending_new_windows_.insert( |
1171 std::make_pair(guest, NewWindowInfo(target_url, guest_name))); | 1167 std::make_pair(guest, NewWindowInfo(target_url, guest_name))); |
1172 } | 1168 } |
1173 | 1169 |
1174 void WebViewGuest::LoadURLWithParams(const GURL& url, | 1170 void WebViewGuest::LoadURLWithParams(const GURL& url, |
1175 const content::Referrer& referrer, | 1171 const content::Referrer& referrer, |
1176 ui::PageTransition transition_type, | 1172 ui::PageTransition transition_type, |
1177 content::WebContents* web_contents) { | 1173 content::WebContents* web_contents) { |
1178 content::NavigationController::LoadURLParams load_url_params(url); | 1174 content::NavigationController::LoadURLParams load_url_params(url); |
1179 load_url_params.referrer = referrer; | 1175 load_url_params.referrer = referrer; |
1180 load_url_params.transition_type = transition_type; | 1176 load_url_params.transition_type = transition_type; |
1181 load_url_params.extra_headers = std::string(); | 1177 load_url_params.extra_headers = std::string(); |
1182 if (is_overriding_user_agent_) { | 1178 if (is_overriding_user_agent_) { |
1183 load_url_params.override_user_agent = | 1179 load_url_params.override_user_agent = |
1184 content::NavigationController::UA_OVERRIDE_TRUE; | 1180 content::NavigationController::UA_OVERRIDE_TRUE; |
1185 } | 1181 } |
1186 web_contents->GetController().LoadURLWithParams(load_url_params); | 1182 web_contents->GetController().LoadURLWithParams(load_url_params); |
1187 } | 1183 } |
1188 | 1184 |
1189 void WebViewGuest::RequestNewWindowPermission( | 1185 void WebViewGuest::RequestNewWindowPermission( |
1190 WindowOpenDisposition disposition, | 1186 WindowOpenDisposition disposition, |
1191 const gfx::Rect& initial_bounds, | 1187 const gfx::Rect& initial_bounds, |
1192 bool user_gesture, | 1188 bool user_gesture, |
1193 content::WebContents* new_contents) { | 1189 content::WebContents* new_contents) { |
1194 WebViewGuest* guest = WebViewGuest::FromWebContents(new_contents); | 1190 auto guest = WebViewGuest::FromWebContents(new_contents); |
1195 if (!guest) | 1191 if (!guest) |
1196 return; | 1192 return; |
1197 PendingWindowMap::iterator it = pending_new_windows_.find(guest); | 1193 auto it = pending_new_windows_.find(guest); |
1198 if (it == pending_new_windows_.end()) | 1194 if (it == pending_new_windows_.end()) |
1199 return; | 1195 return; |
1200 const NewWindowInfo& new_window_info = it->second; | 1196 const NewWindowInfo& new_window_info = it->second; |
1201 | 1197 |
1202 // Retrieve the opener partition info if we have it. | 1198 // Retrieve the opener partition info if we have it. |
1203 const GURL& site_url = new_contents->GetSiteInstance()->GetSiteURL(); | 1199 const GURL& site_url = new_contents->GetSiteInstance()->GetSiteURL(); |
1204 std::string storage_partition_id = GetStoragePartitionIdFromSiteURL(site_url); | 1200 std::string storage_partition_id = GetStoragePartitionIdFromSiteURL(site_url); |
1205 | 1201 |
1206 base::DictionaryValue request_info; | 1202 base::DictionaryValue request_info; |
1207 request_info.SetInteger(webview::kInitialHeight, initial_bounds.height()); | 1203 request_info.SetInteger(webview::kInitialHeight, initial_bounds.height()); |
(...skipping 27 matching lines...) Expand all Loading... |
1235 GURL default_url(base::StringPrintf("%s://%s/", | 1231 GURL default_url(base::StringPrintf("%s://%s/", |
1236 kExtensionScheme, | 1232 kExtensionScheme, |
1237 owner_extension_id().c_str())); | 1233 owner_extension_id().c_str())); |
1238 return default_url.Resolve(src); | 1234 return default_url.Resolve(src); |
1239 } | 1235 } |
1240 | 1236 |
1241 void WebViewGuest::OnWebViewNewWindowResponse( | 1237 void WebViewGuest::OnWebViewNewWindowResponse( |
1242 int new_window_instance_id, | 1238 int new_window_instance_id, |
1243 bool allow, | 1239 bool allow, |
1244 const std::string& user_input) { | 1240 const std::string& user_input) { |
1245 WebViewGuest* guest = | 1241 auto guest = |
1246 WebViewGuest::From(owner_web_contents()->GetRenderProcessHost()->GetID(), | 1242 WebViewGuest::From(owner_web_contents()->GetRenderProcessHost()->GetID(), |
1247 new_window_instance_id); | 1243 new_window_instance_id); |
1248 if (!guest) | 1244 if (!guest) |
1249 return; | 1245 return; |
1250 | 1246 |
1251 if (!allow) | 1247 if (!allow) |
1252 guest->Destroy(); | 1248 guest->Destroy(); |
1253 } | 1249 } |
1254 | 1250 |
1255 } // namespace extensions | 1251 } // namespace extensions |
OLD | NEW |