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