Example #1
0
        public CefWebBrowser(CefBrowserSettings settings, string startUrl)
        {
            this.core = new CefWebBrowserCore(this, settings, startUrl);
            this.core.Created += new EventHandler(core_Created);

            this.SetStyle(
                ControlStyles.ContainerControl
                | ControlStyles.ResizeRedraw
                | ControlStyles.FixedWidth
                | ControlStyles.FixedHeight
                | ControlStyles.StandardClick
                | ControlStyles.UserMouse
                | ControlStyles.SupportsTransparentBackColor
                | ControlStyles.StandardDoubleClick
                | ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.CacheText
                | ControlStyles.EnableNotifyMessage
                | ControlStyles.DoubleBuffer
                | ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.UseTextForAccessibility
                | ControlStyles.Opaque,
                false);

            this.SetStyle(
                ControlStyles.UserPaint
                | ControlStyles.AllPaintingInWmPaint
                | ControlStyles.Selectable,
                true);
        }
        public CefWebBrowser()
        {
            SetStyle(
                ControlStyles.ContainerControl
                | ControlStyles.ResizeRedraw
                | ControlStyles.FixedWidth
                | ControlStyles.FixedHeight
                | ControlStyles.StandardClick
                | ControlStyles.UserMouse
                | ControlStyles.SupportsTransparentBackColor
                | ControlStyles.StandardDoubleClick
                | ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.CacheText
                | ControlStyles.EnableNotifyMessage
                | ControlStyles.DoubleBuffer
                | ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.UseTextForAccessibility
                | ControlStyles.Opaque,
                false);

            SetStyle(
                ControlStyles.UserPaint
                | ControlStyles.AllPaintingInWmPaint
                | ControlStyles.Selectable,
                true);

            var settings = new CefBrowserSettings();
            // settings.ImageLoading = CefState.Disabled;
            // settings.AcceleratedCompositing = CefState.Disabled;

            _core = new WebBrowser(this, settings, "about:blank");
            _core.Created += new EventHandler(BrowserCreated);
        }
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(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 = new CefWebClient(this);

                var settings = new CefBrowserSettings
                {
                    // AuthorAndUserStylesDisabled = false,
                };

                CefBrowserHost.CreateBrowser(windowInfo, client, settings, StartUrl);
            }

            _handleCreated = true;
        }
 public CefBeforePopupEventArgs(CefBrowser parentBrowser, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, string url, ref CefClient client, CefBrowserSettings settings)
 {
     this.parentBrowser = parentBrowser;
     this.popupFeatures = popupFeatures;
     this.windowInfo = windowInfo;
     this.url = url;
     this.client = client;
     this.settings = settings;
 }
        public CefWebBrowserCore(object owner, CefBrowserSettings settings, string startUrl)
        {
            this.owner = owner;
            this.settings = settings;
            this.startUrl = startUrl;
            this.jsBindingContext = new JSBindingContext(this);
            this.jsBindingContext.BindJSObject("cefGlue._browser", this);

            this.readyOptions = CefReadyOptions.None;
        }
Example #6
0
		protected override bool OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref bool noJavascriptAccess)
		{
			var e = new BeforePopupEventArgs(frame, targetUrl, targetFrameName, popupFeatures, windowInfo, client, settings,
								 noJavascriptAccess);

			_core.InvokeIfRequired(() => _core.OnBeforePopup(e));

			client = e.Client;
			noJavascriptAccess = e.NoJavascriptAccess;

			return e.Handled;
		}
Example #7
0
		public BeforePopupEventArgs(
			CefFrame frame,
			string targetUrl,
			string targetFrameName,
			CefPopupFeatures popupFeatures,
			CefWindowInfo windowInfo,
			CefClient client,
			CefBrowserSettings settings,
			bool noJavascriptAccess)
		{
			Frame = frame;
			TargetUrl = targetUrl;
			TargetFrameName = targetFrameName;
			PopupFeatures = popupFeatures;
			WindowInfo = windowInfo;
			Client = client;
			Settings = settings;
			NoJavascriptAccess = noJavascriptAccess;
		}
Example #8
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.");
                }
            }
        }
 protected override bool OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref bool noJavascriptAccess)
 {
     if (Path.HasExtension(targetUrl))
     {
         var ext = Path.GetExtension(targetUrl).Remove(0, 1);
         var extarr = new string[] { "exe", "pdf", "doc", "xls", "rar", "zip" }.ToList();
         if (extarr.Contains(ext))
         {
             browser.GetMainFrame().Browser.GetHost().StartDownload(targetUrl);
             return(true);
         }
     }
     return(base.OnBeforePopup(browser, frame, targetUrl, targetFrameName, popupFeatures, windowInfo, ref client, settings, ref noJavascriptAccess));
 }
Example #10
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.");
                }
            }
        }
Example #11
0
 public CefWebBrowser(CefBrowserSettings settings)
     : this(settings, null)
 {
 }
        protected override bool OnBeforePopup(CefBrowser parentBrowser, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, string url, ref CefClient client, CefBrowserSettings settings)
        {
            #if DIAGNOSTICS
            Cef.Logger.Trace(LogTarget.CefLifeSpanHandler, "OnBeforePopup");
            #endif

            /*
                #if DEBUG
                string message = string.Format(
                    "LifeSpanHandler:OnBeforePopup\n"
                    + "               URL: {0}\n"
                    + "                 X: {1}\n"
                    + "                 Y: {2}\n"
                    + "             Width: {3}\n"
                    + "            Height: {4}\n"
                    + "    MenuBarVisible: {5}\n"
                    + "  StatusBarVisible: {6}\n"
                    + "    ToolBarVisible: {7}\n"
                    + "LocationBarVisible: {8}\n"
                    + " ScrollbarsVisible: {9}\n"
                    + "         Resizable: {10}\n"
                    + "        Fullscreen: {11}\n"
                    + "            Dialog: {12}\n",
                    url,
                    popupFeatures.X.HasValue ? popupFeatures.X.ToString() : "not set",
                    popupFeatures.Y.HasValue ? popupFeatures.Y.ToString() : "not set",
                    popupFeatures.Width.HasValue ? popupFeatures.Width.ToString() : "not set",
                    popupFeatures.Height.HasValue ? popupFeatures.Height.ToString() : "not set",
                    popupFeatures.MenuBarVisible,
                    popupFeatures.StatusBarVisible,
                    popupFeatures.ToolBarVisible,
                    popupFeatures.LocationBarVisible,
                    popupFeatures.ScrollbarsVisible,
                    popupFeatures.Resizable,
                    popupFeatures.Fullscreen,
                    popupFeatures.Dialog
                    );
                #endif
                */

            this.context.OnBeforePopup(parentBrowser, popupFeatures, windowInfo, url, ref client, settings);

            // TODO: create webview here
            // TODO: do not inherit browser settings for devtools
            client = CefWebClientFactory.Default.Create(new CefWebBrowserCore(null, this.context.Settings, url));

            // TODO: create new browsercontext for popup windows

            return false;
        }
Example #13
0
 public WebBrowser(CefBrowserSettings settings)
 {
     _settings = settings;
     Client    = new WebClient(this);
 }
 /// <summary>
 /// Creates the CefGlue browser.
 /// </summary>
 /// <param name="owner"></param>
 /// <param name="container"></param>
 /// <param name="config"></param>
 /// <param name="commandTaskRunner"></param>
 /// <param name="browserMessageRouter"></param>
 /// <param name="settings"></param>
 public CefGlueBrowser(object owner, IChromelyContainer container, IChromelyConfiguration config, IChromelyCommandTaskRunner commandTaskRunner, CefMessageRouterBrowserSide browserMessageRouter, CefBrowserSettings settings)
 {
     Owner                 = owner;
     _container            = container;
     _config               = config;
     _commandTaskRunner    = commandTaskRunner;
     _browserMessageRouter = browserMessageRouter;
     _settings             = settings;
     StartUrl              = config.StartUrl;
 }
Example #15
0
 public WebBrowser(object owner, CefBrowserSettings settings, string startUrl)
 {
     _owner    = owner;
     _settings = settings;
     _startUrl = startUrl;
 }
Example #16
0
        protected override bool OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref bool noJavascriptAccess)
        {
            var e = new BeforePopupEventArgs(frame, targetUrl, targetFrameName, popupFeatures, windowInfo, client, settings,
                                             noJavascriptAccess);

            _core.InvokeIfRequired(() => _core.OnBeforePopup(e));

            client             = e.Client;
            noJavascriptAccess = e.NoJavascriptAccess;

            return(e.Handled);
        }
 public override bool OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefWindowOpenDisposition targetDisposition, bool userGesture, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref CefDictionaryValue extraInfo, ref int noJavascriptAccess)
 {
     return(_implementation.OnBeforePopup(browser, frame, targetUrl, targetFrameName, targetDisposition, userGesture, popupFeatures, windowInfo, ref client, settings, ref extraInfo, ref noJavascriptAccess));
 }
Example #18
0
        protected override bool OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefWindowOpenDisposition targetDisposition, bool userGesture, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref bool noJavascriptAccess)
        {
            var e = new BeforePopupEventArgs(frame, targetUrl, targetFrameName, popupFeatures, windowInfo, client, settings,
                                             noJavascriptAccess);

            m_core.InvokeAsyncIfPossible(() => m_core.OnBeforePopup(e));

            client             = e.Client;
            noJavascriptAccess = e.NoJavascriptAccess;

            return(e.Handled);
        }
Example #19
0
        protected override bool OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefWindowOpenDisposition targetDisposition, bool userGesture, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref CefDictionaryValue extraInfo, ref bool noJavascriptAccess)
        {
            var isUrlExternal = _config?.UrlSchemes?.IsUrlRegisteredExternalScheme(targetUrl);

            if (isUrlExternal.HasValue && isUrlExternal.Value)
            {
                RegisteredExternalUrl.Launch(targetUrl);
            }

            var isUrlCommand = _config?.UrlSchemes?.IsUrlRegisteredCommandScheme(targetUrl);

            if (isUrlCommand.HasValue && isUrlCommand.Value)
            {
                _commandTaskRunner.RunAsync(targetUrl);
            }

            return(true);
        }
 public void OnBeforePopup(CefBrowser parentBrowser, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, string url, ref CefClient client, CefBrowserSettings settings)
 {
     var handler = this.BeforePopup;
     if (handler != null)
     {
         handler(this.owner, new CefBeforePopupEventArgs(parentBrowser, popupFeatures, windowInfo, url, ref client, settings));
     }
 }
Example #21
0
 public WebBrowser(object owner, CefBrowserSettings settings, string startUrl)
 {
     _owner = owner;
     _settings = settings;
     _startUrl = startUrl;
 }
Example #22
0
        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            var size = base.ArrangeOverride(arrangeBounds);

            if (_browserPageImage != null)
            {
                var newWidth  = (int)size.Width;
                var newHeight = (int)size.Height;

                _logger.Debug("BrowserResize. Old H{0}xW{1}; New H{2}xW{3}.", _browserHeight, _browserWidth, newHeight, newWidth);

                if (newWidth > 0 && newHeight > 0)
                {
                    if (!_created)
                    {
                        AttachEventHandlers(this); // TODO: ?

                        // Create the bitmap that holds the rendered website bitmap
                        _browserWidth       = newWidth;
                        _browserHeight      = newHeight;
                        _browserSizeChanged = true;

                        // Find the window that's hosting us
                        Window parentWnd = FindParentOfType <Window>(this);
                        if (parentWnd != null)
                        {
                            IntPtr hParentWnd = new WindowInteropHelper(parentWnd).Handle;

                            var windowInfo = CefWindowInfo.Create();
                            windowInfo.SetAsWindowless(hParentWnd, AllowsTransparency);

                            var settings = new CefBrowserSettings();
                            CefClient = new WpfCefClient(this);

                            // This is the first time the window is being rendered, so create it.
                            CefBrowserHost.CreateBrowser(windowInfo, CefClient, settings, !string.IsNullOrEmpty(StartUrl) ? StartUrl : "about:blank");

                            _created = true;
                        }
                    }
                    else
                    {
                        // Only update the bitmap if the size has changed
                        if (_browserPageBitmap == null || (_browserPageBitmap.Width != newWidth || _browserPageBitmap.Height != newHeight))
                        {
                            _browserWidth       = newWidth;
                            _browserHeight      = newHeight;
                            _browserSizeChanged = true;

                            // If the window has already been created, just resize it
                            if (BrowserHost != null)
                            {
                                _logger.Trace("CefBrowserHost::WasResized to {0}x{1}.", newWidth, newHeight);
                                BrowserHost.WasResized();
                            }
                        }
                    }
                }
            }

            return(size);
        }