OLD | NEW |
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/common/extensions/extension_messages.h" | 5 #include "chrome/common/extensions/extension_messages.h" |
6 #include "chrome/renderer/extensions/chrome_v8_context.h" | 6 #include "chrome/renderer/extensions/chrome_v8_context.h" |
7 #include "chrome/renderer/extensions/chrome_v8_extension.h" | 7 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
8 #include "chrome/renderer/extensions/content_watcher.h" | 8 #include "chrome/renderer/extensions/content_watcher.h" |
9 #include "chrome/renderer/extensions/dispatcher.h" | 9 #include "chrome/renderer/extensions/dispatcher.h" |
10 #include "content/public/renderer/render_view.h" | 10 #include "content/public/renderer/render_view.h" |
11 #include "content/public/renderer/render_view_visitor.h" | 11 #include "content/public/renderer/render_view_visitor.h" |
12 #include "third_party/WebKit/public/web/WebDocument.h" | 12 #include "third_party/WebKit/public/web/WebDocument.h" |
| 13 #include "third_party/WebKit/public/web/WebElement.h" |
13 #include "third_party/WebKit/public/web/WebFrame.h" | 14 #include "third_party/WebKit/public/web/WebFrame.h" |
14 #include "third_party/WebKit/public/web/WebView.h" | 15 #include "third_party/WebKit/public/web/WebView.h" |
15 | 16 |
16 namespace extensions { | 17 namespace extensions { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 class MutationHandler : public ChromeV8Extension { | 21 class MutationHandler : public ChromeV8Extension { |
21 public: | 22 public: |
22 explicit MutationHandler(Dispatcher* dispatcher, | 23 explicit MutationHandler(Dispatcher* dispatcher, |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 return; | 123 return; |
123 | 124 |
124 using std::swap; | 125 using std::swap; |
125 swap(old_matches, new_matches); | 126 swap(old_matches, new_matches); |
126 NotifyBrowserOfChange(frame); | 127 NotifyBrowserOfChange(frame); |
127 } | 128 } |
128 | 129 |
129 std::vector<base::StringPiece> ContentWatcher::FindMatchingSelectors( | 130 std::vector<base::StringPiece> ContentWatcher::FindMatchingSelectors( |
130 WebKit::WebFrame* frame) const { | 131 WebKit::WebFrame* frame) const { |
131 std::vector<base::StringPiece> result; | 132 std::vector<base::StringPiece> result; |
132 v8::HandleScope scope; | 133 WebKit::WebDocument document = frame->document(); |
133 | 134 for (size_t i = 0; i < css_selectors_.size(); ++i) { |
134 // Get the indices within |css_selectors_| that match elements in | 135 WebKit::WebExceptionCode exception = 0; |
135 // |frame|, as a JS Array. | 136 if (!document.querySelector(WebKit::WebString::fromUTF8(css_selectors_[i]), |
136 v8::Local<v8::Value> selector_indices; | 137 exception).isNull()) |
137 if (ModuleSystem* module_system = GetModuleSystem(frame)) { | 138 result.push_back(css_selectors_[i]); |
138 v8::Context::Scope context_scope(frame->mainWorldScriptContext()); | 139 DCHECK_EQ(0, exception) |
139 v8::Local<v8::Array> js_css_selectors = | 140 << "We should already have checked that the selectors are valid."; |
140 v8::Array::New(css_selectors_.size()); | |
141 for (size_t i = 0; i < css_selectors_.size(); ++i) { | |
142 js_css_selectors->Set(i, v8::String::New(css_selectors_[i].data(), | |
143 css_selectors_[i].size())); | |
144 } | |
145 std::vector<v8::Handle<v8::Value> > find_selectors_args; | |
146 find_selectors_args.push_back(js_css_selectors); | |
147 selector_indices = module_system->CallModuleMethod("contentWatcher", | |
148 "FindMatchingSelectors", | |
149 &find_selectors_args); | |
150 } | |
151 if (selector_indices.IsEmpty() || !selector_indices->IsArray()) | |
152 return result; | |
153 | |
154 // Iterate over the array, and extract the indices, laboriously | |
155 // converting them back to integers. | |
156 v8::Local<v8::Array> index_array = selector_indices.As<v8::Array>(); | |
157 const size_t length = index_array->Length(); | |
158 result.reserve(length); | |
159 for (size_t i = 0; i < length; ++i) { | |
160 v8::Local<v8::Value> index_value = index_array->Get(i); | |
161 if (!index_value->IsNumber()) | |
162 continue; | |
163 double index = index_value->NumberValue(); | |
164 // Make sure the index is within bounds. | |
165 if (index < 0 || css_selectors_.size() <= index) | |
166 continue; | |
167 // Push a StringPiece referring to the CSS selector onto the result. | |
168 result.push_back( | |
169 base::StringPiece(css_selectors_[static_cast<size_t>(index)])); | |
170 } | 141 } |
171 return result; | 142 return result; |
172 } | 143 } |
173 | 144 |
174 void ContentWatcher::NotifyBrowserOfChange( | 145 void ContentWatcher::NotifyBrowserOfChange( |
175 WebKit::WebFrame* changed_frame) const { | 146 WebKit::WebFrame* changed_frame) const { |
176 WebKit::WebFrame* const top_frame = changed_frame->top(); | 147 WebKit::WebFrame* const top_frame = changed_frame->top(); |
177 const WebKit::WebSecurityOrigin top_origin = | 148 const WebKit::WebSecurityOrigin top_origin = |
178 top_frame->document().securityOrigin(); | 149 top_frame->document().securityOrigin(); |
179 // Want to aggregate matched selectors from all frames where an | 150 // Want to aggregate matched selectors from all frames where an |
(...skipping 22 matching lines...) Expand all Loading... |
202 it = transitive_selectors.begin(); | 173 it = transitive_selectors.begin(); |
203 it != transitive_selectors.end(); ++it) | 174 it != transitive_selectors.end(); ++it) |
204 selector_strings.push_back(it->as_string()); | 175 selector_strings.push_back(it->as_string()); |
205 content::RenderView* view = | 176 content::RenderView* view = |
206 content::RenderView::FromWebView(top_frame->view()); | 177 content::RenderView::FromWebView(top_frame->view()); |
207 view->Send(new ExtensionHostMsg_OnWatchedPageChange( | 178 view->Send(new ExtensionHostMsg_OnWatchedPageChange( |
208 view->GetRoutingID(), selector_strings)); | 179 view->GetRoutingID(), selector_strings)); |
209 } | 180 } |
210 | 181 |
211 } // namespace extensions | 182 } // namespace extensions |
OLD | NEW |