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.h" | 5 #include "extensions/renderer/script_context.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 private: | 73 private: |
74 ScriptContext* context_; | 74 ScriptContext* context_; |
75 }; | 75 }; |
76 | 76 |
77 ScriptContext::ScriptContext(const v8::Handle<v8::Context>& v8_context, | 77 ScriptContext::ScriptContext(const v8::Handle<v8::Context>& v8_context, |
78 blink::WebFrame* web_frame, | 78 blink::WebFrame* web_frame, |
79 const Extension* extension, | 79 const Extension* extension, |
80 Feature::Context context_type, | 80 Feature::Context context_type, |
81 const Extension* effective_extension, | 81 const Extension* effective_extension, |
82 Feature::Context effective_context_type) | 82 Feature::Context effective_context_type) |
83 : is_valid_(true), | 83 : v8_context_(v8_context->GetIsolate(), v8_context), |
84 v8_context_(v8_context->GetIsolate(), v8_context), | |
85 web_frame_(web_frame), | 84 web_frame_(web_frame), |
86 extension_(extension), | 85 extension_(extension), |
87 context_type_(context_type), | 86 context_type_(context_type), |
88 effective_extension_(effective_extension), | 87 effective_extension_(effective_extension), |
89 effective_context_type_(effective_context_type), | 88 effective_context_type_(effective_context_type), |
90 safe_builtins_(this), | 89 safe_builtins_(this), |
91 isolate_(v8_context->GetIsolate()), | 90 isolate_(v8_context->GetIsolate()), |
92 url_(web_frame_ ? GetDataSourceURLForFrame(web_frame_) : GURL()), | 91 url_(web_frame_ ? GetDataSourceURLForFrame(web_frame_) : GURL()), |
93 runner_(new Runner(this)) { | 92 runner_(new Runner(this)) { |
94 VLOG(1) << "Created context:\n" | 93 VLOG(1) << "Created context:\n" |
95 << " extension id: " << GetExtensionID() << "\n" | 94 << " extension id: " << GetExtensionID() << "\n" |
96 << " frame: " << web_frame_ << "\n" | 95 << " frame: " << web_frame_ << "\n" |
97 << " URL: " << GetURL() << "\n" | 96 << " URL: " << GetURL() << "\n" |
98 << " context type: " << GetContextTypeDescription() << "\n" | 97 << " context type: " << GetContextTypeDescription() << "\n" |
99 << " effective extension id: " | 98 << " effective extension id: " |
100 << (effective_extension_.get() ? effective_extension_->id() : "") | 99 << (effective_extension_.get() ? effective_extension_->id() : "") |
101 << " effective context type: " | 100 << " effective context type: " |
102 << GetEffectiveContextTypeDescription(); | 101 << GetEffectiveContextTypeDescription(); |
103 gin::PerContextData::From(v8_context)->set_runner(runner_.get()); | 102 gin::PerContextData::From(v8_context)->set_runner(runner_.get()); |
104 } | 103 } |
105 | 104 |
106 ScriptContext::~ScriptContext() { | 105 ScriptContext::~ScriptContext() { |
107 VLOG(1) << "Destroyed context for extension\n" | 106 VLOG(1) << "Destroyed context for extension\n" |
108 << " extension id: " << GetExtensionID() << "\n" | 107 << " extension id: " << GetExtensionID() << "\n" |
109 << " effective extension id: " | 108 << " effective extension id: " |
110 << (effective_extension_.get() ? effective_extension_->id() : ""); | 109 << (effective_extension_.get() ? effective_extension_->id() : ""); |
111 CHECK(!is_valid_) << "ScriptContexts must be invalidated before destruction"; | 110 Invalidate(); |
112 } | 111 } |
113 | 112 |
114 void ScriptContext::Invalidate() { | 113 void ScriptContext::Invalidate() { |
115 CHECK(is_valid_); | 114 if (!is_valid()) |
116 is_valid_ = false; | 115 return; |
117 | |
118 // TODO(kalman): Make ModuleSystem use AddInvalidationObserver. | |
119 // Ownership graph is a bit weird here. | |
120 if (module_system_) | 116 if (module_system_) |
121 module_system_->Invalidate(); | 117 module_system_->Invalidate(); |
122 | 118 web_frame_ = NULL; |
123 // Swap |invalidate_observers_| to a local variable to clear it, and to make | 119 v8_context_.Reset(); |
124 // sure it's not mutated as we iterate. | |
125 std::vector<base::Closure> observers; | |
126 observers.swap(invalidate_observers_); | |
127 for (const base::Closure& observer : observers) { | |
128 observer.Run(); | |
129 } | |
130 DCHECK(invalidate_observers_.empty()) | |
131 << "Invalidation observers cannot be added during invalidation"; | |
132 | |
133 runner_.reset(); | 120 runner_.reset(); |
134 v8_context_.Reset(); | |
135 } | |
136 | |
137 void ScriptContext::AddInvalidationObserver(const base::Closure& observer) { | |
138 invalidate_observers_.push_back(observer); | |
139 } | 121 } |
140 | 122 |
141 const std::string& ScriptContext::GetExtensionID() const { | 123 const std::string& ScriptContext::GetExtensionID() const { |
142 return extension_.get() ? extension_->id() : base::EmptyString(); | 124 return extension_.get() ? extension_->id() : base::EmptyString(); |
143 } | 125 } |
144 | 126 |
145 content::RenderView* ScriptContext::GetRenderView() const { | 127 content::RenderView* ScriptContext::GetRenderView() const { |
146 if (web_frame_ && web_frame_->view()) | 128 if (web_frame_ && web_frame_->view()) |
147 return content::RenderView::FromWebView(web_frame_->view()); | 129 return content::RenderView::FromWebView(web_frame_->view()); |
148 return NULL; | 130 return NULL; |
149 } | 131 } |
150 | 132 |
151 content::RenderFrame* ScriptContext::GetRenderFrame() const { | 133 content::RenderFrame* ScriptContext::GetRenderFrame() const { |
152 if (web_frame_) | 134 if (web_frame_) |
153 return content::RenderFrame::FromWebFrame(web_frame_); | 135 return content::RenderFrame::FromWebFrame(web_frame_); |
154 return NULL; | 136 return NULL; |
155 } | 137 } |
156 | 138 |
157 v8::Local<v8::Value> ScriptContext::CallFunction( | 139 v8::Local<v8::Value> ScriptContext::CallFunction( |
158 v8::Handle<v8::Function> function, | 140 v8::Handle<v8::Function> function, |
159 int argc, | 141 int argc, |
160 v8::Handle<v8::Value> argv[]) const { | 142 v8::Handle<v8::Value> argv[]) const { |
161 v8::EscapableHandleScope handle_scope(isolate()); | 143 v8::EscapableHandleScope handle_scope(isolate()); |
162 v8::Context::Scope scope(v8_context()); | 144 v8::Context::Scope scope(v8_context()); |
163 | 145 |
164 blink::WebScopedMicrotaskSuppression suppression; | 146 blink::WebScopedMicrotaskSuppression suppression; |
165 if (!is_valid_) { | 147 if (!is_valid()) { |
166 return handle_scope.Escape( | 148 return handle_scope.Escape( |
167 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); | 149 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); |
168 } | 150 } |
169 | 151 |
170 v8::Handle<v8::Object> global = v8_context()->Global(); | 152 v8::Handle<v8::Object> global = v8_context()->Global(); |
171 if (!web_frame_) | 153 if (!web_frame_) |
172 return handle_scope.Escape(function->Call(global, argc, argv)); | 154 return handle_scope.Escape(function->Call(global, argc, argv)); |
173 return handle_scope.Escape( | 155 return handle_scope.Escape( |
174 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( | 156 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( |
175 function, global, argc, argv))); | 157 function, global, argc, argv))); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 v8::Handle<v8::Value> argv[]) { | 311 v8::Handle<v8::Value> argv[]) { |
330 return context_->CallFunction(function, argc, argv); | 312 return context_->CallFunction(function, argc, argv); |
331 } | 313 } |
332 | 314 |
333 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { | 315 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { |
334 v8::HandleScope handle_scope(context_->isolate()); | 316 v8::HandleScope handle_scope(context_->isolate()); |
335 return gin::PerContextData::From(context_->v8_context())->context_holder(); | 317 return gin::PerContextData::From(context_->v8_context())->context_holder(); |
336 } | 318 } |
337 | 319 |
338 } // namespace extensions | 320 } // namespace extensions |
OLD | NEW |