Home | History | Annotate | Download | only in renderer
      1 // Copyright 2014 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 #ifndef EXTENSIONS_RENDERER_CONTENT_WATCHER_H_
      6 #define EXTENSIONS_RENDERER_CONTENT_WATCHER_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "third_party/WebKit/public/platform/WebVector.h"
     14 
     15 namespace blink {
     16 class WebFrame;
     17 class WebString;
     18 }
     19 
     20 namespace extensions {
     21 class Dispatcher;
     22 class Extension;
     23 class NativeHandler;
     24 
     25 // Watches the content of WebFrames to notify extensions when they match various
     26 // patterns.  This class tracks the set of relevant patterns (set by
     27 // ExtensionMsg_WatchPages) and the set that match on each WebFrame, and sends a
     28 // ExtensionHostMsg_OnWatchedPageChange whenever a RenderView's set changes.
     29 //
     30 // There's one ContentWatcher per Dispatcher rather than per RenderView because
     31 // WebFrames can move between RenderViews through adoptNode.
     32 class ContentWatcher {
     33  public:
     34   ContentWatcher();
     35   ~ContentWatcher();
     36 
     37   // Handler for ExtensionMsg_WatchPages.
     38   void OnWatchPages(const std::vector<std::string>& css_selectors);
     39 
     40   // Uses WebDocument::watchCSSSelectors to watch the selectors in
     41   // css_selectors_ and get a callback into DidMatchCSS() whenever the set of
     42   // matching selectors in |frame| changes.
     43   void DidCreateDocumentElement(blink::WebFrame* frame);
     44 
     45   // Records that |newly_matching_selectors| have started matching on |*frame|,
     46   // and |stopped_matching_selectors| have stopped matching.
     47   void DidMatchCSS(
     48       blink::WebFrame* frame,
     49       const blink::WebVector<blink::WebString>& newly_matching_selectors,
     50       const blink::WebVector<blink::WebString>& stopped_matching_selectors);
     51 
     52  private:
     53   // Given that we saw a change in the CSS selectors that |changed_frame|
     54   // matched, tell the browser about the new set of matching selectors in its
     55   // top-level page.  We filter this so that if an extension were to be granted
     56   // activeTab permission on that top-level page, we only send CSS selectors for
     57   // frames that it could run on.
     58   void NotifyBrowserOfChange(blink::WebFrame* changed_frame) const;
     59 
     60   // If any of these selectors match on a page, we need to send an
     61   // ExtensionHostMsg_OnWatchedPageChange back to the browser.
     62   blink::WebVector<blink::WebString> css_selectors_;
     63 
     64   // Maps live WebFrames to the set of CSS selectors they match. Blink sends
     65   // back diffs, which we apply to these sets.
     66   std::map<blink::WebFrame*, std::set<std::string> > matching_selectors_;
     67 };
     68 
     69 }  // namespace extensions
     70 
     71 #endif  // EXTENSIONS_RENDERER_CONTENT_WATCHER_H_
     72