Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/common/extensions/extension_messages.h" | |
| 6 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
| 7 #include "chrome/renderer/extensions/chrome_v8_extension.h" | |
| 8 #include "chrome/renderer/extensions/content_watcher.h" | |
| 9 #include "chrome/renderer/extensions/dispatcher.h" | |
| 10 #include "content/public/renderer/render_view.h" | |
| 11 #include "content/public/renderer/render_view_visitor.h" | |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 15 | |
| 16 namespace extensions { | |
| 17 | |
| 18 namespace { | |
| 19 class MutationHandler : public ChromeV8Extension { | |
| 20 public: | |
| 21 explicit MutationHandler(Dispatcher* dispatcher, | |
| 22 base::WeakPtr<ContentWatcher> content_watcher) | |
| 23 : ChromeV8Extension(dispatcher), | |
| 24 content_watcher_(content_watcher) { | |
| 25 RouteFunction("FrameMutated", | |
| 26 base::Bind(&MutationHandler::FrameMutated, | |
| 27 base::Unretained(this))); | |
| 28 } | |
| 29 | |
| 30 private: | |
| 31 v8::Handle<v8::Value> FrameMutated(const v8::Arguments& args) { | |
| 32 if (content_watcher_) { | |
| 33 content_watcher_->ScanAndNotify( | |
| 34 WebKit::WebFrame::frameForCurrentContext()); | |
| 35 } | |
| 36 return v8::Undefined(); | |
| 37 } | |
| 38 | |
| 39 base::WeakPtr<ContentWatcher> content_watcher_; | |
| 40 }; | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 ContentWatcher::ContentWatcher(Dispatcher* dispatcher) | |
| 45 : weak_ptr_factory_(this), | |
| 46 dispatcher_(dispatcher) {} | |
| 47 ContentWatcher::~ContentWatcher() {} | |
| 48 | |
| 49 scoped_ptr<NativeHandler> ContentWatcher::MakeNatives() { | |
| 50 return scoped_ptr<NativeHandler>( | |
| 51 new MutationHandler(dispatcher_, weak_ptr_factory_.GetWeakPtr())); | |
| 52 } | |
| 53 | |
| 54 void ContentWatcher::OnWatchPages( | |
| 55 const std::vector<std::string>& new_css_selectors) { | |
| 56 if (new_css_selectors == css_selectors_) | |
| 57 return; | |
| 58 | |
| 59 css_selectors_ = new_css_selectors; | |
| 60 | |
| 61 for (std::map<WebKit::WebFrame*, | |
| 62 std::vector<base::StringPiece> >::iterator | |
| 63 it = matching_selectors_.begin(); | |
| 64 it != matching_selectors_.end(); ++it) { | |
| 65 WebKit::WebFrame* frame = it->first; | |
| 66 if (!css_selectors_.empty()) | |
| 67 EnsureWatchingMutations(frame); | |
| 68 | |
| 69 // Make sure to replace the contents of it->second because it contains | |
| 70 // dangling StringPieces that referred into the old css_selectors_ content. | |
| 71 it->second = FindMatchingSelectors(frame); | |
| 72 } | |
| 73 | |
| 74 // For each top-level frame, inform the browser about its new matching set of | |
| 75 // selectors. | |
| 76 struct NotifyVisitor : public content::RenderViewVisitor { | |
| 77 explicit NotifyVisitor(ContentWatcher* watcher) : watcher_(watcher) {} | |
| 78 virtual bool Visit(content::RenderView* view) { | |
| 79 watcher_->NotifyBrowserOfChange(view->GetWebView()->mainFrame()); | |
| 80 return true; // Continue visiting. | |
| 81 } | |
| 82 ContentWatcher* watcher_; | |
| 83 }; | |
| 84 NotifyVisitor visitor(this); | |
| 85 content::RenderView::ForEach(&visitor); | |
| 86 } | |
| 87 | |
| 88 void ContentWatcher::DidCreateDocumentElement(WebKit::WebFrame* frame) { | |
| 89 // Make sure the frame is represented in the matching_selectors_ map. | |
| 90 matching_selectors_[frame]; | |
| 91 | |
| 92 if (!css_selectors_.empty()) { | |
| 93 EnsureWatchingMutations(frame); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void ContentWatcher::EnsureWatchingMutations(WebKit::WebFrame* frame) { | |
| 98 v8::HandleScope scope; | |
| 99 v8::Context::Scope context_scope(frame->mainWorldScriptContext()); | |
| 100 if (ModuleSystem* module_system = GetModuleSystem(frame)) { | |
| 101 ModuleSystem::NativesEnabledScope scope(module_system); | |
| 102 module_system->Require("contentWatcher"); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 ModuleSystem* ContentWatcher::GetModuleSystem(WebKit::WebFrame* frame) const { | |
| 107 ChromeV8Context* v8_context = | |
| 108 dispatcher_->v8_context_set().GetByV8Context( | |
| 109 frame->mainWorldScriptContext()); | |
| 110 | |
| 111 if (!v8_context) | |
| 112 return NULL; | |
| 113 return v8_context->module_system(); | |
| 114 } | |
| 115 | |
| 116 void ContentWatcher::ScanAndNotify(WebKit::WebFrame* frame) { | |
| 117 std::vector<base::StringPiece> new_matches = FindMatchingSelectors(frame); | |
| 118 std::vector<base::StringPiece>& old_matches = matching_selectors_[frame]; | |
| 119 if (new_matches == old_matches) | |
| 120 return; | |
| 121 | |
| 122 using std::swap; | |
|
Matt Perry
2013/01/24 01:55:17
err... why :)
Jeffrey Yasskin
2013/01/24 03:10:22
It has the same effect in this case, because old_m
| |
| 123 swap(old_matches, new_matches); | |
| 124 NotifyBrowserOfChange(frame); | |
| 125 } | |
| 126 | |
| 127 std::vector<base::StringPiece> ContentWatcher::FindMatchingSelectors( | |
| 128 WebKit::WebFrame* frame) const { | |
| 129 std::vector<base::StringPiece> result; | |
| 130 v8::HandleScope scope; | |
| 131 | |
| 132 // Get the indices within |css_selectors_| that match elements in | |
| 133 // |frame|, as a JS Array. | |
| 134 v8::Local<v8::Value> selector_indices; | |
| 135 if (ModuleSystem* module_system = GetModuleSystem(frame)) { | |
| 136 v8::Context::Scope context_scope(frame->mainWorldScriptContext()); | |
| 137 v8::Local<v8::Array> js_css_selectors = | |
| 138 v8::Array::New(css_selectors_.size()); | |
| 139 for (size_t i = 0; i < css_selectors_.size(); ++i) { | |
| 140 js_css_selectors->Set(i, v8::String::New(css_selectors_[i].c_str(), | |
|
Matt Perry
2013/01/24 01:55:17
nit: use data() in conjunction with size() since c
Jeffrey Yasskin
2013/01/24 03:10:22
Done.
| |
| 141 css_selectors_[i].size())); | |
| 142 } | |
| 143 std::vector<v8::Handle<v8::Value> > find_selectors_args; | |
| 144 find_selectors_args.push_back(js_css_selectors); | |
| 145 selector_indices = module_system->CallModuleMethod("contentWatcher", | |
| 146 "FindMatchingSelectors", | |
| 147 &find_selectors_args); | |
| 148 } | |
| 149 if (selector_indices.IsEmpty() || !selector_indices->IsArray()) | |
| 150 return result; | |
| 151 | |
| 152 // Iterate over the array, and extract the indices, laboriously | |
| 153 // converting them back to integers. | |
| 154 v8::Local<v8::Array> index_array = selector_indices.As<v8::Array>(); | |
| 155 const size_t length = index_array->Length(); | |
| 156 result.reserve(length); | |
| 157 for (size_t i = 0; i < length; ++i) { | |
| 158 v8::Local<v8::Value> index_value = index_array->Get(i); | |
| 159 if (!index_value->IsNumber()) | |
| 160 continue; | |
| 161 double index = index_value->NumberValue(); | |
| 162 // Make sure the index is within bounds. | |
| 163 if (index < 0 || css_selectors_.size() <= index) | |
| 164 continue; | |
| 165 // Push a StringPiece referring to the CSS selector onto the result. | |
| 166 result.push_back( | |
| 167 base::StringPiece(css_selectors_[static_cast<size_t>(index)])); | |
| 168 } | |
| 169 return result; | |
| 170 } | |
| 171 | |
| 172 void ContentWatcher::NotifyBrowserOfChange( | |
| 173 WebKit::WebFrame* changed_frame) const { | |
| 174 WebKit::WebFrame* const top_frame = changed_frame->top(); | |
| 175 const WebKit::WebSecurityOrigin top_origin = | |
| 176 top_frame->document().securityOrigin(); | |
| 177 // Want to aggregate matched selectors from all frames where an | |
| 178 // extension with access to top_origin could run on the frame. | |
| 179 if (!top_origin.canAccess(changed_frame->document().securityOrigin())) { | |
| 180 // If the changed frame can't be accessed by the top frame, than | |
|
Matt Perry
2013/01/24 01:55:17
then*
Jeffrey Yasskin
2013/01/24 03:10:22
Oops.
| |
| 181 // no change in it could affect the set of selectors we'd send back. | |
| 182 return; | |
| 183 } | |
| 184 | |
| 185 std::set<base::StringPiece> transitive_selectors; | |
| 186 for (WebKit::WebFrame* frame = top_frame; frame; | |
| 187 frame = frame->traverseNext(/*wrap=*/false)) { | |
| 188 if (top_origin.canAccess(frame->document().securityOrigin())) { | |
| 189 std::map<WebKit::WebFrame*, | |
| 190 std::vector<base::StringPiece> >::const_iterator | |
| 191 frame_selectors = matching_selectors_.find(frame); | |
| 192 if (frame_selectors != matching_selectors_.end()) { | |
| 193 transitive_selectors.insert(frame_selectors->second.begin(), | |
| 194 frame_selectors->second.end()); | |
| 195 } | |
| 196 } | |
| 197 } | |
| 198 std::vector<std::string> selector_strings; | |
| 199 for (std::set<base::StringPiece>::const_iterator | |
| 200 it = transitive_selectors.begin(); | |
| 201 it != transitive_selectors.end(); ++it) | |
| 202 selector_strings.push_back(it->as_string()); | |
| 203 content::RenderView* view = | |
| 204 content::RenderView::FromWebView(top_frame->view()); | |
| 205 view->Send(new ExtensionHostMsg_OnWatchedPageChange( | |
| 206 view->GetRoutingID(), selector_strings)); | |
| 207 } | |
| 208 | |
| 209 } // namespace extensions | |
| OLD | NEW |