Chromium Code Reviews| 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 : v8_context_(v8_context->GetIsolate(), v8_context), | 83 : is_valid_(true), |
| 84 v8_context_(v8_context->GetIsolate(), v8_context), | |
| 84 web_frame_(web_frame), | 85 web_frame_(web_frame), |
| 85 extension_(extension), | 86 extension_(extension), |
| 86 context_type_(context_type), | 87 context_type_(context_type), |
| 87 effective_extension_(effective_extension), | 88 effective_extension_(effective_extension), |
| 88 effective_context_type_(effective_context_type), | 89 effective_context_type_(effective_context_type), |
| 89 safe_builtins_(this), | 90 safe_builtins_(this), |
| 90 isolate_(v8_context->GetIsolate()), | 91 isolate_(v8_context->GetIsolate()), |
| 91 url_(web_frame_ ? GetDataSourceURLForFrame(web_frame_) : GURL()), | 92 url_(web_frame_ ? GetDataSourceURLForFrame(web_frame_) : GURL()), |
| 92 runner_(new Runner(this)) { | 93 runner_(new Runner(this)) { |
| 93 VLOG(1) << "Created context:\n" | 94 VLOG(1) << "Created context:\n" |
| 94 << " extension id: " << GetExtensionID() << "\n" | 95 << " extension id: " << GetExtensionID() << "\n" |
| 95 << " frame: " << web_frame_ << "\n" | 96 << " frame: " << web_frame_ << "\n" |
| 96 << " URL: " << GetURL() << "\n" | 97 << " URL: " << GetURL() << "\n" |
| 97 << " context type: " << GetContextTypeDescription() << "\n" | 98 << " context type: " << GetContextTypeDescription() << "\n" |
| 98 << " effective extension id: " | 99 << " effective extension id: " |
| 99 << (effective_extension_.get() ? effective_extension_->id() : "") | 100 << (effective_extension_.get() ? effective_extension_->id() : "") |
| 100 << " effective context type: " | 101 << " effective context type: " |
| 101 << GetEffectiveContextTypeDescription(); | 102 << GetEffectiveContextTypeDescription(); |
| 102 gin::PerContextData::From(v8_context)->set_runner(runner_.get()); | 103 gin::PerContextData::From(v8_context)->set_runner(runner_.get()); |
| 103 } | 104 } |
| 104 | 105 |
| 105 ScriptContext::~ScriptContext() { | 106 ScriptContext::~ScriptContext() { |
| 106 VLOG(1) << "Destroyed context for extension\n" | 107 VLOG(1) << "Destroyed context for extension\n" |
| 107 << " extension id: " << GetExtensionID() << "\n" | 108 << " extension id: " << GetExtensionID() << "\n" |
| 108 << " effective extension id: " | 109 << " effective extension id: " |
| 109 << (effective_extension_.get() ? effective_extension_->id() : ""); | 110 << (effective_extension_.get() ? effective_extension_->id() : ""); |
| 110 Invalidate(); | 111 CHECK(!is_valid_) << "ScriptContexts must be invalidated before destruction"; |
|
Devlin
2015/04/10 22:00:24
I'm liking all these CHECKs. Hopefully it's not a
| |
| 111 } | 112 } |
| 112 | 113 |
| 113 void ScriptContext::Invalidate() { | 114 void ScriptContext::Invalidate() { |
| 114 if (!is_valid()) | 115 CHECK(is_valid_); |
| 115 return; | 116 is_valid_ = false; |
| 117 | |
| 118 // TODO(kalman): Make ModuleSystem use AddInvalidationObserver. | |
| 119 // Ownership graph is a bit weird here. | |
| 116 if (module_system_) | 120 if (module_system_) |
| 117 module_system_->Invalidate(); | 121 module_system_->Invalidate(); |
|
Devlin
2015/04/10 22:00:24
You're getting a compile error here (Invalidate()
not at google - send to devlin
2015/04/10 22:31:45
Fixed in person (I meant to friend ModuleSystemTes
| |
| 118 web_frame_ = NULL; | 122 |
| 123 // Swap |invalidate_observers_| to a local variable to clear it, and to make | |
| 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(); | |
| 119 v8_context_.Reset(); | 134 v8_context_.Reset(); |
| 120 runner_.reset(); | 135 } |
| 136 | |
| 137 void ScriptContext::AddInvalidationObserver(const base::Closure& observer) { | |
| 138 invalidate_observers_.push_back(observer); | |
| 121 } | 139 } |
| 122 | 140 |
| 123 const std::string& ScriptContext::GetExtensionID() const { | 141 const std::string& ScriptContext::GetExtensionID() const { |
| 124 return extension_.get() ? extension_->id() : base::EmptyString(); | 142 return extension_.get() ? extension_->id() : base::EmptyString(); |
| 125 } | 143 } |
| 126 | 144 |
| 127 content::RenderView* ScriptContext::GetRenderView() const { | 145 content::RenderView* ScriptContext::GetRenderView() const { |
| 128 if (web_frame_ && web_frame_->view()) | 146 if (web_frame_ && web_frame_->view()) |
| 129 return content::RenderView::FromWebView(web_frame_->view()); | 147 return content::RenderView::FromWebView(web_frame_->view()); |
| 130 return NULL; | 148 return NULL; |
| 131 } | 149 } |
| 132 | 150 |
| 133 content::RenderFrame* ScriptContext::GetRenderFrame() const { | 151 content::RenderFrame* ScriptContext::GetRenderFrame() const { |
| 134 if (web_frame_) | 152 if (web_frame_) |
| 135 return content::RenderFrame::FromWebFrame(web_frame_); | 153 return content::RenderFrame::FromWebFrame(web_frame_); |
| 136 return NULL; | 154 return NULL; |
| 137 } | 155 } |
| 138 | 156 |
| 139 v8::Local<v8::Value> ScriptContext::CallFunction( | 157 v8::Local<v8::Value> ScriptContext::CallFunction( |
| 140 v8::Handle<v8::Function> function, | 158 v8::Handle<v8::Function> function, |
| 141 int argc, | 159 int argc, |
| 142 v8::Handle<v8::Value> argv[]) const { | 160 v8::Handle<v8::Value> argv[]) const { |
| 143 v8::EscapableHandleScope handle_scope(isolate()); | 161 v8::EscapableHandleScope handle_scope(isolate()); |
| 144 v8::Context::Scope scope(v8_context()); | 162 v8::Context::Scope scope(v8_context()); |
| 145 | 163 |
| 146 blink::WebScopedMicrotaskSuppression suppression; | 164 blink::WebScopedMicrotaskSuppression suppression; |
| 147 if (!is_valid()) { | 165 if (!is_valid_) { |
| 148 return handle_scope.Escape( | 166 return handle_scope.Escape( |
| 149 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); | 167 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); |
| 150 } | 168 } |
| 151 | 169 |
| 152 v8::Handle<v8::Object> global = v8_context()->Global(); | 170 v8::Handle<v8::Object> global = v8_context()->Global(); |
| 153 if (!web_frame_) | 171 if (!web_frame_) |
| 154 return handle_scope.Escape(function->Call(global, argc, argv)); | 172 return handle_scope.Escape(function->Call(global, argc, argv)); |
| 155 return handle_scope.Escape( | 173 return handle_scope.Escape( |
| 156 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( | 174 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( |
| 157 function, global, argc, argv))); | 175 function, global, argc, argv))); |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 v8::Handle<v8::Value> argv[]) { | 329 v8::Handle<v8::Value> argv[]) { |
| 312 return context_->CallFunction(function, argc, argv); | 330 return context_->CallFunction(function, argc, argv); |
| 313 } | 331 } |
| 314 | 332 |
| 315 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { | 333 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { |
| 316 v8::HandleScope handle_scope(context_->isolate()); | 334 v8::HandleScope handle_scope(context_->isolate()); |
| 317 return gin::PerContextData::From(context_->v8_context())->context_holder(); | 335 return gin::PerContextData::From(context_->v8_context())->context_holder(); |
| 318 } | 336 } |
| 319 | 337 |
| 320 } // namespace extensions | 338 } // namespace extensions |
| OLD | NEW |