summaryrefslogtreecommitdiffstats
path: root/extensions/renderer/content_watcher.h
blob: 4480ce99cf73850a6b5029a67ef14b589231eb38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_RENDERER_CONTENT_WATCHER_H_
#define EXTENSIONS_RENDERER_CONTENT_WATCHER_H_

#include <map>
#include <set>
#include <string>
#include <vector>

#include "third_party/WebKit/public/platform/WebVector.h"

namespace blink {
class WebFrame;
class WebLocalFrame;
class WebString;
}

namespace extensions {
class Dispatcher;
class Extension;
class NativeHandler;

// Watches the content of WebFrames to notify extensions when they match various
// patterns.  This class tracks the set of relevant patterns (set by
// ExtensionMsg_WatchPages) and the set that match on each WebFrame, and sends a
// ExtensionHostMsg_OnWatchedPageChange whenever a RenderView's set changes.
//
// There's one ContentWatcher per Dispatcher rather than per RenderView because
// WebFrames can move between RenderViews through adoptNode.
// TODO(devlin): This class isn't OOPI-safe.
class ContentWatcher {
 public:
  ContentWatcher();
  ~ContentWatcher();

  // Handler for ExtensionMsg_WatchPages.
  void OnWatchPages(const std::vector<std::string>& css_selectors);

  // Uses WebDocument::watchCSSSelectors to watch the selectors in
  // css_selectors_ and get a callback into DidMatchCSS() whenever the set of
  // matching selectors in |frame| changes.
  void DidCreateDocumentElement(blink::WebLocalFrame* frame);

  // Records that |newly_matching_selectors| have started matching on |*frame|,
  // and |stopped_matching_selectors| have stopped matching.
  void DidMatchCSS(
      blink::WebLocalFrame* frame,
      const blink::WebVector<blink::WebString>& newly_matching_selectors,
      const blink::WebVector<blink::WebString>& stopped_matching_selectors);

 private:
  // Given that we saw a change in the CSS selectors that |changed_frame|
  // matched, tell the browser about the new set of matching selectors in its
  // top-level page.  We filter this so that if an extension were to be granted
  // activeTab permission on that top-level page, we only send CSS selectors for
  // frames that it could run on.
  void NotifyBrowserOfChange(blink::WebLocalFrame* changed_frame) const;

  // If any of these selectors match on a page, we need to send an
  // ExtensionHostMsg_OnWatchedPageChange back to the browser.
  blink::WebVector<blink::WebString> css_selectors_;

  // Maps live WebFrames to the set of CSS selectors they match. Blink sends
  // back diffs, which we apply to these sets.
  std::map<blink::WebFrame*, std::set<std::string> > matching_selectors_;
};

}  // namespace extensions

#endif  // EXTENSIONS_RENDERER_CONTENT_WATCHER_H_