Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: extensions/browser/guest_view/web_view/web_view_guest.cc

Issue 1181263007: WebView context menu cleanup. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: A new strategy. Rebased. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <utility>
8
7 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
8 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
10 #include "components/browsing_data/storage_partition_http_cache_data_remover.h" 12 #include "components/browsing_data/storage_partition_http_cache_data_remover.h"
11 #include "components/guest_view/browser/guest_view_event.h" 13 #include "components/guest_view/browser/guest_view_event.h"
12 #include "components/guest_view/browser/guest_view_manager.h" 14 #include "components/guest_view/browser/guest_view_manager.h"
13 #include "components/guest_view/common/guest_view_constants.h" 15 #include "components/guest_view/common/guest_view_constants.h"
14 #include "components/web_cache/browser/web_cache_manager.h" 16 #include "components/web_cache/browser/web_cache_manager.h"
15 #include "content/public/browser/browser_context.h" 17 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
(...skipping 16 matching lines...) Expand all
33 #include "content/public/common/media_stream_request.h" 35 #include "content/public/common/media_stream_request.h"
34 #include "content/public/common/page_zoom.h" 36 #include "content/public/common/page_zoom.h"
35 #include "content/public/common/result_codes.h" 37 #include "content/public/common/result_codes.h"
36 #include "content/public/common/stop_find_action.h" 38 #include "content/public/common/stop_find_action.h"
37 #include "content/public/common/url_constants.h" 39 #include "content/public/common/url_constants.h"
38 #include "extensions/browser/api/declarative/rules_registry_service.h" 40 #include "extensions/browser/api/declarative/rules_registry_service.h"
39 #include "extensions/browser/api/extensions_api_client.h" 41 #include "extensions/browser/api/extensions_api_client.h"
40 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h" 42 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h"
41 #include "extensions/browser/api/web_request/web_request_api.h" 43 #include "extensions/browser/api/web_request/web_request_api.h"
42 #include "extensions/browser/extension_system.h" 44 #include "extensions/browser/extension_system.h"
45 #include "extensions/browser/extensions_browser_client.h"
43 #include "extensions/browser/guest_view/web_view/web_view_constants.h" 46 #include "extensions/browser/guest_view/web_view/web_view_constants.h"
44 #include "extensions/browser/guest_view/web_view/web_view_content_script_manager .h" 47 #include "extensions/browser/guest_view/web_view/web_view_content_script_manager .h"
45 #include "extensions/browser/guest_view/web_view/web_view_permission_helper.h" 48 #include "extensions/browser/guest_view/web_view/web_view_permission_helper.h"
46 #include "extensions/browser/guest_view/web_view/web_view_permission_types.h" 49 #include "extensions/browser/guest_view/web_view/web_view_permission_types.h"
47 #include "extensions/browser/guest_view/web_view/web_view_renderer_state.h" 50 #include "extensions/browser/guest_view/web_view/web_view_renderer_state.h"
48 #include "extensions/common/constants.h" 51 #include "extensions/common/constants.h"
49 #include "extensions/common/extension_messages.h" 52 #include "extensions/common/extension_messages.h"
50 #include "extensions/strings/grit/extensions_strings.h" 53 #include "extensions/strings/grit/extensions_strings.h"
51 #include "ipc/ipc_message_macros.h" 54 #include "ipc/ipc_message_macros.h"
52 #include "net/base/escape.h" 55 #include "net/base/escape.h"
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 185
183 double ConvertZoomLevelToZoomFactor(double zoom_level) { 186 double ConvertZoomLevelToZoomFactor(double zoom_level) {
184 double zoom_factor = content::ZoomLevelToZoomFactor(zoom_level); 187 double zoom_factor = content::ZoomLevelToZoomFactor(zoom_level);
185 // Because the conversion from zoom level to zoom factor isn't perfect, the 188 // Because the conversion from zoom level to zoom factor isn't perfect, the
186 // resulting zoom factor is rounded to the nearest 6th decimal place. 189 // resulting zoom factor is rounded to the nearest 6th decimal place.
187 zoom_factor = round(zoom_factor * 1000000) / 1000000; 190 zoom_factor = round(zoom_factor * 1000000) / 1000000;
188 return zoom_factor; 191 return zoom_factor;
189 } 192 }
190 193
191 using WebViewKey = std::pair<int, int>; 194 using WebViewKey = std::pair<int, int>;
195
192 using WebViewKeyToIDMap = std::map<WebViewKey, int>; 196 using WebViewKeyToIDMap = std::map<WebViewKey, int>;
193 static base::LazyInstance<WebViewKeyToIDMap> web_view_key_to_id_map = 197 static base::LazyInstance<WebViewKeyToIDMap> web_view_key_to_id_map =
194 LAZY_INSTANCE_INITIALIZER; 198 LAZY_INSTANCE_INITIALIZER;
195 199
200 using WebViewKeyToExtensionIDMap = std::map<WebViewKey, std::string>;
201 static base::LazyInstance<WebViewKeyToExtensionIDMap>
Devlin 2015/07/06 17:46:18 Storing this as global state seems unnecessary (an
202 web_view_key_to_extension_id_map = LAZY_INSTANCE_INITIALIZER;
203
196 } // namespace 204 } // namespace
197 205
198 // static 206 // static
199 void WebViewGuest::CleanUp(int embedder_process_id, int view_instance_id) { 207 void WebViewGuest::CleanUp(int embedder_process_id, int view_instance_id) {
200 GuestViewBase::CleanUp(embedder_process_id, view_instance_id); 208 GuestViewBase::CleanUp(embedder_process_id, view_instance_id);
201 209
202 auto rph = content::RenderProcessHost::FromID(embedder_process_id); 210 auto rph = content::RenderProcessHost::FromID(embedder_process_id);
203 // TODO(paulmeyer): It should be impossible for rph to be nullptr here, but 211 // TODO(paulmeyer): It should be impossible for rph to be nullptr here, but
204 // this check is needed here for now as there seems to be occasional crashes 212 // this check is needed here for now as there seems to be occasional crashes
205 // because of this (http//crbug.com/499438). This should be removed once the 213 // because of this (http//crbug.com/499438). This should be removed once the
(...skipping 21 matching lines...) Expand all
227 FROM_HERE, 235 FROM_HERE,
228 base::Bind( 236 base::Bind(
229 &RemoveWebViewEventListenersOnIOThread, 237 &RemoveWebViewEventListenersOnIOThread,
230 browser_context, 238 browser_context,
231 embedder_process_id, 239 embedder_process_id,
232 view_instance_id)); 240 view_instance_id));
233 241
234 // Clean up content scripts for the WebView. 242 // Clean up content scripts for the WebView.
235 auto csm = WebViewContentScriptManager::Get(browser_context); 243 auto csm = WebViewContentScriptManager::Get(browser_context);
236 csm->RemoveAllContentScriptsForWebView(embedder_process_id, view_instance_id); 244 csm->RemoveAllContentScriptsForWebView(embedder_process_id, view_instance_id);
245
246 // Allow an extensions browser client to potentially perform more cleanup.
247 ExtensionsBrowserClient::Get()->CleanUpWebView(embedder_process_id,
248 view_instance_id);
249
250 // Remove this WebView's entry in |web_view_key_to_extension_id_map|.
251 web_view_key_to_extension_id_map.Get().erase(key);
237 } 252 }
238 253
239 // static 254 // static
240 GuestViewBase* WebViewGuest::Create(content::WebContents* owner_web_contents) { 255 GuestViewBase* WebViewGuest::Create(content::WebContents* owner_web_contents) {
241 return new WebViewGuest(owner_web_contents); 256 return new WebViewGuest(owner_web_contents);
242 } 257 }
243 258
244 // static 259 // static
245 bool WebViewGuest::GetGuestPartitionConfigForSite( 260 bool WebViewGuest::GetGuestPartitionConfigForSite(
246 const GURL& site, 261 const GURL& site,
(...skipping 21 matching lines...) Expand all
268 const char WebViewGuest::Type[] = "webview"; 283 const char WebViewGuest::Type[] = "webview";
269 284
270 // static 285 // static
271 int WebViewGuest::GetOrGenerateRulesRegistryID( 286 int WebViewGuest::GetOrGenerateRulesRegistryID(
272 int embedder_process_id, 287 int embedder_process_id,
273 int webview_instance_id) { 288 int webview_instance_id) {
274 bool is_web_view = embedder_process_id && webview_instance_id; 289 bool is_web_view = embedder_process_id && webview_instance_id;
275 if (!is_web_view) 290 if (!is_web_view)
276 return RulesRegistryService::kDefaultRulesRegistryID; 291 return RulesRegistryService::kDefaultRulesRegistryID;
277 292
278 WebViewKey key = std::make_pair(embedder_process_id, webview_instance_id); 293 WebViewKey key(embedder_process_id, webview_instance_id);
279 auto it = web_view_key_to_id_map.Get().find(key); 294 auto it = web_view_key_to_id_map.Get().find(key);
280 if (it != web_view_key_to_id_map.Get().end()) 295 if (it != web_view_key_to_id_map.Get().end())
281 return it->second; 296 return it->second;
282 297
283 auto rph = content::RenderProcessHost::FromID(embedder_process_id); 298 auto rph = content::RenderProcessHost::FromID(embedder_process_id);
284 int rules_registry_id = 299 int rules_registry_id =
285 RulesRegistryService::Get(rph->GetBrowserContext())-> 300 RulesRegistryService::Get(rph->GetBrowserContext())->
286 GetNextRulesRegistryID(); 301 GetNextRulesRegistryID();
287 web_view_key_to_id_map.Get()[key] = rules_registry_id; 302 web_view_key_to_id_map.Get()[key] = rules_registry_id;
288 return rules_registry_id; 303 return rules_registry_id;
289 } 304 }
290 305
291 // static 306 // static
292 int WebViewGuest::GetViewInstanceId(WebContents* contents) { 307 int WebViewGuest::GetViewInstanceId(WebContents* contents) {
293 auto guest = FromWebContents(contents); 308 auto guest = FromWebContents(contents);
294 if (!guest) 309 if (!guest)
295 return guest_view::kInstanceIDNone; 310 return guest_view::kInstanceIDNone;
296 311
297 return guest->view_instance_id(); 312 return guest->view_instance_id();
298 } 313 }
299 314
315 // static
316 std::string WebViewGuest::GetExtensionID(int embedder_process_id,
317 int view_instance_id) {
318 WebViewKey key(embedder_process_id, view_instance_id);
319 auto it = web_view_key_to_extension_id_map.Get().find(key);
320
321 if (it == web_view_key_to_extension_id_map.Get().end())
322 return "";
323
324 return it->second;
325 }
326
327 // static
328 void WebViewGuest::RegisterExtensionID(const std::string& extension_id,
329 int embedder_process_id,
330 int view_instance_id) {
331 WebViewKey key(embedder_process_id, view_instance_id);
332 web_view_key_to_extension_id_map.Get()[key] = extension_id;
333 }
334
300 bool WebViewGuest::CanRunInDetachedState() const { 335 bool WebViewGuest::CanRunInDetachedState() const {
301 return true; 336 return true;
302 } 337 }
303 338
304 void WebViewGuest::CreateWebContents( 339 void WebViewGuest::CreateWebContents(
305 const base::DictionaryValue& create_params, 340 const base::DictionaryValue& create_params,
306 const WebContentsCreatedCallback& callback) { 341 const WebContentsCreatedCallback& callback) {
307 content::RenderProcessHost* owner_render_process_host = 342 content::RenderProcessHost* owner_render_process_host =
308 owner_web_contents()->GetRenderProcessHost(); 343 owner_web_contents()->GetRenderProcessHost();
309 std::string storage_partition_id; 344 std::string storage_partition_id;
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 462
428 const char* WebViewGuest::GetAPINamespace() const { 463 const char* WebViewGuest::GetAPINamespace() const {
429 return webview::kAPINamespace; 464 return webview::kAPINamespace;
430 } 465 }
431 466
432 int WebViewGuest::GetTaskPrefix() const { 467 int WebViewGuest::GetTaskPrefix() const {
433 return IDS_EXTENSION_TASK_MANAGER_WEBVIEW_TAG_PREFIX; 468 return IDS_EXTENSION_TASK_MANAGER_WEBVIEW_TAG_PREFIX;
434 } 469 }
435 470
436 void WebViewGuest::GuestDestroyed() { 471 void WebViewGuest::GuestDestroyed() {
437 // Clean up custom context menu items for this guest.
438 if (web_view_guest_delegate_)
439 web_view_guest_delegate_->OnGuestDestroyed();
440 RemoveWebViewStateFromIOThread(web_contents()); 472 RemoveWebViewStateFromIOThread(web_contents());
441 } 473 }
442 474
443 void WebViewGuest::GuestReady() { 475 void WebViewGuest::GuestReady() {
444 // The guest RenderView should always live in an isolated guest process. 476 // The guest RenderView should always live in an isolated guest process.
445 CHECK(web_contents()->GetRenderProcessHost()->IsForGuestsOnly()); 477 CHECK(web_contents()->GetRenderProcessHost()->IsForGuestsOnly());
446 Send(new ExtensionMsg_SetFrameName(web_contents()->GetRoutingID(), name_)); 478 Send(new ExtensionMsg_SetFrameName(web_contents()->GetRoutingID(), name_));
447 479
448 // We don't want to accidentally set the opacity of an interstitial page. 480 // We don't want to accidentally set the opacity of an interstitial page.
449 // WebContents::GetRenderWidgetHostView will return the RWHV of an 481 // WebContents::GetRenderWidgetHostView will return the RWHV of an
(...skipping 1012 matching lines...) Expand 10 before | Expand all | Expand 10 after
1462 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); 1494 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
1463 DispatchEventToView( 1495 DispatchEventToView(
1464 new GuestViewEvent(webview::kEventExitFullscreen, args.Pass())); 1496 new GuestViewEvent(webview::kEventExitFullscreen, args.Pass()));
1465 } 1497 }
1466 // Since we changed fullscreen state, sending a Resize message ensures that 1498 // Since we changed fullscreen state, sending a Resize message ensures that
1467 // renderer/ sees the change. 1499 // renderer/ sees the change.
1468 web_contents()->GetRenderViewHost()->WasResized(); 1500 web_contents()->GetRenderViewHost()->WasResized();
1469 } 1501 }
1470 1502
1471 } // namespace extensions 1503 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698