Roll src/third_party/WebKit 9301d6f:4619053 (svn 201058:201059)
[chromium-blink-merge.git] / extensions / renderer / content_watcher.h
blob4480ce99cf73850a6b5029a67ef14b589231eb38
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.
5 #ifndef EXTENSIONS_RENDERER_CONTENT_WATCHER_H_
6 #define EXTENSIONS_RENDERER_CONTENT_WATCHER_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "third_party/WebKit/public/platform/WebVector.h"
15 namespace blink {
16 class WebFrame;
17 class WebLocalFrame;
18 class WebString;
21 namespace extensions {
22 class Dispatcher;
23 class Extension;
24 class NativeHandler;
26 // 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 // ExtensionMsg_WatchPages) and the set that match on each WebFrame, and sends a
29 // ExtensionHostMsg_OnWatchedPageChange whenever a RenderView's set changes.
31 // There's one ContentWatcher per Dispatcher rather than per RenderView because
32 // WebFrames can move between RenderViews through adoptNode.
33 // TODO(devlin): This class isn't OOPI-safe.
34 class ContentWatcher {
35 public:
36 ContentWatcher();
37 ~ContentWatcher();
39 // Handler for ExtensionMsg_WatchPages.
40 void OnWatchPages(const std::vector<std::string>& css_selectors);
42 // Uses WebDocument::watchCSSSelectors to watch the selectors in
43 // css_selectors_ and get a callback into DidMatchCSS() whenever the set of
44 // matching selectors in |frame| changes.
45 void DidCreateDocumentElement(blink::WebLocalFrame* frame);
47 // Records that |newly_matching_selectors| have started matching on |*frame|,
48 // and |stopped_matching_selectors| have stopped matching.
49 void DidMatchCSS(
50 blink::WebLocalFrame* frame,
51 const blink::WebVector<blink::WebString>& newly_matching_selectors,
52 const blink::WebVector<blink::WebString>& stopped_matching_selectors);
54 private:
55 // Given that we saw a change in the CSS selectors that |changed_frame|
56 // matched, tell the browser about the new set of matching selectors in its
57 // top-level page. We filter this so that if an extension were to be granted
58 // activeTab permission on that top-level page, we only send CSS selectors for
59 // frames that it could run on.
60 void NotifyBrowserOfChange(blink::WebLocalFrame* changed_frame) const;
62 // If any of these selectors match on a page, we need to send an
63 // ExtensionHostMsg_OnWatchedPageChange back to the browser.
64 blink::WebVector<blink::WebString> css_selectors_;
66 // Maps live WebFrames to the set of CSS selectors they match. Blink sends
67 // back diffs, which we apply to these sets.
68 std::map<blink::WebFrame*, std::set<std::string> > matching_selectors_;
71 } // namespace extensions
73 #endif // EXTENSIONS_RENDERER_CONTENT_WATCHER_H_