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
{
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
36 virtual void OnUserScriptsUpdated(
37 const std::set
<HostID
>& changed_hosts
,
38 const std::vector
<UserScript
*>& scripts
) = 0;
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
55 void GetInjections(ScopedVector
<ScriptInjection
>* injections
,
56 content::RenderFrame
* render_frame
,
58 UserScript::RunLocation run_location
);
60 scoped_ptr
<ScriptInjection
> GetDeclarativeScriptInjection(
62 content::RenderFrame
* render_frame
,
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(); }
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
,
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 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_