summaryrefslogtreecommitdiffstats
path: root/extensions/renderer/user_script_set.h
blob: a371a94d730e34d75f746e578ce5eb26d6c1b329 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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_USER_SCRIPT_SET_H_
#define EXTENSIONS_RENDERER_USER_SCRIPT_SET_H_

#include <set>
#include <string>

#include "base/basictypes.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/shared_memory.h"
#include "base/observer_list.h"
#include "content/public/renderer/render_process_observer.h"
#include "extensions/common/user_script.h"

class GURL;

namespace blink {
class WebFrame;
}

namespace extensions {
class ExtensionSet;
class ScriptInjection;

// The UserScriptSet is a collection of UserScripts which knows how to update
// itself from SharedMemory and create ScriptInjections for UserScripts to
// inject on a page.
class UserScriptSet {
 public:
  class Observer {
   public:
    virtual void OnUserScriptsUpdated(
        const std::set<HostID>& changed_hosts,
        const std::vector<UserScript*>& scripts) = 0;
  };

  explicit UserScriptSet(const ExtensionSet* extensions);
  ~UserScriptSet();

  // Adds or removes observers.
  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Appends the ids of the extensions that have user scripts to |ids|.
  void GetActiveExtensionIds(std::set<std::string>* ids) const;

  // Append any ScriptInjections that should run on the given |web_frame| and
  // |tab_id|, at the given |run_location|, to |injections|.
  // |extensions| is passed in to verify the corresponding extension is still
  // valid.
  void GetInjections(ScopedVector<ScriptInjection>* injections,
                     blink::WebFrame* web_frame,
                     int tab_id,
                     UserScript::RunLocation run_location);

  scoped_ptr<ScriptInjection> GetDeclarativeScriptInjection(
      int script_id,
      blink::WebFrame* web_frame,
      int tab_id,
      UserScript::RunLocation run_location,
      const GURL& document_url);

  // Updates scripts given the shared memory region containing user scripts.
  // Returns true if the scripts were successfully updated.
  bool UpdateUserScripts(base::SharedMemoryHandle shared_memory,
                         const std::set<HostID>& changed_hosts);

  const std::vector<UserScript*>& scripts() const { return scripts_.get(); }

 private:
  // Returns a new ScriptInjection for the given |script| to execute in the
  // |web_frame|, or NULL if the script should not execute.
  scoped_ptr<ScriptInjection> GetInjectionForScript(
      UserScript* script,
      blink::WebFrame* web_frame,
      int tab_id,
      UserScript::RunLocation run_location,
      const GURL& document_url,
      bool is_declarative);

  // Shared memory containing raw script data.
  scoped_ptr<base::SharedMemory> shared_memory_;

  // The set of all known extensions. Owned by the Dispatcher.
  const ExtensionSet* extensions_;

  // The UserScripts this injector manages.
  ScopedVector<UserScript> scripts_;

  // The associated observers.
  ObserverList<Observer> observers_;

  DISALLOW_COPY_AND_ASSIGN(UserScriptSet);
};

}  // namespace extensions

#endif  // EXTENSIONS_RENDERER_USER_SCRIPT_SET_H_