Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / renderer / user_script_set.h
blobe5e91fd418b2158bdfa0852cbcb43467b592da97
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_USER_SCRIPT_SET_H_
6 #define EXTENSIONS_RENDERER_USER_SCRIPT_SET_H_
8 #include <set>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/memory/shared_memory.h"
16 #include "base/observer_list.h"
17 #include "content/public/renderer/render_process_observer.h"
18 #include "extensions/common/user_script.h"
20 class GURL;
22 namespace blink {
23 class WebFrame;
26 namespace extensions {
27 class Extension;
28 class ExtensionSet;
29 class ScriptInjection;
31 // The UserScriptSet is a collection of UserScripts which knows how to update
32 // itself from SharedMemory and create ScriptInjections for UserScripts to
33 // inject on a page.
34 class UserScriptSet {
35 public:
36 class Observer {
37 public:
38 virtual void OnUserScriptsUpdated(
39 const std::set<std::string>& changed_extensions,
40 const std::vector<UserScript*>& scripts) = 0;
43 explicit UserScriptSet(const ExtensionSet* extensions);
44 ~UserScriptSet();
46 // Adds or removes observers.
47 void AddObserver(Observer* observer);
48 void RemoveObserver(Observer* observer);
50 // Appends the ids of the extensions that have user scripts to |ids|.
51 void GetActiveExtensionIds(std::set<std::string>* ids) const;
53 // Populate |injections| with any ScriptInjections that should run on the
54 // given |web_frame| and |tab_id|, at the given |run_location|.
55 // |extensions| is passed in to verify the corresponding extension is still
56 // valid.
57 void GetInjections(ScopedVector<ScriptInjection>* injections,
58 blink::WebFrame* web_frame,
59 int tab_id,
60 UserScript::RunLocation run_location);
62 // Updates scripts given the shared memory region containing user scripts.
63 // Returns true if the scripts were successfully updated.
64 bool UpdateUserScripts(base::SharedMemoryHandle shared_memory,
65 const std::set<std::string>& changed_extensions);
67 const std::vector<UserScript*>& scripts() const { return scripts_.get(); }
69 private:
70 // Returns a new ScriptInjection for the given |script| to execute in the
71 // |web_frame|, or NULL if the script should not execute.
72 scoped_ptr<ScriptInjection> GetInjectionForScript(
73 UserScript* script,
74 blink::WebFrame* web_frame,
75 int tab_id,
76 UserScript::RunLocation run_location,
77 const GURL& document_url,
78 const Extension* extension);
80 // Shared memory containing raw script data.
81 scoped_ptr<base::SharedMemory> shared_memory_;
83 // The set of all known extensions. Owned by the Dispatcher.
84 const ExtensionSet* extensions_;
86 // The UserScripts this injector manages.
87 ScopedVector<UserScript> scripts_;
89 // The associated observers.
90 ObserverList<Observer> observers_;
92 DISALLOW_COPY_AND_ASSIGN(UserScriptSet);
95 } // namespace extensions
97 #endif // EXTENSIONS_RENDERER_USER_SCRIPT_SET_H_