summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/hung_renderer_view_win.cc
blob: e4b6f62e812610e106b701298ea7a031a40c3aaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/hung_renderer_view_win.h"

#include "base/win/metro.h"
#include "chrome/browser/hang_monitor/hang_crash_dump_win.h"
#include "chrome/browser/ui/views/hung_renderer_view.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "win8/util/win8_util.h"

// Metro functions for displaying and dismissing a dialog box.
typedef void (*MetroShowDialogBox)(
    const wchar_t* title,
    const wchar_t* content,
    const wchar_t* button1_label,
    const wchar_t* button2_label,
    base::win::MetroDialogButtonPressedHandler button1_handler,
    base::win::MetroDialogButtonPressedHandler button2_handler);

typedef void (*MetroDismissDialogBox)();

using content::BrowserThread;
using content::WebContents;

HungRendererDialogMetro* HungRendererDialogMetro::g_instance_ = NULL;

bool PlatformShowCustomHungRendererDialog(WebContents* contents) {
  if (!win8::IsSingleWindowMetroMode())
    return false;

  HungRendererDialogMetro::Create()->Show(contents);
  return true;
}

bool PlatformHideCustomHungRendererDialog(WebContents* contents) {
  if (!win8::IsSingleWindowMetroMode())
    return false;

  if (HungRendererDialogMetro::GetInstance())
    HungRendererDialogMetro::GetInstance()->Hide(contents);
  return true;
}

// static
void HungRendererDialogView::KillRendererProcess(
    base::ProcessHandle process_handle) {
  // Try to generate a crash report for the hung process.
  CrashDumpAndTerminateHungChildProcess(process_handle);
}

// static
HungRendererDialogMetro* HungRendererDialogMetro::Create() {
  if (!GetInstance())
    g_instance_ = new HungRendererDialogMetro;
  return g_instance_;
}

// static
HungRendererDialogMetro* HungRendererDialogMetro::GetInstance() {
  return g_instance_;
}

HungRendererDialogMetro::HungRendererDialogMetro()
    : contents_(NULL),
      metro_dialog_displayed_(false) {
}

HungRendererDialogMetro::~HungRendererDialogMetro() {
  g_instance_ = NULL;
}

void HungRendererDialogMetro::Show(WebContents* contents) {
  if (!metro_dialog_displayed_ &&
      HungRendererDialogView::IsFrameActive(contents)) {
    HMODULE metro_dll = base::win::GetMetroModule();
    DCHECK(metro_dll);
    if (metro_dll) {
      MetroShowDialogBox show_dialog_box = reinterpret_cast<MetroShowDialogBox>
          (::GetProcAddress(metro_dll, "ShowDialogBox"));
      DCHECK(show_dialog_box);
      if (show_dialog_box) {
        string16 dialog_box_title =
            l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_TITLE);

        string16 kill_button_label =
            l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_END);

        string16 wait_button_label =
            l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_WAIT);

        contents_ = contents;
        metro_dialog_displayed_ = true;

        (*show_dialog_box)(dialog_box_title.c_str(),
                           contents->GetTitle().c_str(),
                           kill_button_label.c_str(),
                           wait_button_label.c_str(),
                           HungRendererDialogMetro::OnMetroKillProcess,
                           HungRendererDialogMetro::OnMetroWait);
      }
    }
  }
}

void HungRendererDialogMetro::Hide(WebContents* contents) {
  HMODULE metro_dll = base::win::GetMetroModule();
  DCHECK(metro_dll);
  if (metro_dll) {
    MetroDismissDialogBox dismiss_dialog_box =
        reinterpret_cast<MetroDismissDialogBox>
            (::GetProcAddress(metro_dll, "DismissDialogBox"));
    DCHECK(dismiss_dialog_box);
    if (dismiss_dialog_box) {
      (*dismiss_dialog_box)();
      ResetMetroState();
    }
  }
}

void HungRendererDialogMetro::ResetMetroState() {
  metro_dialog_displayed_ = false;
  contents_ = NULL;
  delete g_instance_;
}

// static
void HungRendererDialogMetro::OnMetroKillProcess() {
  // Metro chrome will invoke these handlers on the metro thread. Ensure that
  // we switch to the UI thread.
  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
    BrowserThread::PostTask(
        BrowserThread::UI, FROM_HERE,
        base::Bind(HungRendererDialogMetro::OnMetroKillProcess));
    return;
  }

  // Its possible that we got deleted in the meantime.
  if (!GetInstance())
    return;

  DCHECK(GetInstance()->contents_);

  HungRendererDialogView::KillRendererProcess(
      GetInstance()->contents_->GetRenderProcessHost()->GetHandle());

  // The metro dialog box is dismissed when the button handlers are invoked.
  GetInstance()->ResetMetroState();
}

// static
void HungRendererDialogMetro::OnMetroWait() {
  // Metro chrome will invoke these handlers on the metro thread. Ensure that
  // we switch to the UI thread.
  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
    BrowserThread::PostTask(
        BrowserThread::UI, FROM_HERE,
        base::Bind(HungRendererDialogMetro::OnMetroWait));
    return;
  }

  // Its possible that we got deleted in the meantime.
  if (!GetInstance())
    return;

  GetInstance()->ResetMetroState();
}