Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / extensions / renderer / user_script_set.h
blob231722e8d1137920e11d5c5b96063af268a2e646
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 ScriptInjection;
29 // The UserScriptSet is a collection of UserScripts which knows how to update
30 // itself from SharedMemory and create ScriptInjections for UserScripts to
31 // inject on a page.
32 class UserScriptSet {
33 public:
34 class Observer {
35 public:
36 virtual void OnUserScriptsUpdated(
37 const std::set<HostID>& changed_hosts,
38 const std::vector<UserScript*>& scripts) = 0;
41 UserScriptSet();
42 ~UserScriptSet();
44 // Adds or removes observers.
45 void AddObserver(Observer* observer);
46 void RemoveObserver(Observer* observer);
48 // Appends the ids of the extensions that have user scripts to |ids|.
49 void GetActiveExtensionIds(std::set<std::string>* ids) const;
51 // Append any ScriptInjections that should run on the given |render_frame| and
52 // |tab_id|, at the given |run_location|, to |injections|.
53 // |extensions| is passed in to verify the corresponding extension is still
54 // valid.
55 void GetInjections(ScopedVector<ScriptInjection>* injections,
56 content::RenderFrame* render_frame,
57 int tab_id,
58 UserScript::RunLocation run_location);
60 scoped_ptr<ScriptInjection> GetDeclarativeScriptInjection(
61 int script_id,
62 content::RenderFrame* render_frame,
63 int tab_id,
64 UserScript::RunLocation run_location,
65 const GURL& document_url);
67 // Updates scripts given the shared memory region containing user scripts.
68 // Returns true if the scripts were successfully updated.
69 bool UpdateUserScripts(base::SharedMemoryHandle shared_memory,
70 const std::set<HostID>& changed_hosts,
71 bool whitelisted_only);
73 const std::vector<UserScript*>& scripts() const { return scripts_.get(); }
75 private:
76 // Returns a new ScriptInjection for the given |script| to execute in the
77 // |render_frame|, or NULL if the script should not execute.
78 scoped_ptr<ScriptInjection> GetInjectionForScript(
79 const UserScript* script,
80 content::RenderFrame* render_frame,
81 int tab_id,
82 UserScript::RunLocation run_location,
83 const GURL& document_url,
84 bool is_declarative);
86 // Shared memory containing raw script data.
87 scoped_ptr<base::SharedMemory> shared_memory_;
89 // The UserScripts this injector manages.
90 ScopedVector<UserScript> scripts_;
92 // The associated observers.
93 base::ObserverList<Observer> observers_;
95 DISALLOW_COPY_AND_ASSIGN(UserScriptSet);
98 } // namespace extensions
100 #endif // EXTENSIONS_RENDERER_USER_SCRIPT_SET_H_