1 // Copyright 2020 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 CHROME_BROWSER_CONTENT_SETTINGS_PAGE_SPECIFIC_CONTENT_SETTINGS_DELEGATE_H_
6 #define CHROME_BROWSER_CONTENT_SETTINGS_PAGE_SPECIFIC_CONTENT_SETTINGS_DELEGATE_H_
7 
8 #include "build/build_config.h"
9 #include "chrome/common/custom_handlers/protocol_handler.h"
10 #include "components/content_settings/browser/page_specific_content_settings.h"
11 
12 #if !defined(OS_ANDROID)
13 #include "chrome/browser/browsing_data/access_context_audit_service.h"
14 #endif  // !defined(OS_ANDROID)
15 
16 namespace chrome {
17 
18 class PageSpecificContentSettingsDelegate
19     : public content_settings::PageSpecificContentSettings::Delegate,
20       public content::WebContentsObserver {
21  public:
22   explicit PageSpecificContentSettingsDelegate(
23       content::WebContents* web_contents);
24   ~PageSpecificContentSettingsDelegate() override;
25   PageSpecificContentSettingsDelegate(
26       const PageSpecificContentSettingsDelegate&) = delete;
27   PageSpecificContentSettingsDelegate& operator=(
28       const PageSpecificContentSettingsDelegate&) = delete;
29 
30   static PageSpecificContentSettingsDelegate* FromWebContents(
31       content::WebContents* web_contents);
32 
33   // Call to indicate that there is a protocol handler pending user approval.
set_pending_protocol_handler(const ProtocolHandler & handler)34   void set_pending_protocol_handler(const ProtocolHandler& handler) {
35     pending_protocol_handler_ = handler;
36   }
37 
pending_protocol_handler()38   const ProtocolHandler& pending_protocol_handler() const {
39     return pending_protocol_handler_;
40   }
41 
ClearPendingProtocolHandler()42   void ClearPendingProtocolHandler() {
43     pending_protocol_handler_ = ProtocolHandler::EmptyProtocolHandler();
44   }
45 
46   // Sets the previous protocol handler which will be replaced by the
47   // pending protocol handler.
set_previous_protocol_handler(const ProtocolHandler & handler)48   void set_previous_protocol_handler(const ProtocolHandler& handler) {
49     previous_protocol_handler_ = handler;
50   }
51 
previous_protocol_handler()52   const ProtocolHandler& previous_protocol_handler() const {
53     return previous_protocol_handler_;
54   }
55 
56   // Set whether the setting for the pending handler is DEFAULT (ignore),
57   // ALLOW, or DENY.
set_pending_protocol_handler_setting(ContentSetting setting)58   void set_pending_protocol_handler_setting(ContentSetting setting) {
59     pending_protocol_handler_setting_ = setting;
60   }
61 
pending_protocol_handler_setting()62   ContentSetting pending_protocol_handler_setting() const {
63     return pending_protocol_handler_setting_;
64   }
65 
66  private:
67   // PageSpecificContentSettings::Delegate:
68   void UpdateLocationBar() override;
69   void SetContentSettingRules(
70       content::RenderProcessHost* process,
71       const RendererContentSettingRules& rules) override;
72   PrefService* GetPrefs() override;
73   HostContentSettingsMap* GetSettingsMap() override;
74   ContentSetting GetEmbargoSetting(const GURL& request_origin,
75                                    ContentSettingsType permission) override;
76   std::vector<storage::FileSystemType> GetAdditionalFileSystemTypes() override;
77   browsing_data::CookieHelper::IsDeletionDisabledCallback
78   GetIsDeletionDisabledCallback() override;
79   bool IsMicrophoneCameraStateChanged(
80       content_settings::PageSpecificContentSettings::MicrophoneCameraState
81           microphone_camera_state,
82       const std::string& media_stream_selected_audio_device,
83       const std::string& media_stream_selected_video_device) override;
84   content_settings::PageSpecificContentSettings::MicrophoneCameraState
85   GetMicrophoneCameraState() override;
86   void OnContentAllowed(ContentSettingsType type) override;
87   void OnContentBlocked(ContentSettingsType type) override;
88   void OnCacheStorageAccessAllowed(const url::Origin& origin) override;
89   void OnCookieAccessAllowed(const net::CookieList& accessed_cookies) override;
90   void OnDomStorageAccessAllowed(const url::Origin& origin) override;
91   void OnFileSystemAccessAllowed(const url::Origin& origin) override;
92   void OnIndexedDBAccessAllowed(const url::Origin& origin) override;
93   void OnServiceWorkerAccessAllowed(const url::Origin& origin) override;
94   void OnWebDatabaseAccessAllowed(const url::Origin& origin) override;
95 
96   // content::WebContentsObserver:
97   void DidFinishNavigation(
98       content::NavigationHandle* navigation_handle) override;
99 
100   // The pending protocol handler, if any. This can be set if
101   // registerProtocolHandler was invoked without user gesture.
102   // The |IsEmpty| method will be true if no protocol handler is
103   // pending registration.
104   ProtocolHandler pending_protocol_handler_ =
105       ProtocolHandler::EmptyProtocolHandler();
106 
107   // The previous protocol handler to be replaced by
108   // the pending_protocol_handler_, if there is one. Empty if
109   // there is no handler which would be replaced.
110   ProtocolHandler previous_protocol_handler_ =
111       ProtocolHandler::EmptyProtocolHandler();
112 
113   // The setting on the pending protocol handler registration. Persisted in case
114   // the user opens the bubble and makes changes multiple times.
115   ContentSetting pending_protocol_handler_setting_ = CONTENT_SETTING_DEFAULT;
116 
117 #if !defined(OS_ANDROID)
118   std::unique_ptr<AccessContextAuditService::CookieAccessHelper>
119       cookie_access_helper_;
120 #endif  // !defined(OS_ANDROID)
121 };
122 
123 }  // namespace chrome
124 
125 #endif  // CHROME_BROWSER_CONTENT_SETTINGS_PAGE_SPECIFIC_CONTENT_SETTINGS_DELEGATE_H_
126