Skip to content

RequestContextHandler

Mohammed Osama Mohamed Sayed Ahmed edited this page May 26, 2019 · 1 revision

Customize RequestContextHandler : IRequestContextHandler

public partial class RequestContextHandler : IRequestContextHandler {

        private ICookieManager customCookieManager;

        public ICookieManager GetCookieManager()
        {
            ///  return null;
            ///
            ///
            if (customCookieManager == null)
            {
                //In memory cookie manager	
                //customCookieManager = new CookieManager(null, persistSessionCookies: false, callback: null);

                //Store cookies in cookies directory (user must have write permission to this folder)
                customCookieManager = new CookieManager("cookies", persistSessionCookies: false, callback: null);
            }

            return customCookieManager;

            //NOTE: DO NOT RETURN A NEW COOKIE MANAGER EVERY TIME
            //This method will be called many times, you should return the same cookie manager within the scope
            //of the RequestContext (unless you REALLY know what your doing)
            //return new CookieManager("cookies", persistSessionCookies: false, callback: null);

            //Default to using the Global cookieManager (default)
            //return null;
        }

        public bool OnBeforePluginLoad(string mimeType, string url, bool isMainFrame, string topOriginUrl, WebPluginInfo pluginInfo, ref PluginPolicy pluginPolicy)
        {
            pluginPolicy = PluginPolicy.Allow;
            return true;

            // return false;
        }

        public void OnRequestContextInitialized(IRequestContext requestContext)
        {
            string error;
            string errorMessage;
             string SettingsPreference;

            requestContext.SetPreference("spellcheck.dictionaries", new List<object> { "en-US", "ru", "ar", "zh-CN", "ja" }, out error);
            requestContext.SetPreference("spellcheck.dictionary", "en-US", out error);
            requestContext.SetPreference("webkit.webprefs.plugins_enabled", true, out errorMessage);
            requestContext.SetPreference("spellcheck.use_spelling_service", true, out errorMessage);
            requestContext.SetPreference("browser.enable_spellchecking", true, out errorMessage);


            ///
            ///
            ///

            // with this chrome still asks for permission
            requestContext.SetPreference("profile.managed_default_content_settings.media_stream", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.media_stream_camera", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.media_stream_mic", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.Images", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.plugins", 1, out SettingsPreference); //1

            requestContext.SetPreference("profile.managed_default_content_settings.javascript", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.geolocation", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.important_site_info", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.accessibility_events", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.protocol_handler", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.app_banner", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.site_engagement", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.payment_handler", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.managed_default_content_settings.cookies", 1, out SettingsPreference); //1
            requestContext.SetPreference("profile.content_settings.exceptions.geolocation", 1, out SettingsPreference); //1

            //

            // Settings Preference
            requestContext.SetPreference("profile.content_settings.exceptions.cookies.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.cookies.http://*,*.last_used", 1, out SettingsPreference); //https

            requestContext.SetPreference("profile.content_settings.exceptions.javascript.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.javascript.http://*,*.last_used", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.protocol_handler.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.protocol_handler.http://*,*.last_used", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.app_banner.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.app_banner.http://*,*.last_used", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.site_engagement.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.site_engagement.http://*,*.last_used", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.payment_handler.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.payment_handler.http://*,*.last_used", 1, out SettingsPreference); //https

            // Background sync
            requestContext.SetPreference("profile.managed_default_content_settings.background_sync", 1, out SettingsPreference); //1

            // Settings Preference Background sync
            requestContext.SetPreference("profile.content_settings.exceptions.background_sync.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.background_sync.http://*,*.last_used", 1, out SettingsPreference); //https

            //
            requestContext.SetPreference("profile.content_settings.exceptions.accessibility_events.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.accessibility_events.http://*,*.last_used", 1, out SettingsPreference); //https

            // and this prevents chrome from starting
            requestContext.SetPreference("profile.content_settings.exceptions.media_stream_mic.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.media_stream_mic.http://*,*.last_used", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.media_stream_camera.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.media_stream_camera.http://*,*.last_used", 1, out SettingsPreference); //https

            // Permission
            requestContext.SetPreference("profile.content_settings.exceptions.important_site_info.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.important_site_info.http://*,*.last_used", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.plugins.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.plugins.http://*,*.last_used", 1, out SettingsPreference); //https   
            requestContext.SetPreference("profile.content_settings.exceptions.geolocation.http://*,*.setting", 1, out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.exceptions.geolocation.http://*,*.last_used", 1, out SettingsPreference); //https

            // and this prevents chrome from starting as well
            requestContext.SetPreference("profile.content_settings.pattern_pairs.http://*,*.media_stream.video", "Allow", out SettingsPreference); //https
            requestContext.SetPreference("profile.content_settings.pattern_pairs.http://*,*.media_stream.audio", "Allow", out SettingsPreference); //https

            // change default camera using
            requestContext.SetPreference("media.default_video_capture_Device", "\\\\?\\root#media#0002#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\\global", out SettingsPreference);

  } }