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_MANAGER_H_
6 #define EXTENSIONS_RENDERER_USER_SCRIPT_SET_MANAGER_H_
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/shared_memory.h"
14 #include "base/observer_list.h"
15 #include "content/public/renderer/render_process_observer.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/user_script.h"
18 #include "extensions/renderer/user_script_set.h"
28 namespace extensions
{
30 class ScriptInjection
;
32 // Manager for separate UserScriptSets, one for each shared memory region.
33 // Regions are organized as follows:
34 // static_scripts -- contains all extensions' scripts that are statically
35 // declared in the extension manifest.
36 // programmatic_scripts -- one region per host (extension or WebUI) containing
37 // only programmatically-declared scripts, instantiated
38 // when an extension first creates a declarative rule
39 // that would, if triggered, request a script injection.
40 class UserScriptSetManager
: public content::RenderProcessObserver
{
42 // Like a UserScriptSet::Observer, but automatically subscribes to all sets
43 // associated with the manager.
46 virtual void OnUserScriptsUpdated(
47 const std::set
<HostID
>& changed_hosts
,
48 const std::vector
<UserScript
*>& scripts
) = 0;
51 UserScriptSetManager();
53 ~UserScriptSetManager() override
;
55 void AddObserver(Observer
* observer
);
56 void RemoveObserver(Observer
* observer
);
58 // Looks up the script injection associated with |script_id| and
59 // |extension_id| in the context of the given |web_frame|, |tab_id|,
61 scoped_ptr
<ScriptInjection
> GetInjectionForDeclarativeScript(
63 content::RenderFrame
* render_frame
,
66 const std::string
& extension_id
);
68 // Append all injections from |static_scripts| and each of
69 // |programmatic_scripts_| to |injections|.
70 void GetAllInjections(ScopedVector
<ScriptInjection
>* injections
,
71 content::RenderFrame
* render_frame
,
73 UserScript::RunLocation run_location
);
75 // Get active extension IDs from |static_scripts| and each of
76 // |programmatic_scripts_|.
77 void GetAllActiveExtensionIds(std::set
<std::string
>* ids
) const;
79 const UserScriptSet
* static_scripts() const { return &static_scripts_
; }
82 // Map for per-extension sets that may be defined programmatically.
83 typedef std::map
<HostID
, linked_ptr
<UserScriptSet
> > UserScriptSetMap
;
85 // content::RenderProcessObserver implementation.
86 bool OnControlMessageReceived(const IPC::Message
& message
) override
;
88 UserScriptSet
* GetProgrammaticScriptsByHostID(const HostID
& host_id
);
90 // Handle the UpdateUserScripts extension message.
91 void OnUpdateUserScripts(base::SharedMemoryHandle shared_memory
,
92 const HostID
& host_id
,
93 const std::set
<HostID
>& changed_hosts
,
94 bool whitelisted_only
);
96 // Scripts statically defined in extension manifests.
97 UserScriptSet static_scripts_
;
99 // Scripts programmatically-defined through API calls (initialized and stored
101 UserScriptSetMap programmatic_scripts_
;
103 // The associated observers.
104 base::ObserverList
<Observer
> observers_
;
106 DISALLOW_COPY_AND_ASSIGN(UserScriptSetManager
);
109 } // namespace extensions
111 #endif // EXTENSIONS_RENDERER_USER_SCRIPT_SET_MANAGER_H_