This source file includes following definitions.
- data
- DumpBrowserInBlockingPool
- DumpRenderersInBlockingPool
- DumpAndTerminatePluginInBlockingPool
- KillPluginOnIOThread
- Create
- button_text_
- GetIconID
- GetMessageText
- GetButtons
- GetButtonLabel
- Accept
- timer
- PluginCrashed
- PluginHungStatusChanged
- Observe
- KillPlugin
- OnReshowTimer
- ShowBar
- CloseBar
#include "chrome/browser/ui/hung_plugin_tab_helper.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/process/process.h"
#include "base/rand_util.h"
#include "build/build_config.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/infobars/confirm_infobar_delegate.h"
#include "chrome/browser/infobars/infobar.h"
#include "chrome/browser/infobars/infobar_manager.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/common/chrome_version_info.h"
#include "content/public/browser/browser_child_process_host_iterator.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_data.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/plugin_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/process_type.h"
#include "content/public/common/result_codes.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#if defined(OS_WIN)
#include "base/win/scoped_handle.h"
#include "chrome/browser/hang_monitor/hang_crash_dump_win.h"
#endif
namespace {
#if defined(OS_WIN)
class OwnedHandleVector {
public:
typedef std::vector<HANDLE> Handles;
OwnedHandleVector();
~OwnedHandleVector();
Handles* data() { return &data_; }
private:
Handles data_;
DISALLOW_COPY_AND_ASSIGN(OwnedHandleVector);
};
OwnedHandleVector::OwnedHandleVector() {
}
OwnedHandleVector::~OwnedHandleVector() {
for (Handles::iterator iter = data_.begin(); iter != data_.end(); ++iter)
::CloseHandle(*iter);
}
const char kDumpChildProcessesSequenceName[] = "DumpChildProcesses";
void DumpBrowserInBlockingPool() {
CrashDumpForHangDebugging(::GetCurrentProcess());
}
void DumpRenderersInBlockingPool(OwnedHandleVector* renderer_handles) {
for (OwnedHandleVector::Handles::const_iterator iter =
renderer_handles->data()->begin();
iter != renderer_handles->data()->end(); ++iter) {
CrashDumpForHangDebugging(*iter);
}
}
void DumpAndTerminatePluginInBlockingPool(
base::win::ScopedHandle* plugin_handle) {
CrashDumpAndTerminateHungChildProcess(plugin_handle->Get());
}
#endif
void KillPluginOnIOThread(int child_id) {
content::BrowserChildProcessHostIterator iter(
content::PROCESS_TYPE_PPAPI_PLUGIN);
while (!iter.Done()) {
const content::ChildProcessData& data = iter.GetData();
if (data.id == child_id) {
#if defined(OS_WIN)
HANDLE handle = NULL;
HANDLE current_process = ::GetCurrentProcess();
::DuplicateHandle(current_process, data.handle, current_process, &handle,
0, FALSE, DUPLICATE_SAME_ACCESS);
content::BrowserThread::PostBlockingPoolSequencedTask(
kDumpChildProcessesSequenceName, FROM_HERE,
base::Bind(&DumpAndTerminatePluginInBlockingPool,
base::Owned(new base::win::ScopedHandle(handle))));
#else
base::KillProcess(data.handle, content::RESULT_CODE_HUNG, false);
#endif
break;
}
++iter;
}
}
}
class HungPluginInfoBarDelegate : public ConfirmInfoBarDelegate {
public:
static InfoBar* Create(InfoBarService* infobar_service,
HungPluginTabHelper* helper,
int plugin_child_id,
const base::string16& plugin_name);
private:
HungPluginInfoBarDelegate(HungPluginTabHelper* helper,
int plugin_child_id,
const base::string16& plugin_name);
virtual ~HungPluginInfoBarDelegate();
virtual int GetIconID() const OVERRIDE;
virtual base::string16 GetMessageText() const OVERRIDE;
virtual int GetButtons() const OVERRIDE;
virtual base::string16 GetButtonLabel(InfoBarButton button) const OVERRIDE;
virtual bool Accept() OVERRIDE;
HungPluginTabHelper* helper_;
int plugin_child_id_;
base::string16 message_;
base::string16 button_text_;
};
InfoBar* HungPluginInfoBarDelegate::Create(InfoBarService* infobar_service,
HungPluginTabHelper* helper,
int plugin_child_id,
const base::string16& plugin_name) {
return infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
scoped_ptr<ConfirmInfoBarDelegate>(new HungPluginInfoBarDelegate(
helper, plugin_child_id, plugin_name))));
}
HungPluginInfoBarDelegate::HungPluginInfoBarDelegate(
HungPluginTabHelper* helper,
int plugin_child_id,
const base::string16& plugin_name)
: ConfirmInfoBarDelegate(),
helper_(helper),
plugin_child_id_(plugin_child_id),
message_(l10n_util::GetStringFUTF16(
IDS_BROWSER_HANGMONITOR_PLUGIN_INFOBAR, plugin_name)),
button_text_(l10n_util::GetStringUTF16(
IDS_BROWSER_HANGMONITOR_PLUGIN_INFOBAR_KILLBUTTON)) {
}
HungPluginInfoBarDelegate::~HungPluginInfoBarDelegate() {
}
int HungPluginInfoBarDelegate::GetIconID() const {
return IDR_INFOBAR_PLUGIN_CRASHED;
}
base::string16 HungPluginInfoBarDelegate::GetMessageText() const {
return message_;
}
int HungPluginInfoBarDelegate::GetButtons() const {
return BUTTON_OK;
}
base::string16 HungPluginInfoBarDelegate::GetButtonLabel(
InfoBarButton button) const {
return button_text_;
}
bool HungPluginInfoBarDelegate::Accept() {
helper_->KillPlugin(plugin_child_id_);
return true;
}
struct HungPluginTabHelper::PluginState {
PluginState(const base::FilePath& p, const base::string16& n);
~PluginState();
base::FilePath path;
base::string16 name;
InfoBar* infobar;
base::TimeDelta next_reshow_delay;
base::Timer timer;
private:
static const int kInitialReshowDelaySec;
DISALLOW_COPY_AND_ASSIGN(PluginState);
};
const int HungPluginTabHelper::PluginState::kInitialReshowDelaySec = 10;
HungPluginTabHelper::PluginState::PluginState(const base::FilePath& p,
const base::string16& n)
: path(p),
name(n),
infobar(NULL),
next_reshow_delay(base::TimeDelta::FromSeconds(kInitialReshowDelaySec)),
timer(false, false) {
}
HungPluginTabHelper::PluginState::~PluginState() {
}
DEFINE_WEB_CONTENTS_USER_DATA_KEY(HungPluginTabHelper);
HungPluginTabHelper::HungPluginTabHelper(content::WebContents* contents)
: content::WebContentsObserver(contents) {
registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
content::NotificationService::AllSources());
}
HungPluginTabHelper::~HungPluginTabHelper() {
}
void HungPluginTabHelper::PluginCrashed(const base::FilePath& plugin_path,
base::ProcessId plugin_pid) {
InfoBarService* infobar_service =
InfoBarService::FromWebContents(web_contents());
if (!infobar_service)
return;
InfoBarManager* infobar_manager = infobar_service->infobar_manager();
for (PluginStateMap::iterator i = hung_plugins_.begin();
i != hung_plugins_.end(); ++i) {
if (i->second->path == plugin_path) {
if (i->second->infobar)
infobar_manager->RemoveInfoBar(i->second->infobar);
hung_plugins_.erase(i);
break;
}
}
}
void HungPluginTabHelper::PluginHungStatusChanged(
int plugin_child_id,
const base::FilePath& plugin_path,
bool is_hung) {
InfoBarService* infobar_service =
InfoBarService::FromWebContents(web_contents());
if (!infobar_service)
return;
InfoBarManager* infobar_manager = infobar_service->infobar_manager();
PluginStateMap::iterator found = hung_plugins_.find(plugin_child_id);
if (found != hung_plugins_.end()) {
if (!is_hung) {
if (found->second->infobar)
infobar_manager->RemoveInfoBar(found->second->infobar);
hung_plugins_.erase(found);
}
return;
}
base::string16 plugin_name =
content::PluginService::GetInstance()->GetPluginDisplayNameByPath(
plugin_path);
linked_ptr<PluginState> state(new PluginState(plugin_path, plugin_name));
hung_plugins_[plugin_child_id] = state;
ShowBar(plugin_child_id, state.get());
}
void HungPluginTabHelper::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
InfoBar* infobar = content::Details<InfoBar::RemovedDetails>(details)->first;
for (PluginStateMap::iterator i = hung_plugins_.begin();
i != hung_plugins_.end(); ++i) {
PluginState* state = i->second.get();
if (state->infobar == infobar) {
state->infobar = NULL;
state->timer.Start(FROM_HERE, state->next_reshow_delay,
base::Bind(&HungPluginTabHelper::OnReshowTimer,
base::Unretained(this),
i->first));
state->next_reshow_delay *= 2;
return;
}
}
}
void HungPluginTabHelper::KillPlugin(int child_id) {
#if defined(OS_WIN)
if (base::RandInt(0, 100) < 20) {
chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
if (channel == chrome::VersionInfo::CHANNEL_CANARY) {
scoped_ptr<OwnedHandleVector> renderer_handles(new OwnedHandleVector);
HANDLE current_process = ::GetCurrentProcess();
content::RenderProcessHost::iterator renderer_iter =
content::RenderProcessHost::AllHostsIterator();
for (; !renderer_iter.IsAtEnd(); renderer_iter.Advance()) {
content::RenderProcessHost* host = renderer_iter.GetCurrentValue();
HANDLE handle = NULL;
::DuplicateHandle(current_process, host->GetHandle(), current_process,
&handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
renderer_handles->data()->push_back(handle);
}
if (renderer_handles->data()->size() > 0 &&
renderer_handles->data()->size() < 4) {
content::BrowserThread::PostBlockingPoolSequencedTask(
kDumpChildProcessesSequenceName, FROM_HERE,
base::Bind(&DumpBrowserInBlockingPool));
content::BrowserThread::PostBlockingPoolSequencedTask(
kDumpChildProcessesSequenceName, FROM_HERE,
base::Bind(&DumpRenderersInBlockingPool,
base::Owned(renderer_handles.release())));
}
}
}
#endif
PluginStateMap::iterator found = hung_plugins_.find(child_id);
DCHECK(found != hung_plugins_.end());
content::BrowserThread::PostTask(content::BrowserThread::IO,
FROM_HERE,
base::Bind(&KillPluginOnIOThread, child_id));
CloseBar(found->second.get());
}
void HungPluginTabHelper::OnReshowTimer(int child_id) {
PluginStateMap::iterator found = hung_plugins_.find(child_id);
DCHECK(found != hung_plugins_.end());
DCHECK(!found->second->infobar);
ShowBar(child_id, found->second.get());
}
void HungPluginTabHelper::ShowBar(int child_id, PluginState* state) {
InfoBarService* infobar_service =
InfoBarService::FromWebContents(web_contents());
if (!infobar_service)
return;
DCHECK(!state->infobar);
state->infobar = HungPluginInfoBarDelegate::Create(infobar_service, this,
child_id, state->name);
}
void HungPluginTabHelper::CloseBar(PluginState* state) {
InfoBarService* infobar_service =
InfoBarService::FromWebContents(web_contents());
if (infobar_service && state->infobar) {
infobar_service->infobar_manager()->RemoveInfoBar(state->infobar);
state->infobar = NULL;
}
}