Chromium Code Reviews| Index: chrome/browser/webview/webview_guest.cc |
| diff --git a/chrome/browser/webview/webview_guest.cc b/chrome/browser/webview/webview_guest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d61ebd47ac41800f1ce82a9125febce75478b268 |
| --- /dev/null |
| +++ b/chrome/browser/webview/webview_guest.cc |
| @@ -0,0 +1,81 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/webview/webview_guest.h" |
| + |
| +#include "base/lazy_instance.h" |
| +#include "chrome/browser/extensions/api/web_request/web_request_api.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/site_instance.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +using content::WebContents; |
| + |
| +namespace chrome { |
| + |
| +namespace { |
| + |
| +typedef std::map<int, WebViewGuest*> WebViewGuestMap; |
| +typedef std::map<Profile*, WebViewGuestMap> WebViewProfileMap; |
| +static base::LazyInstance<WebViewProfileMap> webview_profile_map = |
| + LAZY_INSTANCE_INITIALIZER; |
| + |
| +void RemoveWebViewEventListenersOnIOThread( |
| + Profile* profile, |
|
sky
2013/05/23 15:41:11
What if the profile is deleted between the time yo
Fady Samuel
2013/05/24 03:13:47
profile is used as a key to webview_profile_map bu
sky
2013/05/24 15:58:03
How about making this take a void* then.
|
| + const std::string& extension_id, |
| + int embedder_process_id, |
| + int web_view_instance_id) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| + ExtensionWebRequestEventRouter::GetInstance()->RemoveWebViewEventListeners( |
| + profile, extension_id, embedder_process_id, web_view_instance_id); |
| +} |
| + |
| +} // namespace |
| + |
| +WebViewGuest::WebViewGuest(WebContents* guest_web_contents, |
| + WebContents* embedder_web_contents, |
| + const std::string& extension_id) |
| + : WebContentsObserver(guest_web_contents), |
| + embedder_web_contents_(embedder_web_contents), |
| + extension_id_(extension_id), |
| + embedder_render_process_id_( |
| + embedder_web_contents->GetRenderProcessHost()->GetID()), |
| + profile_(static_cast<Profile*>(guest_web_contents->GetBrowserContext())), |
| + instance_id_(guest_web_contents->GetEmbeddedInstanceID()) { |
| + webview_profile_map.Get()[profile_].insert( |
| + std::make_pair(instance_id_, this)); |
| +} |
| + |
| +WebViewGuest::~WebViewGuest() { |
|
sky
2013/05/23 15:41:11
Make order match header.
Fady Samuel
2013/05/24 03:13:47
Done.
|
| + webview_profile_map.Get()[profile_].erase(instance_id_); |
| + if (webview_profile_map.Get()[profile_].empty()) |
| + webview_profile_map.Get().erase(profile_); |
| +} |
| + |
| +// static |
| +WebViewGuest* WebViewGuest::From(Profile* profile, int instance_id) { |
| + WebViewProfileMap* profile_map = webview_profile_map.Pointer(); |
| + WebViewProfileMap::iterator it = profile_map->find(profile); |
| + if (it == profile_map->end()) |
| + return NULL; |
| + const WebViewGuestMap& guest_map = it->second; |
| + WebViewGuestMap::const_iterator guest_it = guest_map.find(instance_id); |
| + return guest_it == guest_map.end() ? NULL : guest_it->second; |
| +} |
| + |
| +void WebViewGuest::WebContentsDestroyed(WebContents* web_contents) { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind( |
| + &RemoveWebViewEventListenersOnIOThread, |
| + profile_, extension_id_, |
| + embedder_render_process_id_, |
| + instance_id_)); |
| + delete this; |
| +} |
| + |
| +} // namespace chrome |