| 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/renderer/script_context_set.h" | 5 #include "extensions/renderer/script_context_set.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "content/public/common/url_constants.h" | 8 #include "content/public/common/url_constants.h" |
| 9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
| 10 #include "extensions/common/extension.h" | 10 #include "extensions/common/extension.h" |
| 11 #include "extensions/renderer/extension_groups.h" | 11 #include "extensions/renderer/extension_groups.h" |
| 12 #include "extensions/renderer/script_context.h" | 12 #include "extensions/renderer/script_context.h" |
| 13 #include "extensions/renderer/script_injection.h" | 13 #include "extensions/renderer/script_injection.h" |
| 14 #include "third_party/WebKit/public/web/WebDocument.h" | 14 #include "third_party/WebKit/public/web/WebDocument.h" |
| 15 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 15 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 16 #include "v8/include/v8.h" | 16 #include "v8/include/v8.h" |
| 17 | 17 |
| 18 namespace extensions { | 18 namespace extensions { |
| 19 | 19 |
| 20 namespace { |
| 21 // There is only ever one instance of the ScriptContextSet. |
| 22 ScriptContextSet* g_context_set = nullptr; |
| 23 } |
| 24 |
| 20 ScriptContextSet::ScriptContextSet(ExtensionSet* extensions, | 25 ScriptContextSet::ScriptContextSet(ExtensionSet* extensions, |
| 21 ExtensionIdSet* active_extension_ids) | 26 ExtensionIdSet* active_extension_ids) |
| 22 : extensions_(extensions), active_extension_ids_(active_extension_ids) { | 27 : extensions_(extensions), active_extension_ids_(active_extension_ids) { |
| 28 DCHECK(!g_context_set); |
| 29 g_context_set = this; |
| 23 } | 30 } |
| 24 | 31 |
| 25 ScriptContextSet::~ScriptContextSet() { | 32 ScriptContextSet::~ScriptContextSet() { |
| 33 g_context_set = nullptr; |
| 26 } | 34 } |
| 27 | 35 |
| 28 ScriptContext* ScriptContextSet::Register( | 36 ScriptContext* ScriptContextSet::Register( |
| 29 blink::WebLocalFrame* frame, | 37 blink::WebLocalFrame* frame, |
| 30 const v8::Local<v8::Context>& v8_context, | 38 const v8::Local<v8::Context>& v8_context, |
| 31 int extension_group, | 39 int extension_group, |
| 32 int world_id) { | 40 int world_id) { |
| 33 const Extension* extension = | 41 const Extension* extension = |
| 34 GetExtensionFromFrameAndWorld(frame, world_id, false); | 42 GetExtensionFromFrameAndWorld(frame, world_id, false); |
| 35 const Extension* effective_extension = | 43 const Extension* effective_extension = |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 | 80 |
| 73 ScriptContext* ScriptContextSet::GetByV8Context( | 81 ScriptContext* ScriptContextSet::GetByV8Context( |
| 74 const v8::Local<v8::Context>& v8_context) const { | 82 const v8::Local<v8::Context>& v8_context) const { |
| 75 for (ScriptContext* script_context : contexts_) { | 83 for (ScriptContext* script_context : contexts_) { |
| 76 if (script_context->v8_context() == v8_context) | 84 if (script_context->v8_context() == v8_context) |
| 77 return script_context; | 85 return script_context; |
| 78 } | 86 } |
| 79 return nullptr; | 87 return nullptr; |
| 80 } | 88 } |
| 81 | 89 |
| 90 ScriptContext* ScriptContextSet::GetContextByV8Context( |
| 91 const v8::Local<v8::Context>& v8_context) { |
| 92 // g_context_set can be null in unittests. |
| 93 return g_context_set ? g_context_set->GetByV8Context(v8_context) : nullptr; |
| 94 } |
| 95 |
| 82 void ScriptContextSet::ForEach( | 96 void ScriptContextSet::ForEach( |
| 83 const std::string& extension_id, | 97 const std::string& extension_id, |
| 84 content::RenderView* render_view, | 98 content::RenderView* render_view, |
| 85 const base::Callback<void(ScriptContext*)>& callback) const { | 99 const base::Callback<void(ScriptContext*)>& callback) const { |
| 86 // We copy the context list, because calling into javascript may modify it | 100 // We copy the context list, because calling into javascript may modify it |
| 87 // out from under us. | 101 // out from under us. |
| 88 std::set<ScriptContext*> contexts_copy = contexts_; | 102 std::set<ScriptContext*> contexts_copy = contexts_; |
| 89 | 103 |
| 90 for (ScriptContext* context : contexts_copy) { | 104 for (ScriptContext* context : contexts_copy) { |
| 91 // For the same reason as above, contexts may become invalid while we run. | 105 // For the same reason as above, contexts may become invalid while we run. |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 | 217 |
| 204 void ScriptContextSet::DispatchOnUnloadEventAndRemove( | 218 void ScriptContextSet::DispatchOnUnloadEventAndRemove( |
| 205 std::set<ScriptContext*>* out, | 219 std::set<ScriptContext*>* out, |
| 206 ScriptContext* context) { | 220 ScriptContext* context) { |
| 207 context->DispatchOnUnloadEvent(); | 221 context->DispatchOnUnloadEvent(); |
| 208 Remove(context); // deleted asynchronously | 222 Remove(context); // deleted asynchronously |
| 209 out->insert(context); | 223 out->insert(context); |
| 210 } | 224 } |
| 211 | 225 |
| 212 } // namespace extensions | 226 } // namespace extensions |
| OLD | NEW |