15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_UI_HUNG_PLUGIN_TAB_HELPER_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_UI_HUNG_PLUGIN_TAB_HELPER_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/linked_ptr.h" 11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h" 12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h" 13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/timer/timer.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_observer.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_registrar.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents_observer.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents_user_data.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base { 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FilePath; 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochnamespace infobars { 240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass InfoBarDelegate; 250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} 260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Manages per-tab state with regard to hung plugins. This only handles 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Pepper plugins which we know are windowless. Hung NPAPI plugins (which 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// may have native windows) can not be handled with infobars and have a 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// separate OS-specific hang monitoring. 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Our job is: 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - Pop up an infobar when a plugin is hung. 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - Terminate the plugin process if the user so chooses. 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - Periodically re-show the hung plugin infobar if the user closes it without 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// terminating the plugin. 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - Hide the infobar if the plugin starts responding again. 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - Keep track of all of this for any number of plugins. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HungPluginTabHelper 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : public content::WebContentsObserver, 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public content::NotificationObserver, 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public content::WebContentsUserData<HungPluginTabHelper> { 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~HungPluginTabHelper(); 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // content::WebContentsObserver: 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void PluginCrashed(const base::FilePath& plugin_path, 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::ProcessId plugin_pid) OVERRIDE; 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void PluginHungStatusChanged(int plugin_child_id, 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& plugin_path, 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_hung) OVERRIDE; 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // content::NotificationObserver: 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Observe(int type, 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const content::NotificationSource& source, 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const content::NotificationDetails& details) OVERRIDE; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Called by an infobar when the user selects to kill the plugin. 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void KillPlugin(int child_id); 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) friend class content::WebContentsUserData<HungPluginTabHelper>; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) struct PluginState; 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef std::map<int, linked_ptr<PluginState> > PluginStateMap; 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) explicit HungPluginTabHelper(content::WebContents* contents); 68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Called on a timer for a hung plugin to re-show the bar. 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void OnReshowTimer(int child_id); 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Shows the bar for the plugin identified by the given state, updating the 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // state accordingly. The plugin must not have an infobar already. 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void ShowBar(int child_id, PluginState* state); 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Closes the infobar associated with the given state. Note that this can 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // be called even if the bar is not opened, in which case it will do nothing. 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void CloseBar(PluginState* state); 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::NotificationRegistrar registrar_; 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // All currently hung plugins. 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PluginStateMap hung_plugins_; 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(HungPluginTabHelper); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // CHROME_BROWSER_UI_HUNG_PLUGIN_TAB_HELPER_H_ 89