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_
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"
26 namespace extensions
{
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
37 virtual void OnUserScriptsUpdated(
38 const std::set
<HostID
>& changed_hosts
,
39 const std::vector
<UserScript
*>& scripts
) = 0;
42 explicit UserScriptSet(const ExtensionSet
* extensions
);
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 |web_frame| and
53 // |tab_id|, at the given |run_location|, to |injections|.
54 // |extensions| is passed in to verify the corresponding extension is still
56 void GetInjections(ScopedVector
<ScriptInjection
>* injections
,
57 blink::WebFrame
* web_frame
,
59 UserScript::RunLocation run_location
);
61 scoped_ptr
<ScriptInjection
> GetDeclarativeScriptInjection(
63 blink::WebFrame
* web_frame
,
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
);
73 const std::vector
<UserScript
*>& scripts() const { return scripts_
.get(); }
76 // Returns a new ScriptInjection for the given |script| to execute in the
77 // |web_frame|, or NULL if the script should not execute.
78 scoped_ptr
<ScriptInjection
> GetInjectionForScript(
80 blink::WebFrame
* web_frame
,
82 UserScript::RunLocation run_location
,
83 const GURL
& document_url
,
86 // Shared memory containing raw script data.
87 scoped_ptr
<base::SharedMemory
> shared_memory_
;
89 // The set of all known extensions. Owned by the Dispatcher.
90 const ExtensionSet
* extensions_
;
92 // The UserScripts this injector manages.
93 ScopedVector
<UserScript
> scripts_
;
95 // The associated observers.
96 ObserverList
<Observer
> observers_
;
98 DISALLOW_COPY_AND_ASSIGN(UserScriptSet
);
101 } // namespace extensions
103 #endif // EXTENSIONS_RENDERER_USER_SCRIPT_SET_H_