11using System ;
2+ using System . ComponentModel ;
3+ using System . IO ;
24using System . Linq ;
5+ using System . Net ;
6+ using System . Text ;
37
48namespace CefSharp . Example
59{
6- public class ExamplePresenter
10+ public class ExamplePresenter : IRequestHandler
711 {
812 // Use when debugging the actual SubProcess, to make breakpoints etc. inside that project work.
913 private const bool debuggingSubProcess = false ;
@@ -43,7 +47,7 @@ public static void Init()
4347 }
4448
4549 public static string DefaultUrl = "custom://cefsharp/home" ;
46- // private static readonly Uri resource_url = new Uri("http://test/resource/load");
50+ private static readonly Uri resource_url = new Uri ( "http://test/resource/load" ) ;
4751 // private static readonly Uri scheme_url = new Uri("test://test/SchemeTest.html");
4852 // private static readonly Uri bind_url = new Uri("test://test/BindingTest.html");
4953 // private static readonly Uri tooltip_url = new Uri("test://test/TooltipTest.html");
@@ -57,84 +61,88 @@ public static void Init()
5761 // "green",
5862 // };
5963
60- // private readonly IWebBrowser model;
61- // private readonly IExampleView view;
62- // private readonly Action<Action> uiThreadInvoke;
64+ private readonly IWebBrowser model ;
65+ private readonly IExampleView view ;
66+ private readonly Action < Action > uiThreadInvoke ;
6367
64- // public ExamplePresenter(IWebBrowser model, IExampleView view, Action<Action> uiThreadInvoke)
65- // {
66- // this.model = model;
67- // this.view = view;
68- // this.uiThreadInvoke = uiThreadInvoke;
69-
70- // model.RequestHandler = this;
71- // model.PropertyChanged += OnModelPropertyChanged;
72- // model.ConsoleMessage += OnModelConsoleMessage;
73-
74- // // file
75- // view.ShowDevToolsActivated += OnViewShowDevToolsActivated;
76- // view.CloseDevToolsActivated += OnViewCloseDevToolsActivated;
77- // view.ExitActivated += OnViewExitActivated;
78-
79- // // edit
80- // view.UndoActivated += OnViewUndoActivated;
81- // view.RedoActivated += OnViewRedoActivated;
82- // view.CutActivated += OnViewCutActivated;
83- // view.CopyActivated += OnViewCopyActivated;
84- // view.PasteActivated += OnViewPasteActivated;
85- // view.DeleteActivated += OnViewDeleteActivated;
86- // view.SelectAllActivated += OnViewSelectAllActivated;
87-
88- // // test
89- // view.TestResourceLoadActivated += OnViewTestResourceLoadActivated;
90- // view.TestSchemeLoadActivated += OnViewTestSchemeLoadActivated;
91- // view.TestExecuteScriptActivated += OnViewTestExecuteScriptActivated;
92- // view.TestEvaluateScriptActivated += OnViewTestEvaluateScriptActivated;
93- // view.TestBindActivated += OnViewTestBindActivated;
94- // view.TestConsoleMessageActivated += OnViewTestConsoleMessageActivated;
95- // view.TestTooltipActivated += OnViewTestTooltipActivated;
96- // view.TestPopupActivated += OnViewTestPopupActivated;
97- // view.TestLoadStringActivated += OnViewTestLoadStringActivated;
98- // view.TestCookieVisitorActivated += OnViewTestCookieVisitorActivated;
99-
100- // // navigation
101- // view.UrlActivated += OnViewUrlActivated;
102- // view.ForwardActivated += OnViewForwardActivated;
103- // view.BackActivated += OnViewBackActivated;
104- // }
68+ public ExamplePresenter ( IWebBrowser model , IExampleView view , Action < Action > uiThreadInvoke )
69+ {
70+ this . model = model ;
71+ this . view = view ;
72+ this . uiThreadInvoke = uiThreadInvoke ;
73+
74+ var version = String . Format ( "Chromium: {0}, CEF: {1}, CefSharp: {2}" ,
75+ Cef . ChromiumVersion , Cef . CefVersion , Cef . CefSharpVersion ) ;
76+ view . DisplayOutput ( version ) ;
77+
78+ model . RequestHandler = this ;
79+ model . PropertyChanged += OnModelPropertyChanged ;
80+ //model.ConsoleMessage += OnModelConsoleMessage;
81+
82+ //// file
83+ //view.ShowDevToolsActivated += OnViewShowDevToolsActivated;
84+ //view.CloseDevToolsActivated += OnViewCloseDevToolsActivated;
85+ //view.ExitActivated += OnViewExitActivated;
86+
87+ //// edit
88+ //view.UndoActivated += OnViewUndoActivated;
89+ //view.RedoActivated += OnViewRedoActivated;
90+ //view.CutActivated += OnViewCutActivated;
91+ //view.CopyActivated += OnViewCopyActivated;
92+ //view.PasteActivated += OnViewPasteActivated;
93+ //view.DeleteActivated += OnViewDeleteActivated;
94+ //view.SelectAllActivated += OnViewSelectAllActivated;
95+
96+ //// test
97+ //view.TestResourceLoadActivated += OnViewTestResourceLoadActivated;
98+ //view.TestSchemeLoadActivated += OnViewTestSchemeLoadActivated;
99+ //view.TestExecuteScriptActivated += OnViewTestExecuteScriptActivated;
100+ //view.TestEvaluateScriptActivated += OnViewTestEvaluateScriptActivated;
101+ //view.TestBindActivated += OnViewTestBindActivated;
102+ //view.TestConsoleMessageActivated += OnViewTestConsoleMessageActivated;
103+ //view.TestTooltipActivated += OnViewTestTooltipActivated;
104+ //view.TestPopupActivated += OnViewTestPopupActivated;
105+ //view.TestLoadStringActivated += OnViewTestLoadStringActivated;
106+ //view.TestCookieVisitorActivated += OnViewTestCookieVisitorActivated;
107+
108+ //// navigation
109+ view . UrlActivated += OnViewUrlActivated ;
110+ //view.ForwardActivated += OnViewForwardActivated;
111+ //view.BackActivated += OnViewBackActivated;
112+ }
105113
106- // private void OnModelPropertyChanged(object sender, PropertyChangedEventArgs e)
107- // {
108- // switch (e.PropertyName)
109- // {
110- // case "IsBrowserInitialized":
111- // if (model.IsBrowserInitialized)
112- // {
113- // //model.Load(DefaultUrl);
114- // }
115- // break;
116-
117- // case "Title":
118- // uiThreadInvoke(() => view.SetTitle(model.Title));
119- // break;
120-
121- // case "Uri ":
122- // uiThreadInvoke(() => view.SetAddress(model.Address));
123- // break;
124-
125- // case "CanGoBack":
126- // uiThreadInvoke(() => view.SetCanGoBack(model.CanGoBack));
127- // break;
128-
129- // case "CanGoForward":
130- // uiThreadInvoke(() => view.SetCanGoForward(model.CanGoForward));
131- // break;
132-
133- // case "IsLoading":
134- // uiThreadInvoke(() => view.SetIsLoading(model.IsLoading));
135- // break;
136- // }
137- // }
114+ private void OnModelPropertyChanged ( object sender , PropertyChangedEventArgs e )
115+ {
116+ switch ( e . PropertyName )
117+ {
118+ // case "IsBrowserInitialized":
119+ // if (model.IsBrowserInitialized)
120+ // {
121+ // //model.Load(DefaultUrl);
122+ // }
123+ // break;
124+
125+ case "Title" :
126+ uiThreadInvoke ( ( ) => view . SetTitle ( model . Title ) ) ;
127+ break ;
128+
129+ case "Address " :
130+ uiThreadInvoke ( ( ) => view . SetAddress ( model . Address ) ) ;
131+ break ;
132+
133+ case "CanGoBack" :
134+ uiThreadInvoke ( ( ) => view . SetCanGoBack ( model . CanGoBack ) ) ;
135+ break ;
136+
137+ case "CanGoForward" :
138+ uiThreadInvoke ( ( ) => view . SetCanGoForward ( model . CanGoForward ) ) ;
139+ break ;
140+
141+ case "IsLoading" :
142+ uiThreadInvoke ( ( ) => view . SetIsLoading ( model . IsLoading ) ) ;
143+ break ;
144+ }
145+ }
138146
139147 // private void OnModelConsoleMessage(object sender, ConsoleMessageEventArgs e)
140148 // {
@@ -265,10 +273,10 @@ public static void Init()
265273 // Cef.VisitAllCookies(this);
266274 // }
267275
268- // private void OnViewUrlActivated(object sender, string address)
269- // {
270- // // model.Load(address);
271- // }
276+ private void OnViewUrlActivated ( object sender , string address )
277+ {
278+ model . Load ( address ) ;
279+ }
272280
273281 // private void OnViewBackActivated(object sender, EventArgs e)
274282 // {
@@ -280,39 +288,39 @@ public static void Init()
280288 // model.Forward();
281289 // }
282290
283- // bool IRequestHandler.OnBeforeBrowse(IWebBrowser browser, IRequest request, NavigationType naigationvType, bool isRedirect)
284- // {
285- // return false;
286- // }
291+ bool IRequestHandler . OnBeforeBrowse ( IWebBrowser browser , IRequest request , NavigationType naigationvType , bool isRedirect )
292+ {
293+ return false ;
294+ }
287295
288- // bool IRequestHandler.OnBeforeResourceLoad(IWebBrowser browser, IRequestResponse requestResponse)
289- // {
290- // IRequest request = requestResponse.Request;
291- // if (request.Url.StartsWith(resource_url.ToString()))
292- // {
293- // Stream resourceStream = new MemoryStream(Encoding.UTF8.GetBytes(
294- // "<html><body><h1>Success</h1><p>This document is loaded from a System.IO.Stream</p></body></html>"));
295- // requestResponse.RespondWith(resourceStream, "text/html");
296- // }
296+ bool IRequestHandler . OnBeforeResourceLoad ( IWebBrowser browser , IRequestResponse requestResponse )
297+ {
298+ IRequest request = requestResponse . Request ;
299+ if ( request . Url . StartsWith ( resource_url . ToString ( ) ) )
300+ {
301+ Stream resourceStream = new MemoryStream ( Encoding . UTF8 . GetBytes (
302+ "<html><body><h1>Success</h1><p>This document is loaded from a System.IO.Stream</p></body></html>" ) ) ;
303+ requestResponse . RespondWith ( resourceStream , "text/html" ) ;
304+ }
297305
298- // return false;
299- // }
306+ return false ;
307+ }
300308
301- // void IRequestHandler.OnResourceResponse(IWebBrowser browser, string url, int status, string statusText, string mimeType, WebHeaderCollection headers)
302- // {
309+ void IRequestHandler . OnResourceResponse ( IWebBrowser browser , string url , int status , string statusText , string mimeType , WebHeaderCollection headers )
310+ {
303311
304- // }
312+ }
305313
306- // bool IRequestHandler.GetDownloadHandler(IWebBrowser browser, out IDownloadHandler handler)
307- // {
308- // handler = new DownloadHandler();
309- // return true;
310- // }
314+ bool IRequestHandler . GetDownloadHandler ( IWebBrowser browser , out IDownloadHandler handler )
315+ {
316+ handler = new DownloadHandler ( ) ;
317+ return true ;
318+ }
311319
312- // bool IRequestHandler.GetAuthCredentials(IWebBrowser browser, bool isProxy, string host, int port, string realm, string scheme, ref string username, ref string password)
313- // {
314- // return false;
315- // }
320+ bool IRequestHandler . GetAuthCredentials ( IWebBrowser browser , bool isProxy , string host , int port , string realm , string scheme , ref string username , ref string password )
321+ {
322+ return false ;
323+ }
316324
317325 // bool ICookieVisitor.Visit(Cookie cookie, int count, int total, ref bool deleteCookie)
318326 // {
0 commit comments