Upstream version 9.38.207.0
[platform/framework/web/crosswalk.git] / src / android_webview / renderer / aw_execution_termination_filter.cc
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.
4
5 #include "android_webview/renderer/aw_execution_termination_filter.h"
6
7 #include <v8.h>
8
9 #include "android_webview/common/render_view_messages.h"
10 #include "base/message_loop/message_loop_proxy.h"
11
12 namespace {
13 const int kTerminationTimeoutInSeconds = 3;
14 }  // namespace
15
16 namespace android_webview {
17
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) {
24 }
25
26 AwExecutionTerminationFilter::~AwExecutionTerminationFilter() {
27 }
28
29 void AwExecutionTerminationFilter::SetRenderThreadIsolate(
30     v8::Isolate* isolate) {
31   render_thread_isolate_ = isolate;
32 }
33
34 bool AwExecutionTerminationFilter::OnMessageReceived(
35     const IPC::Message& message) {
36   bool handled = true;
37   IPC_BEGIN_MESSAGE_MAP(AwExecutionTerminationFilter, message)
38     IPC_MESSAGE_HANDLER(AwViewMsg_CheckRenderThreadResponsiveness,
39                         OnCheckRenderThreadResponsiveness)
40     IPC_MESSAGE_UNHANDLED(handled = false)
41   IPC_END_MESSAGE_MAP()
42   return handled;
43 }
44
45 void AwExecutionTerminationFilter::OnCheckRenderThreadResponsiveness() {
46   termination_timer_.Start(
47       FROM_HERE,
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(
53       FROM_HERE,
54       base::Bind(&AwExecutionTerminationFilter::StopTimerOnMainThread,
55                  this));
56 }
57
58 void AwExecutionTerminationFilter::StopTimerOnMainThread() {
59   io_message_loop_->PostTask(
60       FROM_HERE,
61       base::Bind(&AwExecutionTerminationFilter::StopTimer, this));
62 }
63
64 void AwExecutionTerminationFilter::StopTimer() {
65   termination_timer_.Stop();
66 }
67
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_);
73   }
74 }
75
76 }  // namespace android_webview