diff --git a/CefSharp.Core/Internals/CefTaskScheduler.h b/CefSharp.Core/Internals/CefTaskScheduler.h index ee973e431..86a16541b 100644 --- a/CefSharp.Core/Internals/CefTaskScheduler.h +++ b/CefSharp.Core/Internals/CefTaskScheduler.h @@ -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 { diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.cpp b/CefSharp.Core/ManagedCefBrowserAdapter.cpp index ffd5d67f6..bf589b236 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.cpp +++ b/CefSharp.Core/ManagedCefBrowserAdapter.cpp @@ -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^ task = Cef::UIThreadTaskFactory->StartNew(gcnew Func(this, &ManagedCefBrowserAdapter::GetZoomLevel)); - task->Wait(); - return task->Result; - } + return host->GetZoomLevel(); } return 0.0; } Task^ ManagedCefBrowserAdapter::GetZoomLevelAsync() { + if (CefCurrentlyOn(TID_UI)) + { + TaskCompletionSource^ taskSource = gcnew TaskCompletionSource(); + taskSource->SetResult(GetZoomLevel()); + return taskSource->Task; + } return Cef::UIThreadTaskFactory->StartNew(gcnew Func(this, &ManagedCefBrowserAdapter::GetZoomLevel)); } diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.h b/CefSharp.Core/ManagedCefBrowserAdapter.h index 1e2a87179..7d86ca4e2 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.h +++ b/CefSharp.Core/ManagedCefBrowserAdapter.h @@ -580,8 +580,10 @@ namespace CefSharp return _browserProcessServiceHost->EvaluateScriptAsync(browser->GetIdentifier(), frame->GetIdentifier(), script, timeout); } - public: + private: double GetZoomLevel(); + + public: Task^ GetZoomLevelAsync(); void SetZoomLevel(double zoomLevel) diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index 6b5d944a6..8f891baa6 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -368,7 +368,7 @@ namespace CefSharp.OffScreen public double ZoomLevel { - get { return managedCefBrowserAdapter.GetZoomLevel(); } + get { return managedCefBrowserAdapter.GetZoomLevelAsync().Result; } set { managedCefBrowserAdapter.SetZoomLevel(value); } } diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 09a1b821b..7aa9c07d9 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -58,7 +58,7 @@ namespace CefSharp.WinForms public double ZoomLevel { - get { return managedCefBrowserAdapter.GetZoomLevel(); } + get { return managedCefBrowserAdapter.GetZoomLevelAsync().Result; } set { managedCefBrowserAdapter.SetZoomLevel(value); } } diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 367557a43..ebad0a286 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -90,6 +90,7 @@ + diff --git a/CefSharp/CefThreadIds.cs b/CefSharp/CefThreadIds.cs new file mode 100644 index 000000000..f4e9558de --- /dev/null +++ b/CefSharp/CefThreadIds.cs @@ -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 +{ + /// + /// Managed enum for cef_thread_id_t/CefThreadId + /// + 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, + } +}