Adding .Net enum of CefThreadId (mostly to be able to call .ToString()).

Added license headers on new files.
Remove sync version of GetZoomLevel on ManagedCefBrowserAdapter.
Updated callers to call GetZoomLevelAsync() but in a blocking sync manner.

Added CurrentlyOn/EnsureOn static methods to CefTaskScheduler.
This commit is contained in:
rassilon
2015-04-22 08:19:36 -04:00
parent c96f8a82d6
commit 701d391c59
7 changed files with 103 additions and 19 deletions
+20 -2
View File
@@ -57,9 +57,9 @@ namespace CefSharp
public ref class CefTaskScheduler : TaskScheduler, ITaskScheduler
{
public:
cef_thread_id_t _thread;
CefThreadId _thread;
CefTaskScheduler(cef_thread_id_t thread) :
CefTaskScheduler(CefThreadId thread) :
_thread(thread)
{
};
@@ -76,6 +76,24 @@ namespace CefSharp
TryExecuteTask(task);
};
static bool CurrentlyOnThread(CefThreadId threadId)
{
return CefCurrentlyOn(threadId);
}
static void EnsureOn(CefThreadId threadId, String^ context)
{
if (!CefCurrentlyOn(threadId))
{
throw gcnew InvalidOperationException(String::Format("Executed '{0}' on incorrect thread. This method expects to run on the CEF {1} thread!", context, ((CefThreadIds)threadId).ToString()));
}
}
static void EnsureOn(CefThreadIds threadId, String^ context)
{
EnsureOn((CefThreadId)threadId, context);
}
protected:
virtual bool TryExecuteTaskInline(Task^ task, bool taskWasPreviouslyQueued) override
{
+13 -14
View File
@@ -1,32 +1,31 @@
// Copyright © 2010-2015 The CefSharp Project. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "Stdafx.h"
#include "Cef.h"
double ManagedCefBrowserAdapter::GetZoomLevel()
{
CefTaskScheduler::EnsureOn(TID_UI, "ManagedCefBrowserAdapter::GetZoomLevel");
auto browser = _clientAdapter->GetCefBrowser();
if (browser != nullptr)
{
auto host = browser->GetHost();
if (CefCurrentlyOn(TID_UI))
{
return host->GetZoomLevel();
}
else
{
// TODO: Add an async version of GetZoomLevel at some point.
// NOTE: Use of ManualResetEvent is required here in order
// for simple marshaling of some kind of synchronization primitive
// to the callback.
Task<double>^ task = Cef::UIThreadTaskFactory->StartNew(gcnew Func<double>(this, &ManagedCefBrowserAdapter::GetZoomLevel));
task->Wait();
return task->Result;
}
return host->GetZoomLevel();
}
return 0.0;
}
Task<double>^ ManagedCefBrowserAdapter::GetZoomLevelAsync()
{
if (CefCurrentlyOn(TID_UI))
{
TaskCompletionSource<double>^ taskSource = gcnew TaskCompletionSource<double>();
taskSource->SetResult(GetZoomLevel());
return taskSource->Task;
}
return Cef::UIThreadTaskFactory->StartNew(gcnew Func<double>(this, &ManagedCefBrowserAdapter::GetZoomLevel));
}
+3 -1
View File
@@ -580,8 +580,10 @@ namespace CefSharp
return _browserProcessServiceHost->EvaluateScriptAsync(browser->GetIdentifier(), frame->GetIdentifier(), script, timeout);
}
public:
private:
double GetZoomLevel();
public:
Task<double>^ GetZoomLevelAsync();
void SetZoomLevel(double zoomLevel)
+1 -1
View File
@@ -368,7 +368,7 @@ namespace CefSharp.OffScreen
public double ZoomLevel
{
get { return managedCefBrowserAdapter.GetZoomLevel(); }
get { return managedCefBrowserAdapter.GetZoomLevelAsync().Result; }
set { managedCefBrowserAdapter.SetZoomLevel(value); }
}
+1 -1
View File
@@ -58,7 +58,7 @@ namespace CefSharp.WinForms
public double ZoomLevel
{
get { return managedCefBrowserAdapter.GetZoomLevel(); }
get { return managedCefBrowserAdapter.GetZoomLevelAsync().Result; }
set { managedCefBrowserAdapter.SetZoomLevel(value); }
}
+1
View File
@@ -90,6 +90,7 @@
<ItemGroup>
<Compile Include="CefDirtyRect.cs" />
<Compile Include="CefFileDialogMode.cs" />
<Compile Include="CefThreadIds.cs" />
<Compile Include="DefaultResourceHandlerFactory.cs" />
<Compile Include="DependencyChecker.cs" />
<Compile Include="IJavascriptCallback.cs" />
+64
View File
@@ -0,0 +1,64 @@
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CefSharp
{
/// <summary>
/// Managed enum for cef_thread_id_t/CefThreadId
/// </summary>
public enum CefThreadIds
{
// BROWSER PROCESS THREADS -- Only available in the browser process.
///
// The main thread in the browser. This will be the same as the main
// application thread if CefInitialize() is called with a
// CefSettings.multi_threaded_message_loop value of false.
///
TID_UI,
///
// Used to interact with the database.
///
TID_DB,
///
// Used to interact with the file system.
///
TID_FILE,
///
// Used for file system operations that block user interactions.
// Responsiveness of this thread affects users.
///
TID_FILE_USER_BLOCKING,
///
// Used to launch and terminate browser processes.
///
TID_PROCESS_LAUNCHER,
///
// Used to handle slow HTTP cache operations.
///
TID_CACHE,
///
// Used to process IPC and network messages.
///
TID_IO,
// RENDER PROCESS THREADS -- Only available in the render process.
///
// The main thread in the renderer. Used for all WebKit and V8 interaction.
///
TID_RENDERER,
}
}