1 // Copyright 2019 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.
4 
5 #ifndef WEBLAYER_BROWSER_BROWSER_CONTEXT_IMPL_H_
6 #define WEBLAYER_BROWSER_BROWSER_CONTEXT_IMPL_H_
7 
8 #include "base/files/file_path.h"
9 #include "build/build_config.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "weblayer/browser/download_manager_delegate_impl.h"
13 #include "weblayer/public/profile.h"
14 
15 namespace user_prefs {
16 class PrefRegistrySyncable;
17 }
18 class PrefService;
19 
20 namespace weblayer {
21 class ProfileImpl;
22 class ResourceContextImpl;
23 
24 class BrowserContextImpl : public content::BrowserContext {
25  public:
26   BrowserContextImpl(ProfileImpl* profile_impl, const base::FilePath& path);
27   ~BrowserContextImpl() override;
28   BrowserContextImpl(const BrowserContextImpl&) = delete;
29   BrowserContextImpl& operator=(const BrowserContextImpl&) = delete;
30 
31   static base::FilePath GetDefaultDownloadDirectory();
32 
33   // BrowserContext implementation:
34 #if !defined(OS_ANDROID)
35   std::unique_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
36       const base::FilePath&) override;
37 #endif  // !defined(OS_ANDROID)
38   base::FilePath GetPath() override;
39   bool IsOffTheRecord() override;
40   variations::VariationsClient* GetVariationsClient() override;
41   content::DownloadManagerDelegate* GetDownloadManagerDelegate() override;
42 
43   content::ResourceContext* GetResourceContext() override;
44   content::BrowserPluginGuestManager* GetGuestManager() override;
45   storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override;
46   content::PushMessagingService* GetPushMessagingService() override;
47   content::StorageNotificationService* GetStorageNotificationService() override;
48   content::SSLHostStateDelegate* GetSSLHostStateDelegate() override;
49   content::PermissionControllerDelegate* GetPermissionControllerDelegate()
50       override;
51   content::ClientHintsControllerDelegate* GetClientHintsControllerDelegate()
52       override;
53   content::BackgroundFetchDelegate* GetBackgroundFetchDelegate() override;
54   content::BackgroundSyncController* GetBackgroundSyncController() override;
55   content::BrowsingDataRemoverDelegate* GetBrowsingDataRemoverDelegate()
56       override;
57   download::InProgressDownloadManager* RetriveInProgressDownloadManager()
58       override;
59   content::ContentIndexProvider* GetContentIndexProvider() override;
60 
profile_impl()61   ProfileImpl* profile_impl() const { return profile_impl_; }
62 
pref_service()63   PrefService* pref_service() const { return user_pref_service_.get(); }
64 
65  private:
66   class WebLayerVariationsClient;
67 
68   // Creates a simple in-memory pref service.
69   // TODO(timvolodine): Investigate whether WebLayer needs persistent pref
70   // service.
71   void CreateUserPrefService();
72 
73   // Registers the preferences that WebLayer accesses.
74   void RegisterPrefs(user_prefs::PrefRegistrySyncable* pref_registry);
75 
76   ProfileImpl* const profile_impl_;
77   base::FilePath path_;
78   // ResourceContext needs to be deleted on the IO thread in general (and in
79   // particular due to the destruction of the safebrowsing mojo interface
80   // that has been added in ContentBrowserClient::ExposeInterfacesToRenderer
81   // on IO thread, see crbug.com/1029317). Also this is similar to how Chrome
82   // handles ProfileIOData.
83   // TODO(timvolodine): consider a more general Profile shutdown/destruction
84   // sequence for the IO/UI bits (crbug.com/1029879).
85   std::unique_ptr<ResourceContextImpl, content::BrowserThread::DeleteOnIOThread>
86       resource_context_;
87   DownloadManagerDelegateImpl download_delegate_;
88   std::unique_ptr<PrefService> user_pref_service_;
89   std::unique_ptr<WebLayerVariationsClient> weblayer_variations_client_;
90 };
91 }  // namespace weblayer
92 
93 #endif  // WEBLAYER_BROWSER_BROWSER_CONTEXT_IMPL_H_
94