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

Unified Diff: extensions/browser/guest_view/web_view/web_view_guest.cc

Issue 885493007: Refactoring: de-couple Extensions from "script injection System" [render side] : 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: extensions/browser/guest_view/web_view/web_view_guest.cc
diff --git a/extensions/browser/guest_view/web_view/web_view_guest.cc b/extensions/browser/guest_view/web_view/web_view_guest.cc
index 717770fe44d40121aa73d60011e47262f7f34473..e72c03a1a22e7f9595ab0f5cb0f44a96b12e73c7 100644
--- a/extensions/browser/guest_view/web_view/web_view_guest.cc
+++ b/extensions/browser/guest_view/web_view/web_view_guest.cc
@@ -4,6 +4,7 @@
#include "extensions/browser/guest_view/web_view/web_view_guest.h"
+#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
@@ -59,6 +60,17 @@ namespace extensions {
namespace {
+using GetNextUniqueIDFunction = base::Callback<int(void)>;
Devlin 2015/02/11 00:00:48 remove
Xi Han 2015/02/11 16:00:23 Done.
+
+using WebViewKey = std::pair<int, int>;
+using WebViewKeyToIDMap = std::map<WebViewKey, int>;
+base::LazyInstance<WebViewKeyToIDMap> web_view_key_to_id_map =
+ LAZY_INSTANCE_INITIALIZER;
+base::LazyInstance<WebViewKeyToIDMap> unique_instance_id_map =
+ LAZY_INSTANCE_INITIALIZER;
+
+int current_instance_id = 0;
+
std::string WindowOpenDispositionToString(
WindowOpenDisposition window_open_disposition) {
switch (window_open_disposition) {
@@ -100,6 +112,20 @@ static std::string TerminationStatusToString(base::TerminationStatus status) {
return "unknown";
}
+bool GetIDFromMap(const WebViewKey& key,
Devlin 2015/02/11 00:00:48 Add a comment - something like "Looks up the id fo
Xi Han 2015/02/11 16:00:23 Thanks:)
+ const WebViewKeyToIDMap& map,
Devlin 2015/02/11 00:00:48 indentation
Xi Han 2015/02/11 16:00:23 Done.
+ int default_id,
+ int* result) {
+ WebViewKeyToIDMap::const_iterator iter = map.end();
+ if (!key.first || !key.second)
+ *result = default_id;
+ else if ((iter = map.find(key)) != map.end())
+ *result = iter->second;
+ else
+ return false;
+ return true;
+}
+
std::string GetStoragePartitionIdFromSiteURL(const GURL& site_url) {
const std::string& partition_id = site_url.query();
bool persist_storage = site_url.path().find("persist") != std::string::npos;
@@ -181,30 +207,35 @@ bool WebViewGuest::GetGuestPartitionConfigForSite(
// static
const char WebViewGuest::Type[] = "webview";
-using WebViewKey = std::pair<int, int>;
-using WebViewKeyToIDMap = std::map<WebViewKey, int>;
-static base::LazyInstance<WebViewKeyToIDMap> web_view_key_to_id_map =
- LAZY_INSTANCE_INITIALIZER;
-
// static
int WebViewGuest::GetOrGenerateRulesRegistryID(
int embedder_process_id,
- int webview_instance_id) {
- bool is_web_view = embedder_process_id && webview_instance_id;
- if (!is_web_view)
- return RulesRegistryService::kDefaultRulesRegistryID;
-
- WebViewKey key = std::make_pair(embedder_process_id, webview_instance_id);
- auto it = web_view_key_to_id_map.Get().find(key);
- if (it != web_view_key_to_id_map.Get().end())
- return it->second;
+ int web_view_instance_id) {
+ WebViewKey key(embedder_process_id, web_view_instance_id);
+ WebViewKeyToIDMap& map = web_view_key_to_id_map.Get();
+ int id = 0;
+ if (!GetIDFromMap(
+ key, map, RulesRegistryService::kDefaultRulesRegistryID, &id)) {
+ auto rph = content::RenderProcessHost::FromID(embedder_process_id);
+ id = RulesRegistryService::Get(rph->GetBrowserContext())->
+ GetNextRulesRegistryID();
+ map[key] = id;
+ }
+ return id;
+}
- auto rph = content::RenderProcessHost::FromID(embedder_process_id);
- int rules_registry_id =
- RulesRegistryService::Get(rph->GetBrowserContext())->
- GetNextRulesRegistryID();
- web_view_key_to_id_map.Get()[key] = rules_registry_id;
- return rules_registry_id;
+// static
+int WebViewGuest::GetOrGenerateUniqueInstanceID(
+ int embedder_process_id,
+ int guest_instance_id ) {
+ WebViewKey key(embedder_process_id, guest_instance_id);
+ WebViewKeyToIDMap& map = unique_instance_id_map.Get();
+ int id = 0;
+ if (!GetIDFromMap(key, map, HostID::kDefaultInstanceId, &id)) {
+ id = ++current_instance_id;
+ map[key] = id;
+ }
+ return id;
}
// static
@@ -294,6 +325,10 @@ void WebViewGuest::DidInitialize(const base::DictionaryValue& create_params) {
owner_web_contents()->GetRenderProcessHost()->GetID(),
view_instance_id());
+ GetOrGenerateUniqueInstanceID(
+ owner_web_contents()->GetRenderProcessHost()->GetID(),
+ guest_instance_id());
+
// We must install the mapping from guests to WebViews prior to resuming
// suspended resource loads so that the WebRequest API will catch resource
// requests.
@@ -321,6 +356,7 @@ void WebViewGuest::EmbedderWillBeDestroyed() {
WebViewKey key(owner_web_contents()->GetRenderProcessHost()->GetID(),
view_instance_id());
web_view_key_to_id_map.Get().erase(key);
+ unique_instance_id_map.Get().erase(key);
content::BrowserThread::PostTask(
content::BrowserThread::IO,

Powered by Google App Engine
This is Rietveld 408576698