OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/renderer/extensions/chrome_v8_context_set.h" | 5 #include "extensions/renderer/script_context_set.h" |
6 | 6 |
7 #include "base/logging.h" | |
8 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
9 #include "base/tracked_objects.h" | |
10 #include "base/values.h" | |
11 #include "chrome/common/url_constants.h" | |
12 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
13 #include "content/public/renderer/render_thread.h" | |
14 #include "content/public/renderer/render_view.h" | 8 #include "content/public/renderer/render_view.h" |
15 #include "extensions/common/constants.h" | |
16 #include "extensions/common/extension.h" | 9 #include "extensions/common/extension.h" |
17 #include "third_party/WebKit/public/platform/WebURL.h" | 10 #include "extensions/renderer/script_context.h" |
18 #include "third_party/WebKit/public/platform/WebURLRequest.h" | |
19 #include "third_party/WebKit/public/web/WebDocument.h" | |
20 #include "third_party/WebKit/public/web/WebFrame.h" | |
21 #include "third_party/WebKit/public/web/WebView.h" | |
22 #include "v8/include/v8.h" | 11 #include "v8/include/v8.h" |
23 | 12 |
24 using content::RenderThread; | |
25 | |
26 namespace extensions { | 13 namespace extensions { |
27 | 14 |
28 ChromeV8ContextSet::ChromeV8ContextSet() { | 15 ScriptContextSet::ScriptContextSet() { |
29 } | 16 } |
30 ChromeV8ContextSet::~ChromeV8ContextSet() { | 17 ScriptContextSet::~ScriptContextSet() { |
31 } | 18 } |
32 | 19 |
33 int ChromeV8ContextSet::size() const { | 20 int ScriptContextSet::size() const { |
34 return static_cast<int>(contexts_.size()); | 21 return static_cast<int>(contexts_.size()); |
35 } | 22 } |
36 | 23 |
37 void ChromeV8ContextSet::Add(ChromeV8Context* context) { | 24 void ScriptContextSet::Add(ScriptContext* context) { |
38 #if DCHECK_IS_ON | 25 #if DCHECK_IS_ON |
39 // It's OK to insert the same context twice, but we should only ever have | 26 // It's OK to insert the same context twice, but we should only ever have |
40 // one ChromeV8Context per v8::Context. | 27 // one ScriptContext per v8::Context. |
41 for (ContextSet::iterator iter = contexts_.begin(); iter != contexts_.end(); | 28 for (ContextSet::iterator iter = contexts_.begin(); iter != contexts_.end(); |
42 ++iter) { | 29 ++iter) { |
43 ChromeV8Context* candidate = *iter; | 30 ScriptContext* candidate = *iter; |
44 if (candidate != context) | 31 if (candidate != context) |
45 DCHECK(candidate->v8_context() != context->v8_context()); | 32 DCHECK(candidate->v8_context() != context->v8_context()); |
46 } | 33 } |
47 #endif | 34 #endif |
48 contexts_.insert(context); | 35 contexts_.insert(context); |
49 } | 36 } |
50 | 37 |
51 void ChromeV8ContextSet::Remove(ChromeV8Context* context) { | 38 void ScriptContextSet::Remove(ScriptContext* context) { |
52 if (contexts_.erase(context)) { | 39 if (contexts_.erase(context)) { |
53 context->Invalidate(); | 40 context->Invalidate(); |
54 base::MessageLoop::current()->DeleteSoon(FROM_HERE, context); | 41 base::MessageLoop::current()->DeleteSoon(FROM_HERE, context); |
55 } | 42 } |
56 } | 43 } |
57 | 44 |
58 ChromeV8ContextSet::ContextSet ChromeV8ContextSet::GetAll() const { | 45 ScriptContextSet::ContextSet ScriptContextSet::GetAll() const { |
59 return contexts_; | 46 return contexts_; |
60 } | 47 } |
61 | 48 |
62 ChromeV8Context* ChromeV8ContextSet::GetCurrent() const { | 49 ScriptContext* ScriptContextSet::GetCurrent() const { |
63 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 50 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
64 return isolate->InContext() ? GetByV8Context(isolate->GetCurrentContext()) | 51 return isolate->InContext() ? GetByV8Context(isolate->GetCurrentContext()) |
65 : NULL; | 52 : NULL; |
66 } | 53 } |
67 | 54 |
68 ChromeV8Context* ChromeV8ContextSet::GetCalling() const { | 55 ScriptContext* ScriptContextSet::GetCalling() const { |
69 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 56 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
70 v8::Local<v8::Context> calling = isolate->GetCallingContext(); | 57 v8::Local<v8::Context> calling = isolate->GetCallingContext(); |
71 return calling.IsEmpty() ? NULL : GetByV8Context(calling); | 58 return calling.IsEmpty() ? NULL : GetByV8Context(calling); |
72 } | 59 } |
73 | 60 |
74 ChromeV8Context* ChromeV8ContextSet::GetByV8Context( | 61 ScriptContext* ScriptContextSet::GetByV8Context( |
75 v8::Handle<v8::Context> v8_context) const { | 62 v8::Handle<v8::Context> v8_context) const { |
76 for (ContextSet::const_iterator iter = contexts_.begin(); | 63 for (ContextSet::const_iterator iter = contexts_.begin(); |
77 iter != contexts_.end(); ++iter) { | 64 iter != contexts_.end(); |
| 65 ++iter) { |
78 if ((*iter)->v8_context() == v8_context) | 66 if ((*iter)->v8_context() == v8_context) |
79 return *iter; | 67 return *iter; |
80 } | 68 } |
81 | 69 |
82 return NULL; | 70 return NULL; |
83 } | 71 } |
84 | 72 |
85 void ChromeV8ContextSet::ForEach( | 73 void ScriptContextSet::ForEach( |
86 const std::string& extension_id, | 74 const std::string& extension_id, |
87 content::RenderView* render_view, | 75 content::RenderView* render_view, |
88 const base::Callback<void(ChromeV8Context*)>& callback) const { | 76 const base::Callback<void(ScriptContext*)>& callback) const { |
89 // We copy the context list, because calling into javascript may modify it | 77 // We copy the context list, because calling into javascript may modify it |
90 // out from under us. | 78 // out from under us. |
91 ContextSet contexts = GetAll(); | 79 ContextSet contexts = GetAll(); |
92 | 80 |
93 for (ContextSet::iterator it = contexts.begin(); it != contexts.end(); | 81 for (ContextSet::iterator it = contexts.begin(); it != contexts.end(); ++it) { |
94 ++it) { | 82 ScriptContext* context = *it; |
95 ChromeV8Context* context = *it; | |
96 | 83 |
97 // For the same reason as above, contexts may become invalid while we run. | 84 // For the same reason as above, contexts may become invalid while we run. |
98 if (!context->is_valid()) | 85 if (!context->is_valid()) |
99 continue; | 86 continue; |
100 | 87 |
101 if (!extension_id.empty()) { | 88 if (!extension_id.empty()) { |
102 const Extension* extension = context->extension(); | 89 const Extension* extension = context->extension(); |
103 if (!extension || (extension_id != extension->id())) | 90 if (!extension || (extension_id != extension->id())) |
104 continue; | 91 continue; |
105 } | 92 } |
106 | 93 |
107 content::RenderView* context_render_view = context->GetRenderView(); | 94 content::RenderView* context_render_view = context->GetRenderView(); |
108 if (!context_render_view) | 95 if (!context_render_view) |
109 continue; | 96 continue; |
110 | 97 |
111 if (render_view && render_view != context_render_view) | 98 if (render_view && render_view != context_render_view) |
112 continue; | 99 continue; |
113 | 100 |
114 callback.Run(context); | 101 callback.Run(context); |
115 } | 102 } |
116 } | 103 } |
117 | 104 |
118 ChromeV8ContextSet::ContextSet ChromeV8ContextSet::OnExtensionUnloaded( | 105 ScriptContextSet::ContextSet ScriptContextSet::OnExtensionUnloaded( |
119 const std::string& extension_id) { | 106 const std::string& extension_id) { |
120 ContextSet contexts = GetAll(); | 107 ContextSet contexts = GetAll(); |
121 ContextSet removed; | 108 ContextSet removed; |
122 | 109 |
123 // Clean up contexts belonging to the unloaded extension. This is done so | 110 // Clean up contexts belonging to the unloaded extension. This is done so |
124 // that content scripts (which remain injected into the page) don't continue | 111 // that content scripts (which remain injected into the page) don't continue |
125 // receiving events and sending messages. | 112 // receiving events and sending messages. |
126 for (ContextSet::iterator it = contexts.begin(); it != contexts.end(); | 113 for (ContextSet::iterator it = contexts.begin(); it != contexts.end(); ++it) { |
127 ++it) { | |
128 if ((*it)->extension() && (*it)->extension()->id() == extension_id) { | 114 if ((*it)->extension() && (*it)->extension()->id() == extension_id) { |
129 (*it)->DispatchOnUnloadEvent(); | 115 (*it)->DispatchOnUnloadEvent(); |
130 removed.insert(*it); | 116 removed.insert(*it); |
131 Remove(*it); | 117 Remove(*it); |
132 } | 118 } |
133 } | 119 } |
134 | 120 |
135 return removed; | 121 return removed; |
136 } | 122 } |
137 | 123 |
138 } // namespace extensions | 124 } // namespace extensions |
OLD | NEW |