1 // Copyright 2015 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_BROWSER_EXTENSION_USER_SCRIPT_LOADER_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_USER_SCRIPT_LOADER_H_
8 #include "base/memory/shared_memory.h"
9 #include "extensions/browser/extension_registry_observer.h"
10 #include "extensions/browser/user_script_loader.h"
11 #include "extensions/common/extension.h"
17 namespace extensions
{
19 class ContentVerifier
;
20 class ExtensionRegistry
;
22 // UserScriptLoader for extensions.
23 class ExtensionUserScriptLoader
: public UserScriptLoader
,
24 public ExtensionRegistryObserver
{
26 using PathAndDefaultLocale
= std::pair
<base::FilePath
, std::string
>;
27 using HostsInfo
= std::map
<HostID
, PathAndDefaultLocale
>;
29 // The listen_for_extension_system_loaded is only set true when initilizing
30 // the Extension System, e.g, when constructs SharedUserScriptMaster in
31 // ExtensionSystemImpl.
32 ExtensionUserScriptLoader(content::BrowserContext
* browser_context
,
33 const HostID
& host_id
,
34 bool listen_for_extension_system_loaded
);
35 ~ExtensionUserScriptLoader() override
;
37 // A wrapper around the method to load user scripts, which is normally run on
38 // the file thread. Exposed only for tests.
39 void LoadScriptsForTest(UserScriptList
* user_scripts
);
43 void LoadScripts(scoped_ptr
<UserScriptList
> user_scripts
,
44 const std::set
<HostID
>& changed_hosts
,
45 const std::set
<int>& added_script_ids
,
46 LoadScriptsCallback callback
) override
;
48 // Updates |hosts_info_| to contain info for each element of
50 void UpdateHostsInfo(const std::set
<HostID
>& changed_hosts
);
52 // ExtensionRegistryObserver:
53 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
54 const Extension
* extension
,
55 UnloadedExtensionInfo::Reason reason
) override
;
57 // Initiates script load when we have been waiting for the extension system
59 void OnExtensionSystemReady();
61 // Maps host info needed for localization to a host ID.
62 HostsInfo hosts_info_
;
64 // Manages content verification of the loaded user scripts.
65 scoped_refptr
<ContentVerifier
> content_verifier_
;
67 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
68 extension_registry_observer_
;
70 base::WeakPtrFactory
<ExtensionUserScriptLoader
> weak_factory_
;
72 DISALLOW_COPY_AND_ASSIGN(ExtensionUserScriptLoader
);
75 } // namespace extensions
77 #endif // EXTENSIONS_BROWSER_EXTENSION_USER_SCRIPT_LOADER_H_