Пример #1
0
 public CustomWebView(CefRequestContext requestContext)
 {
     this.RequestContext = requestContext;
 }
Пример #2
0
        protected virtual void OnCreateBrowser()
        {
            if (this.Opener != null)
            {
                return;
            }

            if (GetState(State.Creating) || GetState(State.Created))
            {
                throw new InvalidOperationException();
            }

            SetState(State.Creating, true);

            Dictionary <InitialPropertyKeys, object> propertyBag = InitialPropertyBag;

            InitialPropertyBag = null;

            var avaloniaWindow = this.GetVisualRoot() as Window;

            if (avaloniaWindow is null)
            {
                throw new InvalidOperationException("Window not found!");
            }

            using (var windowInfo = new CefWindowInfo())
            {
                IPlatformHandle platformHandle = avaloniaWindow.PlatformImpl.Handle;
                if (platformHandle is IMacOSTopLevelPlatformHandle macOSHandle)
                {
                    windowInfo.SetAsWindowless(macOSHandle.GetNSWindowRetained());
                }
                else
                {
                    windowInfo.SetAsWindowless(platformHandle.Handle);
                }

                string             initialUrl      = null;
                CefDictionaryValue extraInfo       = null;
                CefRequestContext  requestContext  = null;
                CefBrowserSettings browserSettings = null;
                if (propertyBag != null)
                {
                    object value;
                    if (propertyBag.TryGetValue(InitialPropertyKeys.Url, out value))
                    {
                        initialUrl = value as string;
                    }
                    if (propertyBag.TryGetValue(InitialPropertyKeys.BrowserSettings, out value))
                    {
                        browserSettings = value as CefBrowserSettings;
                    }
                    if (propertyBag.TryGetValue(InitialPropertyKeys.RequestContext, out value))
                    {
                        requestContext = value as CefRequestContext;
                    }
                    if (propertyBag.TryGetValue(InitialPropertyKeys.ExtraInfo, out value))
                    {
                        extraInfo = value as CefDictionaryValue;
                    }
                }

                if (initialUrl == null)
                {
                    initialUrl = "about:blank";
                }
                if (browserSettings == null)
                {
                    browserSettings = DefaultBrowserSettings;
                }

                if (!CefApi.CreateBrowser(windowInfo, ViewGlue.Client, initialUrl, browserSettings, extraInfo, requestContext))
                {
                    throw new InvalidOperationException("Failed to create browser instance.");
                }
            }
        }
Пример #3
0
        protected override void OnHandleCreated(EventArgs e)
        {
            if (DesignMode)
            {
                if (!_handleCreated)
                {
                    Paint += PaintInDesignMode;
                }
            }
            else
            {
                var windowInfo = CefWindowInfo.Create();
                windowInfo.SetAsChild(Handle, new CefRectangle {
                    X = 0, Y = 0, Width = Width, Height = Height
                });

                var client   = CreateWebClient();
                var settings = BrowserSettings;
                if (settings == null)
                {
                    settings = new CefBrowserSettings();
                    settings.AcceptLanguageList          = "zh-CN";          //设置默认为;
                    settings.FileAccessFromFileUrls      = CefState.Enabled; // CefCommandLine.Create("allow-access-from.file");
                    settings.UniversalAccessFromFileUrls = CefState.Enabled;
                    settings.ApplicationCache            = CefState.Enabled;
                    settings.JavaScript = CefState.Enabled;
                    settings.JavaScriptAccessClipboard = CefState.Enabled;
                    settings.JavaScriptDomPaste        = CefState.Enabled;
                    settings.LocalStorage               = CefState.Enabled;
                    settings.WebGL                      = CefState.Disabled;
                    settings.Databases                  = CefState.Enabled;
                    settings.ImageLoading               = CefState.Enabled;
                    settings.WebSecurity                = CefState.Enabled;
                    settings.JavaScriptCloseWindows     = CefState.Enabled;
                    settings.DefaultEncoding            = "UTF8";
                    settings.ImageLoading               = CefState.Enabled;
                    settings.ImageShrinkStandaloneToFit = CefState.Enabled;
                }


                //判断隔离独立页面的Cookie
                if (multiUsersMode)
                {
                    tabIndexNumber++;
                    CefRequestContextSettings contextSettings = new CefRequestContextSettings();
                    contextSettings.PersistSessionCookies   = false;
                    contextSettings.PersistUserPreferences  = true;
                    contextSettings.AcceptLanguageList      = "zh-CN";
                    contextSettings.IgnoreCertificateErrors = true;
                    CefWebRequestContextHandler requestContextHandler = new CefWebRequestContextHandler();
                    CefRequestContext           requestContext        = CefRequestContext.CreateContext(contextSettings, requestContextHandler);
                    CefBrowserHost.CreateBrowser(windowInfo, client, settings, StartUrl, null, requestContext); //隔离Cookie
                    contextSettings = null;
                    requestContext  = null;

                    GC.Collect();
                }
                else
                {
                    //CefRequestContextSettings contextSettings = new CefRequestContextSettings();
                    //contextSettings.PersistSessionCookies = true;
                    //contextSettings.PersistUserPreferences = true;
                    //contextSettings.CachePath = GetAppDir("Cache");
                    //contextSettings.AcceptLanguageList = "zh-CN";
                    //// contextSettings.EnableNetSecurityExpiration = false;
                    //contextSettings.IgnoreCertificateErrors = true;
                    //CefWebRequestContextHandler requestContextHandler = new CefWebRequestContextHandler();

                    //CefRequestContext requestContext = CefRequestContext.CreateContext(contextSettings, requestContextHandler);
                    //CefBrowserHost.CreateBrowser(windowInfo, client, settings, StartUrl, null, requestContext);
                    //contextSettings = null;
                    //requestContext = null;

                    //GC.Collect();
                    CefBrowserHost.CreateBrowser(windowInfo, client, settings, StartUrl);
                }
                windowInfo = null;
                client     = null;
                settings   = null;
                GC.Collect();
            }

            _handleCreated = true;
            base.OnHandleCreated(e);
        }
Пример #4
0
        protected virtual void OnCreateBrowser()
        {
            if (this.Opener != null)
            {
                return;
            }

            if (GetState(State.Creating) || GetState(State.Created))
            {
                throw new InvalidOperationException();
            }

            SetState(State.Creating, true);

            Dictionary <InitialPropertyKeys, object> propertyBag = InitialPropertyBag;

            InitialPropertyBag = null;

            var wpfwindow = System.Windows.Window.GetWindow(this);

            if (wpfwindow == null)
            {
                throw new InvalidOperationException("Window not found!");
            }

            using (var windowInfo = new CefWindowInfo())
            {
                windowInfo.SetAsWindowless(new WindowInteropHelper(wpfwindow).Handle);

                string             initialUrl      = null;
                CefDictionaryValue extraInfo       = null;
                CefRequestContext  requestContext  = null;
                CefBrowserSettings browserSettings = null;
                if (propertyBag != null)
                {
                    object value;
                    if (propertyBag.TryGetValue(InitialPropertyKeys.Url, out value))
                    {
                        initialUrl = value as string;
                    }
                    if (propertyBag.TryGetValue(InitialPropertyKeys.BrowserSettings, out value))
                    {
                        browserSettings = value as CefBrowserSettings;
                    }
                    if (propertyBag.TryGetValue(InitialPropertyKeys.RequestContext, out value))
                    {
                        requestContext = value as CefRequestContext;
                    }
                    if (propertyBag.TryGetValue(InitialPropertyKeys.ExtraInfo, out value))
                    {
                        extraInfo = value as CefDictionaryValue;
                    }
                }

                if (initialUrl == null)
                {
                    initialUrl = "about:blank";
                }
                if (browserSettings == null)
                {
                    browserSettings = DefaultBrowserSettings;
                }

                if (!CefApi.CreateBrowser(windowInfo, ViewGlue.Client, initialUrl, browserSettings, extraInfo, requestContext))
                {
                    throw new InvalidOperationException("Failed to create browser instance.");
                }
            }
        }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CefNetWebClient"/> class.
 /// </summary>
 /// <param name="context">
 /// A request context or null, if <paramref name="context"/> is empty the global
 /// request context will be used. For requests originating from the render process
 /// this parameter must be null.
 /// </param>
 /// <param name="encoding">
 /// The default <see cref="Encoding"/> that is used to encode strings.
 /// If <paramref name="encoding"/> is null then UTF-8 will be used.
 /// </param>
 /// <param name="flags">Flags used to customize the behavior of request.</param>
 public CefNetWebClient(CefRequestContext context, CefUrlRequestFlags flags, Encoding encoding)
 {
     _context             = context;
     this.DefaultEncoding = encoding ?? Encoding.UTF8;
     this.RequestFlags    = flags;
 }
Пример #6
0
 public WebViewTab(CefBrowserSettings settings, CefRequestContext requestContext)
     : this(new CustomWebView {
     RequestContext = requestContext, BrowserSettings = settings
 })
 {
 }