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

Side by Side Diff: chrome/renderer/extensions/event_bindings.cc

Issue 200116: Fix bug where content scripts can get GC'd if they don't attach (Closed)
Patch Set: remove extra braces Created 11 years, 3 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
« no previous file with comments | « chrome/renderer/extensions/bindings_utils.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/event_bindings.h" 5 #include "chrome/renderer/extensions/event_bindings.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/singleton.h" 8 #include "base/singleton.h"
9 #include "chrome/common/render_messages.h" 9 #include "chrome/common/render_messages.h"
10 #include "chrome/common/url_constants.h" 10 #include "chrome/common/url_constants.h"
11 #include "chrome/renderer/extensions/bindings_utils.h" 11 #include "chrome/renderer/extensions/bindings_utils.h"
12 #include "chrome/renderer/extensions/event_bindings.h" 12 #include "chrome/renderer/extensions/event_bindings.h"
13 #include "chrome/renderer/extensions/js_only_v8_extensions.h" 13 #include "chrome/renderer/extensions/js_only_v8_extensions.h"
14 #include "chrome/renderer/render_thread.h" 14 #include "chrome/renderer/render_thread.h"
15 #include "chrome/renderer/render_view.h" 15 #include "chrome/renderer/render_view.h"
16 #include "grit/renderer_resources.h" 16 #include "grit/renderer_resources.h"
17 #include "webkit/api/public/WebDataSource.h" 17 #include "webkit/api/public/WebDataSource.h"
18 #include "webkit/api/public/WebFrame.h" 18 #include "webkit/api/public/WebFrame.h"
19 #include "webkit/api/public/WebURLRequest.h" 19 #include "webkit/api/public/WebURLRequest.h"
20 20
21 using bindings_utils::CallFunctionInContext; 21 using bindings_utils::CallFunctionInContext;
22 using bindings_utils::ContextInfo; 22 using bindings_utils::ContextInfo;
23 using bindings_utils::ContextList; 23 using bindings_utils::ContextList;
24 using bindings_utils::GetContexts; 24 using bindings_utils::GetContexts;
25 using bindings_utils::GetInfoForCurrentContext;
25 using bindings_utils::GetStringResource; 26 using bindings_utils::GetStringResource;
26 using bindings_utils::ExtensionBase; 27 using bindings_utils::ExtensionBase;
27 using bindings_utils::GetPendingRequestMap; 28 using bindings_utils::GetPendingRequestMap;
28 using bindings_utils::PendingRequestMap; 29 using bindings_utils::PendingRequestMap;
29 using WebKit::WebFrame; 30 using WebKit::WebFrame;
30 31
32 static void ContextWeakReferenceCallback(v8::Persistent<v8::Value> context,
33 void*);
34
31 namespace { 35 namespace {
32 36
33 // Keep a local cache of RenderThread so that we can mock it out for unit tests. 37 // Keep a local cache of RenderThread so that we can mock it out for unit tests.
34 static RenderThreadBase* render_thread = NULL; 38 static RenderThreadBase* render_thread = NULL;
35 static bool in_unit_tests = false; 39 static bool in_unit_tests = false;
36 40
37 // Set to true if these bindings are registered. Will be false when extensions 41 // Set to true if these bindings are registered. Will be false when extensions
38 // are disabled. 42 // are disabled.
39 static bool bindings_registered = false; 43 static bool bindings_registered = false;
40 44
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 // Attach an event name to an object. 76 // Attach an event name to an object.
73 static v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) { 77 static v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) {
74 DCHECK(args.Length() == 1); 78 DCHECK(args.Length() == 1);
75 // TODO(erikkay) should enforce that event name is a string in the bindings 79 // TODO(erikkay) should enforce that event name is a string in the bindings
76 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); 80 DCHECK(args[0]->IsString() || args[0]->IsUndefined());
77 81
78 if (args[0]->IsString()) { 82 if (args[0]->IsString()) {
79 std::string event_name(*v8::String::AsciiValue(args[0])); 83 std::string event_name(*v8::String::AsciiValue(args[0]));
80 bool has_permission = 84 bool has_permission =
81 ExtensionProcessBindings::CurrentContextHasPermission(event_name); 85 ExtensionProcessBindings::CurrentContextHasPermission(event_name);
82 #if EXTENSION_TIME_TO_BREAK_API
83 bool allow_api = has_permission; 86 bool allow_api = has_permission;
Matt Perry 2009/09/14 18:07:47 may as well remove this and use has_permission dir
84 #else
85 bool allow_api = true;
86 #endif
87 87
88 // Increment the count even if the caller doesn't have permission, so that 88 // Increment the count even if the caller doesn't have permission, so that
89 // refcounts stay balanced. 89 // refcounts stay balanced.
90 if (EventIncrementListenerCount(event_name) == 1 && allow_api) { 90 if (EventIncrementListenerCount(event_name) == 1 && allow_api) {
91 EventBindings::GetRenderThread()->Send( 91 EventBindings::GetRenderThread()->Send(
92 new ViewHostMsg_ExtensionAddListener(event_name)); 92 new ViewHostMsg_ExtensionAddListener(event_name));
93 } 93 }
94 94
95 ContextInfo* current_context_info = GetInfoForCurrentContext();
96 if (++current_context_info->num_connected_events == 1)
97 current_context_info->context.ClearWeak();
98
95 if (!has_permission) { 99 if (!has_permission) {
96 return ExtensionProcessBindings::ThrowPermissionDeniedException( 100 return ExtensionProcessBindings::ThrowPermissionDeniedException(
97 event_name); 101 event_name);
98 } 102 }
99 } 103 }
100 104
101 return v8::Undefined(); 105 return v8::Undefined();
102 } 106 }
103 107
104 static v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { 108 static v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) {
105 DCHECK(args.Length() == 1); 109 DCHECK(args.Length() == 1);
106 // TODO(erikkay) should enforce that event name is a string in the bindings 110 // TODO(erikkay) should enforce that event name is a string in the bindings
107 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); 111 DCHECK(args[0]->IsString() || args[0]->IsUndefined());
108 112
109 if (args[0]->IsString()) { 113 if (args[0]->IsString()) {
110 std::string event_name(*v8::String::AsciiValue(args[0])); 114 std::string event_name(*v8::String::AsciiValue(args[0]));
111 if (EventDecrementListenerCount(event_name) == 0) { 115 if (EventDecrementListenerCount(event_name) == 0) {
112 EventBindings::GetRenderThread()->Send( 116 EventBindings::GetRenderThread()->Send(
113 new ViewHostMsg_ExtensionRemoveListener(event_name)); 117 new ViewHostMsg_ExtensionRemoveListener(event_name));
114 } 118 }
119
120 ContextInfo* current_context_info = GetInfoForCurrentContext();
121 if (current_context_info &&
Matt Perry 2009/09/14 18:07:47 why the NULL-check here but not in AttachEvent?
122 --current_context_info->num_connected_events == 0) {
123 current_context_info->context.MakeWeak(NULL,
124 &ContextWeakReferenceCallback);
125 }
115 } 126 }
116 127
117 return v8::Undefined(); 128 return v8::Undefined();
118 } 129 }
119 }; 130 };
120 131
121 } // namespace 132 } // namespace
122 133
123 const char* EventBindings::kName = "chrome/EventBindings"; 134 const char* EventBindings::kName = "chrome/EventBindings";
124 135
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 // TODO(rafaelw): Consider only doing this check if function_name == 305 // TODO(rafaelw): Consider only doing this check if function_name ==
295 // "Event.dispatchJSON". 306 // "Event.dispatchJSON".
296 #ifdef _DEBUG 307 #ifdef _DEBUG
297 if (!retval.IsEmpty() && !retval->IsUndefined()) { 308 if (!retval.IsEmpty() && !retval->IsUndefined()) {
298 std::string error = *v8::String::AsciiValue(retval); 309 std::string error = *v8::String::AsciiValue(retval);
299 DCHECK(false) << error; 310 DCHECK(false) << error;
300 } 311 }
301 #endif 312 #endif
302 } 313 }
303 } 314 }
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/bindings_utils.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698