1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that 3 // can be found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_RENDERER_THREAD_UTIL_H_ 6 #define CEF_LIBCEF_RENDERER_THREAD_UTIL_H_ 7 #pragma once 8 9 #include "libcef/common/task_runner_manager.h" 10 11 #include "base/location.h" 12 #include "base/logging.h" 13 #include "content/public/renderer/render_thread.h" 14 15 #define CEF_CURRENTLY_ON_RT() (!!content::RenderThread::Get()) 16 17 #define CEF_REQUIRE_RT() DCHECK(CEF_CURRENTLY_ON_RT()) 18 19 #define CEF_REQUIRE_RT_RETURN(var) \ 20 if (!CEF_CURRENTLY_ON_RT()) { \ 21 NOTREACHED() << "called on invalid thread"; \ 22 return var; \ 23 } 24 25 #define CEF_REQUIRE_RT_RETURN_VOID() \ 26 if (!CEF_CURRENTLY_ON_RT()) { \ 27 NOTREACHED() << "called on invalid thread"; \ 28 return; \ 29 } 30 31 #define CEF_RENDER_TASK_RUNNER() \ 32 (CefTaskRunnerManager::Get()->GetRenderTaskRunner()) 33 34 #define CEF_POST_TASK_RT(task) \ 35 CEF_RENDER_TASK_RUNNER()->PostTask(FROM_HERE, task) 36 #define CEF_POST_DELAYED_TASK_RT(task, delay_ms) \ 37 CEF_RENDER_TASK_RUNNER()->PostDelayedTask(FROM_HERE, task, \ 38 base::Milliseconds(delay_ms)) 39 40 // Use this template in conjuction with RefCountedThreadSafe when you want to 41 // ensure that an object is deleted on the render thread. 42 struct CefDeleteOnRenderThread { 43 template <typename T> DestructCefDeleteOnRenderThread44 static void Destruct(const T* x) { 45 if (CEF_CURRENTLY_ON_RT()) { 46 delete x; 47 } else { 48 if (!CEF_RENDER_TASK_RUNNER()->DeleteSoon(FROM_HERE, x)) { 49 #if defined(UNIT_TEST) 50 // Only logged under unit testing because leaks at shutdown 51 // are acceptable under normal circumstances. 52 LOG(ERROR) << "DeleteSoon failed on thread " << thread; 53 #endif // UNIT_TEST 54 } 55 } 56 } 57 }; 58 59 #endif // CEF_LIBCEF_RENDERER_THREAD_UTIL_H_ 60