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

Side by Side Diff: extensions/renderer/script_context.cc

Issue 1074273002: Move the event attach/detach logic on unload from event.js to (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 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 unified diff | Download patch
OLDNEW
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
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 if (is_valid_)
112 Invalidate();
111 } 113 }
112 114
113 void ScriptContext::Invalidate() { 115 void ScriptContext::Invalidate() {
114 if (!is_valid()) 116 CHECK(is_valid_);
115 return; 117 is_valid_ = false;
118
119 // TODO(kalman): Make ModuleSystem use AddInvalidateObserver.
Devlin 2015/04/10 18:10:40 Wait, didn't you?
not at google - send to devlin 2015/04/10 18:39:02 No I made EventBindings.
120 // Ownership graph is a bit weird here.
116 if (module_system_) 121 if (module_system_)
117 module_system_->Invalidate(); 122 module_system_->Invalidate();
118 web_frame_ = NULL; 123
124 // Swap |invalidate_observers_| to a local variable to clear it, and to make
125 // sure it's not mutated as we iterate.
Devlin 2015/04/10 18:10:40 I mean, I guess swap is pretty cheap - but how wou
not at google - send to devlin 2015/04/10 18:39:01 I might be missing something... but if an Invalida
Devlin 2015/04/10 22:00:24 Yeah, that would be a bug if anyone did that - but
126 std::vector<base::Closure> observers;
127 observers.swap(invalidate_observers_);
128 for (const base::Closure& observer : observers) {
129 observer.Run();
130 }
131
132 runner_.reset();
119 v8_context_.Reset(); 133 v8_context_.Reset();
120 runner_.reset(); 134 }
135
136 void ScriptContext::AddInvalidateObserver(const base::Closure& observer) {
137 invalidate_observers_.push_back(observer);
121 } 138 }
122 139
123 const std::string& ScriptContext::GetExtensionID() const { 140 const std::string& ScriptContext::GetExtensionID() const {
124 return extension_.get() ? extension_->id() : base::EmptyString(); 141 return extension_.get() ? extension_->id() : base::EmptyString();
125 } 142 }
126 143
127 content::RenderView* ScriptContext::GetRenderView() const { 144 content::RenderView* ScriptContext::GetRenderView() const {
128 if (web_frame_ && web_frame_->view()) 145 if (web_frame_ && web_frame_->view())
129 return content::RenderView::FromWebView(web_frame_->view()); 146 return content::RenderView::FromWebView(web_frame_->view());
130 return NULL; 147 return NULL;
131 } 148 }
132 149
133 content::RenderFrame* ScriptContext::GetRenderFrame() const { 150 content::RenderFrame* ScriptContext::GetRenderFrame() const {
134 if (web_frame_) 151 if (web_frame_)
135 return content::RenderFrame::FromWebFrame(web_frame_); 152 return content::RenderFrame::FromWebFrame(web_frame_);
136 return NULL; 153 return NULL;
137 } 154 }
138 155
139 v8::Local<v8::Value> ScriptContext::CallFunction( 156 v8::Local<v8::Value> ScriptContext::CallFunction(
140 v8::Handle<v8::Function> function, 157 v8::Handle<v8::Function> function,
141 int argc, 158 int argc,
142 v8::Handle<v8::Value> argv[]) const { 159 v8::Handle<v8::Value> argv[]) const {
143 v8::EscapableHandleScope handle_scope(isolate()); 160 v8::EscapableHandleScope handle_scope(isolate());
144 v8::Context::Scope scope(v8_context()); 161 v8::Context::Scope scope(v8_context());
145 162
146 blink::WebScopedMicrotaskSuppression suppression; 163 blink::WebScopedMicrotaskSuppression suppression;
147 if (!is_valid()) { 164 if (!is_valid_) {
148 return handle_scope.Escape( 165 return handle_scope.Escape(
149 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); 166 v8::Local<v8::Primitive>(v8::Undefined(isolate())));
150 } 167 }
151 168
152 v8::Handle<v8::Object> global = v8_context()->Global(); 169 v8::Handle<v8::Object> global = v8_context()->Global();
153 if (!web_frame_) 170 if (!web_frame_)
154 return handle_scope.Escape(function->Call(global, argc, argv)); 171 return handle_scope.Escape(function->Call(global, argc, argv));
155 return handle_scope.Escape( 172 return handle_scope.Escape(
156 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( 173 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled(
157 function, global, argc, argv))); 174 function, global, argc, argv)));
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 v8::Handle<v8::Value> argv[]) { 328 v8::Handle<v8::Value> argv[]) {
312 return context_->CallFunction(function, argc, argv); 329 return context_->CallFunction(function, argc, argv);
313 } 330 }
314 331
315 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { 332 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() {
316 v8::HandleScope handle_scope(context_->isolate()); 333 v8::HandleScope handle_scope(context_->isolate());
317 return gin::PerContextData::From(context_->v8_context())->context_holder(); 334 return gin::PerContextData::From(context_->v8_context())->context_holder();
318 } 335 }
319 336
320 } // namespace extensions 337 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698