1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "android_webview/renderer/aw_execution_termination_filter.h"
9 #include "android_webview/common/render_view_messages.h"
10 #include "base/message_loop/message_loop_proxy.h"
13 const int kTerminationTimeoutInSeconds = 3;
16 namespace android_webview {
18 AwExecutionTerminationFilter::AwExecutionTerminationFilter(
19 const scoped_refptr<base::MessageLoopProxy>& io_message_loop,
20 const scoped_refptr<base::MessageLoopProxy>& main_message_loop)
21 : io_message_loop_(io_message_loop),
22 main_message_loop_(main_message_loop),
23 render_thread_isolate_(NULL) {
26 AwExecutionTerminationFilter::~AwExecutionTerminationFilter() {
29 void AwExecutionTerminationFilter::SetRenderThreadIsolate(
30 v8::Isolate* isolate) {
31 render_thread_isolate_ = isolate;
34 bool AwExecutionTerminationFilter::OnMessageReceived(
35 const IPC::Message& message) {
37 IPC_BEGIN_MESSAGE_MAP(AwExecutionTerminationFilter, message)
38 IPC_MESSAGE_HANDLER(AwViewMsg_CheckRenderThreadResponsiveness,
39 OnCheckRenderThreadResponsiveness)
40 IPC_MESSAGE_UNHANDLED(handled = false)
45 void AwExecutionTerminationFilter::OnCheckRenderThreadResponsiveness() {
46 termination_timer_.Start(
48 base::TimeDelta::FromSeconds(kTerminationTimeoutInSeconds),
49 base::Bind(&AwExecutionTerminationFilter::TerminateExecution, this));
50 // Post a request to stop the timer via render thread's message loop
51 // to ensure that render thread is responsive.
52 main_message_loop_->PostTask(
54 base::Bind(&AwExecutionTerminationFilter::StopTimerOnMainThread,
58 void AwExecutionTerminationFilter::StopTimerOnMainThread() {
59 io_message_loop_->PostTask(
61 base::Bind(&AwExecutionTerminationFilter::StopTimer, this));
64 void AwExecutionTerminationFilter::StopTimer() {
65 termination_timer_.Stop();
68 void AwExecutionTerminationFilter::TerminateExecution() {
69 if (render_thread_isolate_) {
70 LOG(WARNING) << "Trying to terminate JavaScript execution because "
71 "renderer is unresponsive";
72 v8::V8::TerminateExecution(render_thread_isolate_);
76 } // namespace android_webview