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
{
31 class ScriptInjection
;
33 // Manager for separate UserScriptSets, one for each shared memory region.
34 // Regions are organized as follows:
35 // static_scripts -- contains all extensions' scripts that are statically
36 // declared in the extension manifest.
37 // programmatic_scripts -- one region per extension containing only
38 // programmatically-declared scripts, instantiated
39 // when an extension first creates a declarative rule
40 // that would, if triggered, request a script injection.
41 class UserScriptSetManager
: public content::RenderProcessObserver
{
43 // Like a UserScriptSet::Observer, but automatically subscribes to all sets
44 // associated with the manager.
47 virtual void OnUserScriptsUpdated(
48 const std::set
<std::string
>& changed_extensions
,
49 const std::vector
<UserScript
*>& scripts
) = 0;
52 UserScriptSetManager(const ExtensionSet
* extensions
);
54 virtual ~UserScriptSetManager();
56 void AddObserver(Observer
* observer
);
57 void RemoveObserver(Observer
* observer
);
59 const UserScriptSet
* GetProgrammaticScriptsByExtension(
60 const ExtensionId
& extensionId
);
62 // Put all injections from |static_scripts| and each of
63 // |programmatic_scripts_| into |injections|.
64 void GetAllInjections(ScopedVector
<ScriptInjection
>* injections
,
65 blink::WebFrame
* web_frame
,
67 UserScript::RunLocation run_location
);
69 // Get active extension IDs from |static_scripts| and each of
70 // |programmatic_scripts_|.
71 void GetAllActiveExtensionIds(std::set
<std::string
>* ids
) const;
73 const UserScriptSet
* static_scripts() const { return &static_scripts_
; }
76 // Map for per-extension sets that may be defined programmatically.
77 typedef std::map
<ExtensionId
, linked_ptr
<UserScriptSet
> > UserScriptSetMap
;
79 // content::RenderProcessObserver implementation.
80 virtual bool OnControlMessageReceived(const IPC::Message
& message
) OVERRIDE
;
82 // Handle the UpdateUserScripts extension message.
83 void OnUpdateUserScripts(base::SharedMemoryHandle shared_memory
,
84 const ExtensionId
& extension_id
,
85 const std::set
<std::string
>& changed_extensions
);
87 // Scripts statically defined in extension manifests.
88 UserScriptSet static_scripts_
;
90 // Scripts programmatically-defined through API calls (initialized and stored
92 UserScriptSetMap programmatic_scripts_
;
94 // The set of all known extensions. Owned by the Dispatcher.
95 const ExtensionSet
* extensions_
;
97 // The associated observers.
98 ObserverList
<Observer
> observers_
;
100 DISALLOW_COPY_AND_ASSIGN(UserScriptSetManager
);
103 } // namespace extensions
105 #endif // EXTENSIONS_RENDERER_USER_SCRIPT_SET_MANAGER_H_