| 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 #ifndef CHROME_RENDERER_EXTENSIONS_CONTENT_WATCHER_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_CONTENT_WATCHER_H_ |
| 6 #define CHROME_RENDERER_EXTENSIONS_CONTENT_WATCHER_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_CONTENT_WATCHER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> |
| 9 #include <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "third_party/WebKit/public/platform/WebVector.h" |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/strings/string_piece.h" | |
| 15 #include "v8/include/v8.h" | |
| 16 | 14 |
| 17 namespace WebKit { | 15 namespace WebKit { |
| 18 class WebFrame; | 16 class WebFrame; |
| 17 class WebString; |
| 19 } | 18 } |
| 20 | 19 |
| 21 namespace extensions { | 20 namespace extensions { |
| 22 class Dispatcher; | 21 class Dispatcher; |
| 23 class Extension; | 22 class Extension; |
| 24 class NativeHandler; | 23 class NativeHandler; |
| 25 | 24 |
| 26 // Watches the content of WebFrames to notify extensions when they match various | 25 // Watches the content of WebFrames to notify extensions when they match various |
| 27 // patterns. This class tracks the set of relevant patterns (set by | 26 // patterns. This class tracks the set of relevant patterns (set by |
| 28 // ExtensionMsg_WatchPages) and the set that match on each WebFrame, and sends a | 27 // ExtensionMsg_WatchPages) and the set that match on each WebFrame, and sends a |
| 29 // ExtensionHostMsg_OnWatchedPageChange whenever a RenderView's set changes. | 28 // ExtensionHostMsg_OnWatchedPageChange whenever a RenderView's set changes. |
| 30 // | 29 // |
| 31 // There's one ContentWatcher per Dispatcher rather than per RenderView because | 30 // There's one ContentWatcher per Dispatcher rather than per RenderView because |
| 32 // WebFrames can move between RenderViews through adoptNode. | 31 // WebFrames can move between RenderViews through adoptNode. |
| 33 class ContentWatcher { | 32 class ContentWatcher { |
| 34 public: | 33 public: |
| 35 explicit ContentWatcher(Dispatcher* dispatcher); | 34 ContentWatcher(); |
| 36 ~ContentWatcher(); | 35 ~ContentWatcher(); |
| 37 | 36 |
| 38 // Returns the callback to call on a frame change. | |
| 39 scoped_ptr<NativeHandler> MakeNatives(ChromeV8Context* context); | |
| 40 | |
| 41 // Handler for ExtensionMsg_WatchPages. | 37 // Handler for ExtensionMsg_WatchPages. |
| 42 void OnWatchPages(const std::vector<std::string>& css_selectors); | 38 void OnWatchPages(const std::vector<std::string>& css_selectors); |
| 43 | 39 |
| 44 // Registers the MutationObserver to call back into this object whenever the | 40 // Uses WebDocument::watchCSSSelectors to watch the selectors in |
| 45 // content of |frame| changes. | 41 // css_selectors_ and get a callback into DidMatchCSS() whenever the set of |
| 42 // matching selectors in |frame| changes. |
| 46 void DidCreateDocumentElement(WebKit::WebFrame* frame); | 43 void DidCreateDocumentElement(WebKit::WebFrame* frame); |
| 47 | 44 |
| 48 // Scans *frame for the current set of interesting CSS selectors, and if | 45 // Records that |newly_matching_selectors| have started matching on |*frame|, |
| 49 // they've changed sends ExtensionHostMsg_OnWatchedPageChange back to the | 46 // and |stopped_matching_selectors| have stopped matching. |
| 50 // RenderViewHost that owns the frame. | 47 void DidMatchCSS( |
| 51 void ScanAndNotify(WebKit::WebFrame* frame); | 48 WebKit::WebFrame* frame, |
| 49 const WebKit::WebVector<WebKit::WebString>& newly_matching_selectors, |
| 50 const WebKit::WebVector<WebKit::WebString>& stopped_matching_selectors); |
| 52 | 51 |
| 53 private: | 52 private: |
| 54 void EnsureWatchingMutations(WebKit::WebFrame* frame); | |
| 55 | |
| 56 ModuleSystem* GetModuleSystem(WebKit::WebFrame* frame) const; | |
| 57 std::vector<base::StringPiece> FindMatchingSelectors( | |
| 58 WebKit::WebFrame* frame) const; | |
| 59 | |
| 60 // Given that we saw a change in the CSS selectors that |changed_frame| | 53 // Given that we saw a change in the CSS selectors that |changed_frame| |
| 61 // matched, tell the browser about the new set of matching selectors in its | 54 // matched, tell the browser about the new set of matching selectors in its |
| 62 // top-level page. We filter this so that if an extension were to be granted | 55 // top-level page. We filter this so that if an extension were to be granted |
| 63 // activeTab permission on that top-level page, we only send CSS selectors for | 56 // activeTab permission on that top-level page, we only send CSS selectors for |
| 64 // frames that it could run on. | 57 // frames that it could run on. |
| 65 void NotifyBrowserOfChange(WebKit::WebFrame* changed_frame) const; | 58 void NotifyBrowserOfChange(WebKit::WebFrame* changed_frame) const; |
| 66 | 59 |
| 67 base::WeakPtrFactory<ContentWatcher> weak_ptr_factory_; | |
| 68 Dispatcher* dispatcher_; | |
| 69 | |
| 70 // If any of these selectors match on a page, we need to send an | 60 // If any of these selectors match on a page, we need to send an |
| 71 // ExtensionHostMsg_OnWatchedPageChange back to the browser. | 61 // ExtensionHostMsg_OnWatchedPageChange back to the browser. |
| 72 std::vector<std::string> css_selectors_; | 62 WebKit::WebVector<WebKit::WebString> css_selectors_; |
| 73 | 63 |
| 74 // Maps live WebFrames to the set of CSS selectors they match. This lets us | 64 // Maps live WebFrames to the set of CSS selectors they match. Blink sends |
| 75 // traverse a top-level frame's sub-frames without rescanning them all each | 65 // back diffs, which we apply to these sets. |
| 76 // time any one changes. | 66 std::map<WebKit::WebFrame*, std::set<std::string> > matching_selectors_; |
| 77 // | |
| 78 // The StringPieces point into css_selectors_ above, so when it changes, they | |
| 79 // all need to be regenerated. | |
| 80 std::map<WebKit::WebFrame*, | |
| 81 std::vector<base::StringPiece> > matching_selectors_; | |
| 82 }; | 67 }; |
| 83 | 68 |
| 84 } // namespace extensions | 69 } // namespace extensions |
| 85 | 70 |
| 86 #endif // CHROME_RENDERER_EXTENSIONS_CONTENT_WATCHER_H_ | 71 #endif // CHROME_RENDERER_EXTENSIONS_CONTENT_WATCHER_H_ |
| OLD | NEW |