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

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

Issue 10114015: Fix bug where transient pages would miss events dispatched while it was (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 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 | Annotate | Revision Log
OLDNEW
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/extension_custom_bindings.h" 5 #include "chrome/renderer/extensions/extension_custom_bindings.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "chrome/common/chrome_view_type.h" 10 #include "chrome/common/chrome_view_type.h"
11 #include "chrome/common/extensions/extension_action.h" 11 #include "chrome/common/extensions/extension_action.h"
12 #include "chrome/common/extensions/extension_messages.h" 12 #include "chrome/common/extensions/extension_messages.h"
13 #include "chrome/common/url_constants.h" 13 #include "chrome/common/url_constants.h"
14 #include "chrome/renderer/extensions/extension_dispatcher.h" 14 #include "chrome/renderer/extensions/extension_dispatcher.h"
15 #include "chrome/renderer/extensions/extension_helper.h" 15 #include "chrome/renderer/extensions/extension_helper.h"
16 #include "content/public/renderer/render_view.h" 16 #include "content/public/renderer/render_view.h"
17 #include "content/public/renderer/render_view_visitor.h"
18 #include "grit/renderer_resources.h" 17 #include "grit/renderer_resources.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 18 #include "v8/include/v8.h"
Yoyo Zhou 2012/04/19 21:47:13 nit: this is unalphabetical
Matt Perry 2012/04/19 22:21:53 Done.
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
22 #include "v8/include/v8.h"
23 #include "webkit/glue/webkit_glue.h" 21 #include "webkit/glue/webkit_glue.h"
24 22
25 namespace extensions { 23 namespace extensions {
26 24
27 namespace { 25 namespace {
28 26
29 // A RenderViewVisitor class that iterates through the set of available
30 // views, looking for a view of the given type, in the given browser window
31 // and within the given extension.
32 // Used to accumulate the list of views associated with an extension.
33 class ExtensionViewAccumulator : public content::RenderViewVisitor {
Matt Perry 2012/04/19 18:57:36 BTW, I moved this, mostly unchanged, to extension_
Yoyo Zhou 2012/04/19 21:47:13 Ok, I was trying to understand what this was doing
Matt Perry 2012/04/19 22:21:53 I'm using it for more than returning results to Ja
34 public:
35 ExtensionViewAccumulator(const std::string& extension_id,
36 int browser_window_id,
37 content::ViewType view_type)
38 : extension_id_(extension_id),
39 browser_window_id_(browser_window_id),
40 view_type_(view_type),
41 views_(v8::Array::New()),
42 index_(0) {
43 }
44
45 v8::Local<v8::Array> views() { return views_; }
46
47 virtual bool Visit(content::RenderView* render_view) {
48 ExtensionHelper* helper = ExtensionHelper::Get(render_view);
49 if (!ViewTypeMatches(helper->view_type(), view_type_))
50 return true;
51
52 GURL url = render_view->GetWebView()->mainFrame()->document().url();
53 if (!url.SchemeIs(chrome::kExtensionScheme))
54 return true;
55 const std::string& extension_id = url.host();
56 if (extension_id != extension_id_)
57 return true;
58
59 if (browser_window_id_ != extension_misc::kUnknownWindowId &&
60 helper->browser_window_id() != browser_window_id_) {
61 return true;
62 }
63
64 v8::Local<v8::Context> context =
65 render_view->GetWebView()->mainFrame()->mainWorldScriptContext();
66 if (!context.IsEmpty()) {
67 v8::Local<v8::Value> window = context->Global();
68 DCHECK(!window.IsEmpty());
69
70 if (!OnMatchedView(window))
71 return false;
72 }
73 return true;
74 }
75
76 private:
77 // Called on each view found matching the search criteria. Returns false
78 // to terminate the iteration.
79 bool OnMatchedView(v8::Local<v8::Value> view_window) {
80 views_->Set(v8::Integer::New(index_), view_window);
81 index_++;
82
83 if (view_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE)
84 return false; // There can be only one...
85
86 return true;
87 }
88
89 // Returns true is |type| "isa" |match|.
90 static bool ViewTypeMatches(content::ViewType type, content::ViewType match) {
91 if (type == match)
92 return true;
93
94 // INVALID means match all.
95 if (match == content::VIEW_TYPE_INVALID)
96 return true;
97
98 return false;
99 }
100
101 std::string extension_id_;
102 int browser_window_id_;
103 content::ViewType view_type_;
104 v8::Local<v8::Array> views_;
105 int index_;
106 };
107
108 } // namespace 27 } // namespace
109 28
110 ExtensionCustomBindings::ExtensionCustomBindings( 29 ExtensionCustomBindings::ExtensionCustomBindings(
111 ExtensionDispatcher* extension_dispatcher) 30 ExtensionDispatcher* extension_dispatcher)
112 : ChromeV8Extension(extension_dispatcher) { 31 : ChromeV8Extension(extension_dispatcher) {
113 RouteStaticFunction("GetExtensionViews", &GetExtensionViews); 32 RouteStaticFunction("GetExtensionViews", &GetExtensionViews);
114 RouteStaticFunction("OpenChannelToExtension", &OpenChannelToExtension); 33 RouteStaticFunction("OpenChannelToExtension", &OpenChannelToExtension);
115 } 34 }
116 35
117 // static 36 // static
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 return v8::Undefined(); 71 return v8::Undefined();
153 } 72 }
154 73
155 ExtensionCustomBindings* v8_extension = 74 ExtensionCustomBindings* v8_extension =
156 GetFromArguments<ExtensionCustomBindings>(args); 75 GetFromArguments<ExtensionCustomBindings>(args);
157 const ::Extension* extension = 76 const ::Extension* extension =
158 v8_extension->GetExtensionForCurrentRenderView(); 77 v8_extension->GetExtensionForCurrentRenderView();
159 if (!extension) 78 if (!extension)
160 return v8::Undefined(); 79 return v8::Undefined();
161 80
162 ExtensionViewAccumulator accumulator(extension->id(), browser_window_id, 81 std::vector<content::RenderView*> views = ExtensionHelper::GetExtensionViews(
163 view_type); 82 extension->id(), browser_window_id, view_type);
164 content::RenderView::ForEach(&accumulator); 83 v8::Local<v8::Array> v8_views = v8::Array::New();
165 return accumulator.views(); 84 int v8_index = 0;
85 for (size_t i = 0; i < views.size(); ++i) {
86 v8::Local<v8::Context> context =
87 views[i]->GetWebView()->mainFrame()->mainWorldScriptContext();
88 if (!context.IsEmpty()) {
89 v8::Local<v8::Value> window = context->Global();
90 DCHECK(!window.IsEmpty());
91 v8_views->Set(v8::Integer::New(v8_index++), window);
92 }
93 }
94
95 return v8_views;
166 } 96 }
167 97
168 // static 98 // static
169 v8::Handle<v8::Value> ExtensionCustomBindings::OpenChannelToExtension( 99 v8::Handle<v8::Value> ExtensionCustomBindings::OpenChannelToExtension(
170 const v8::Arguments& args) { 100 const v8::Arguments& args) {
171 // Get the current RenderView so that we can send a routed IPC message from 101 // Get the current RenderView so that we can send a routed IPC message from
172 // the correct source. 102 // the correct source.
173 content::RenderView* renderview = GetCurrentRenderView(); 103 content::RenderView* renderview = GetCurrentRenderView();
174 if (!renderview) 104 if (!renderview)
175 return v8::Undefined(); 105 return v8::Undefined();
(...skipping 11 matching lines...) Expand all
187 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension( 117 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension(
188 renderview->GetRoutingID(), 118 renderview->GetRoutingID(),
189 source_id, 119 source_id,
190 target_id, 120 target_id,
191 channel_name, 121 channel_name,
192 &port_id)); 122 &port_id));
193 return v8::Integer::New(port_id); 123 return v8::Integer::New(port_id);
194 } 124 }
195 125
196 } // namespace extensions 126 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698