[Extensions OOPI] Clean up script injection for OOPI more
[chromium-blink-merge.git] / extensions / renderer / user_script_set.h
bloba39a543bc3789fb30f54599f2129596055c50def
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 content {
23 class RenderFrame;
26 namespace extensions {
27 class ExtensionSet;
28 class ScriptInjection;
30 // The UserScriptSet is a collection of UserScripts which knows how to update
31 // itself from SharedMemory and create ScriptInjections for UserScripts to
32 // inject on a page.
33 class UserScriptSet {
34 public:
35 class Observer {
36 public:
37 virtual void OnUserScriptsUpdated(
38 const std::set<HostID>& changed_hosts,
39 const std::vector<UserScript*>& scripts) = 0;
42 explicit UserScriptSet(const ExtensionSet* extensions);
43 ~UserScriptSet();
45 // Adds or removes observers.
46 void AddObserver(Observer* observer);
47 void RemoveObserver(Observer* observer);
49 // Appends the ids of the extensions that have user scripts to |ids|.
50 void GetActiveExtensionIds(std::set<std::string>* ids) const;
52 // Append any ScriptInjections that should run on the given |render_frame| and
53 // |tab_id|, at the given |run_location|, to |injections|.
54 // |extensions| is passed in to verify the corresponding extension is still
55 // valid.
56 void GetInjections(ScopedVector<ScriptInjection>* injections,
57 content::RenderFrame* render_frame,
58 int tab_id,
59 UserScript::RunLocation run_location);
61 scoped_ptr<ScriptInjection> GetDeclarativeScriptInjection(
62 int script_id,
63 content::RenderFrame* render_frame,
64 int tab_id,
65 UserScript::RunLocation run_location,
66 const GURL& document_url);
68 // Updates scripts given the shared memory region containing user scripts.
69 // Returns true if the scripts were successfully updated.
70 bool UpdateUserScripts(base::SharedMemoryHandle shared_memory,
71 const std::set<HostID>& changed_hosts,
72 bool whitelisted_only);
74 const std::vector<UserScript*>& scripts() const { return scripts_.get(); }
76 private:
77 // Returns a new ScriptInjection for the given |script| to execute in the
78 // |render_frame|, or NULL if the script should not execute.
79 scoped_ptr<ScriptInjection> GetInjectionForScript(
80 const UserScript* script,
81 content::RenderFrame* render_frame,
82 int tab_id,
83 UserScript::RunLocation run_location,
84 const GURL& document_url,
85 bool is_declarative);
87 // Shared memory containing raw script data.
88 scoped_ptr<base::SharedMemory> shared_memory_;
90 // The set of all known extensions. Owned by the Dispatcher.
91 const ExtensionSet* extensions_;
93 // The UserScripts this injector manages.
94 ScopedVector<UserScript> scripts_;
96 // The associated observers.
97 base::ObserverList<Observer> observers_;
99 DISALLOW_COPY_AND_ASSIGN(UserScriptSet);
102 } // namespace extensions
104 #endif // EXTENSIONS_RENDERER_USER_SCRIPT_SET_H_