From de4b9ea1ecc2c3764e8b3574e5bada2f3bbd94af Mon Sep 17 00:00:00 2001 From: "avi@chromium.org" Date: Fri, 7 Sep 2012 13:06:08 +0000 Subject: Switch ScriptBadgeController and PageActionController to use WebContents. BUG=107201 TEST=no visible change Review URL: https://chromiumcodereview.appspot.com/10911105 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@155378 0039d316-1c4b-4281-b951-d872f2087c98 --- .../browser/extensions/page_action_controller.cc | 22 ++++++------ chrome/browser/extensions/page_action_controller.h | 7 ++-- .../browser/extensions/script_badge_controller.cc | 39 +++++++++++++--------- .../browser/extensions/script_badge_controller.h | 6 +--- .../extensions/script_badge_controller_unittest.cc | 2 +- chrome/browser/extensions/tab_helper.cc | 5 +-- .../ui/cocoa/location_bar/location_bar_view_mac.mm | 4 +-- chrome/browser/ui/gtk/location_bar_view_gtk.cc | 5 ++- .../ui/views/location_bar/location_bar_view.cc | 4 +-- chrome/common/chrome_notification_types.h | 2 +- 10 files changed, 49 insertions(+), 47 deletions(-) diff --git a/chrome/browser/extensions/page_action_controller.cc b/chrome/browser/extensions/page_action_controller.cc index 5b95834..0a706c2 100644 --- a/chrome/browser/extensions/page_action_controller.cc +++ b/chrome/browser/extensions/page_action_controller.cc @@ -20,9 +20,8 @@ namespace extensions { -PageActionController::PageActionController(TabContents* tab_contents) - : content::WebContentsObserver(tab_contents->web_contents()), - tab_contents_(tab_contents) {} +PageActionController::PageActionController(content::WebContents* web_contents) + : content::WebContentsObserver(web_contents) {} PageActionController::~PageActionController() {} @@ -52,9 +51,10 @@ LocationBarController::Action PageActionController::OnClicked( CHECK(extension); ExtensionAction* page_action = extension->page_action(); CHECK(page_action); - int tab_id = ExtensionTabUtil::GetTabId(tab_contents_->web_contents()); + int tab_id = ExtensionTabUtil::GetTabId(web_contents()); - tab_contents_->extension_tab_helper()->active_tab_permission_manager()-> + TabContents* tab_contents = TabContents::FromWebContents(web_contents()); + tab_contents->extension_tab_helper()->active_tab_permission_manager()-> GrantIfRequested(extension); switch (mouse_button) { @@ -64,10 +64,10 @@ LocationBarController::Action PageActionController::OnClicked( return ACTION_SHOW_POPUP; GetExtensionService()->browser_event_router()->PageActionExecuted( - tab_contents_->profile(), + tab_contents->profile(), *page_action, tab_id, - tab_contents_->web_contents()->GetURL().spec(), + web_contents()->GetURL().spec(), mouse_button); return ACTION_NONE; @@ -80,7 +80,7 @@ LocationBarController::Action PageActionController::OnClicked( } void PageActionController::NotifyChange() { - tab_contents_->web_contents()->NotifyNavigationStateChanged( + web_contents()->NotifyNavigationStateChanged( content::INVALIDATE_TYPE_PAGE_ACTIONS); } @@ -95,16 +95,18 @@ void PageActionController::DidNavigateMainFrame( if (current_actions.empty()) return; + TabContents* tab_contents = TabContents::FromWebContents(web_contents()); for (size_t i = 0; i < current_actions.size(); ++i) { current_actions[i]->ClearAllValuesForTab( - SessionID::IdForTab(tab_contents_)); + SessionID::IdForTab(tab_contents)); } NotifyChange(); } ExtensionService* PageActionController::GetExtensionService() const { - return ExtensionSystem::Get(tab_contents_->profile())->extension_service(); + TabContents* tab_contents = TabContents::FromWebContents(web_contents()); + return ExtensionSystem::Get(tab_contents->profile())->extension_service(); } } // namespace extensions diff --git a/chrome/browser/extensions/page_action_controller.h b/chrome/browser/extensions/page_action_controller.h index ab4097e3..1f711f6 100644 --- a/chrome/browser/extensions/page_action_controller.h +++ b/chrome/browser/extensions/page_action_controller.h @@ -13,7 +13,6 @@ #include "content/public/browser/web_contents_observer.h" class ExtensionService; -class TabContents; namespace extensions { @@ -22,7 +21,7 @@ namespace extensions { class PageActionController : public LocationBarController, public content::WebContentsObserver { public: - explicit PageActionController(TabContents* tab_contents); + explicit PageActionController(content::WebContents* web_contents); virtual ~PageActionController(); // LocationBarController implementation. @@ -39,11 +38,9 @@ class PageActionController : public LocationBarController, const content::FrameNavigateParams& params) OVERRIDE; private: - // Gets the ExtensionService for |tab_contents_|. + // Gets the ExtensionService for the web contents. ExtensionService* GetExtensionService() const; - TabContents* tab_contents_; - DISALLOW_COPY_AND_ASSIGN(PageActionController); }; diff --git a/chrome/browser/extensions/script_badge_controller.cc b/chrome/browser/extensions/script_badge_controller.cc index 29de9a4..5b8cbba 100644 --- a/chrome/browser/extensions/script_badge_controller.cc +++ b/chrome/browser/extensions/script_badge_controller.cc @@ -29,14 +29,15 @@ namespace extensions { -ScriptBadgeController::ScriptBadgeController(TabContents* tab_contents, +ScriptBadgeController::ScriptBadgeController(content::WebContents* web_contents, ScriptExecutor* script_executor) : ScriptExecutor::Observer(script_executor), - content::WebContentsObserver(tab_contents->web_contents()), - tab_contents_(tab_contents) { + content::WebContentsObserver(web_contents) { + Profile* profile = + Profile::FromBrowserContext(web_contents->GetBrowserContext()); registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, - content::Source(tab_contents->profile())); + content::Source(profile)); } ScriptBadgeController::~ScriptBadgeController() {} @@ -54,7 +55,8 @@ void ScriptBadgeController::GetAttentionFor( // TODO(jyasskin): Modify the icon's appearance to indicate that the // extension is merely asking for permission to run: // http://crbug.com/133142 - script_badge->SetAppearance(SessionID::IdForTab(tab_contents_), + TabContents* tab_contents = TabContents::FromWebContents(web_contents()); + script_badge->SetAppearance(SessionID::IdForTab(tab_contents), ExtensionAction::WANTS_ATTENTION); NotifyChange(); @@ -72,28 +74,30 @@ LocationBarController::Action ScriptBadgeController::OnClicked( CHECK(script_badge); switch (mouse_button) { - case 1: // left - case 2: // middle - tab_contents_->extension_tab_helper()->active_tab_permission_manager()-> + case 1: // left + case 2: { // middle + TabContents* tab_contents = TabContents::FromWebContents(web_contents()); + tab_contents->extension_tab_helper()->active_tab_permission_manager()-> GrantIfRequested(extension); // Even if clicking the badge doesn't immediately cause the extension to // run script on the page, we want to help users associate clicking with // the extension having permission to modify the page, so we make the icon // full-colored immediately. - if (script_badge->SetAppearance(SessionID::IdForTab(tab_contents_), + if (script_badge->SetAppearance(SessionID::IdForTab(tab_contents), ExtensionAction::ACTIVE)) NotifyChange(); // Fire the scriptBadge.onClicked event. GetExtensionService()->browser_event_router()->ScriptBadgeExecuted( - tab_contents_->profile(), + tab_contents->profile(), *script_badge, - SessionID::IdForTab(tab_contents_)); + SessionID::IdForTab(tab_contents)); // TODO(jyasskin): The fallback order should be user-defined popup -> // onClicked handler -> default popup. return ACTION_SHOW_SCRIPT_POPUP; + } case 3: // right // Don't grant access on right clicks, so users can investigate // the extension without danger. @@ -134,21 +138,23 @@ void ScriptBadgeController::OnExecuteScriptFinished( } ExtensionService* ScriptBadgeController::GetExtensionService() { + TabContents* tab_contents = TabContents::FromWebContents(web_contents()); return extensions::ExtensionSystem::Get( - tab_contents_->profile())->extension_service(); + tab_contents->profile())->extension_service(); } int32 ScriptBadgeController::GetPageID() { content::NavigationEntry* nav_entry = - tab_contents_->web_contents()->GetController().GetActiveEntry(); + web_contents()->GetController().GetActiveEntry(); return nav_entry ? nav_entry->GetPageID() : -1; } void ScriptBadgeController::NotifyChange() { + TabContents* tab_contents = TabContents::FromWebContents(web_contents()); content::NotificationService::current()->Notify( chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED, - content::Source(tab_contents_->profile()), - content::Details(tab_contents_)); + content::Source(tab_contents->profile()), + content::Details(web_contents())); } void ScriptBadgeController::DidNavigateMainFrame( @@ -242,7 +248,8 @@ bool ScriptBadgeController::MarkExtensionExecuting( if (!script_badge) return false; - script_badge->SetAppearance(SessionID::IdForTab(tab_contents_), + TabContents* tab_contents = TabContents::FromWebContents(web_contents()); + script_badge->SetAppearance(SessionID::IdForTab(tab_contents), ExtensionAction::ACTIVE); return true; } diff --git a/chrome/browser/extensions/script_badge_controller.h b/chrome/browser/extensions/script_badge_controller.h index d3bb4c8..13281fe 100644 --- a/chrome/browser/extensions/script_badge_controller.h +++ b/chrome/browser/extensions/script_badge_controller.h @@ -21,7 +21,6 @@ class ExtensionAction; class ExtensionService; class GURL; -class TabContents; namespace base { class ListValue; @@ -53,7 +52,7 @@ class ScriptBadgeController public content::WebContentsObserver, public content::NotificationObserver { public: - explicit ScriptBadgeController(TabContents* tab_contents, + explicit ScriptBadgeController(content::WebContents* web_contents, ScriptExecutor* script_executor); virtual ~ScriptBadgeController(); @@ -110,9 +109,6 @@ class ScriptBadgeController // whether any change was made. bool EraseExtension(const Extension* extension); - // Our parent TabContents. - TabContents* tab_contents_; - // The current extension actions in the order they appeared. These come from // calls to ExecuteScript or getAttention on the current frame. std::vector current_actions_; diff --git a/chrome/browser/extensions/script_badge_controller_unittest.cc b/chrome/browser/extensions/script_badge_controller_unittest.cc index ef51f1e..0ebd81c 100644 --- a/chrome/browser/extensions/script_badge_controller_unittest.cc +++ b/chrome/browser/extensions/script_badge_controller_unittest.cc @@ -59,7 +59,7 @@ class ScriptBadgeControllerTest : public TabContentsTestHarness { script_executor_.reset(new ScriptExecutor(web_contents())); script_badge_controller_.reset(new ScriptBadgeController( - tab_contents(), script_executor_.get())); + web_contents(), script_executor_.get())); } protected: diff --git a/chrome/browser/extensions/tab_helper.cc b/chrome/browser/extensions/tab_helper.cc index 23900fc..52b7656a 100644 --- a/chrome/browser/extensions/tab_helper.cc +++ b/chrome/browser/extensions/tab_helper.cc @@ -66,9 +66,10 @@ TabHelper::TabHelper(TabContents* tab_contents) tab_contents->profile()) { if (switch_utils::AreScriptBadgesEnabled()) { location_bar_controller_.reset(new ScriptBadgeController( - tab_contents, &script_executor_)); + tab_contents->web_contents(), &script_executor_)); } else { - location_bar_controller_.reset(new PageActionController(tab_contents)); + location_bar_controller_.reset( + new PageActionController(tab_contents->web_contents())); } registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, diff --git a/chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.mm b/chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.mm index 30f928e..92309f5 100644 --- a/chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.mm +++ b/chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.mm @@ -567,8 +567,8 @@ void LocationBarViewMac::Observe(int type, case chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED: { // Only update if the updated action box was for the active tab contents. - TabContents* target_tab = content::Details(details).ptr(); - if (target_tab == GetTabContents()) + WebContents* target_tab = content::Details(details).ptr(); + if (target_tab == GetTabContents()->web_contents()) UpdatePageActions(); break; } diff --git a/chrome/browser/ui/gtk/location_bar_view_gtk.cc b/chrome/browser/ui/gtk/location_bar_view_gtk.cc index 7996deb..bb99898 100644 --- a/chrome/browser/ui/gtk/location_bar_view_gtk.cc +++ b/chrome/browser/ui/gtk/location_bar_view_gtk.cc @@ -1081,9 +1081,8 @@ void LocationBarViewGtk::Observe(int type, switch (type) { case chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED: { // Only update if the updated action box was for the active tab contents. - TabContents* target_tab = - content::Details(details).ptr(); - if (target_tab == GetTabContents()) + WebContents* target_tab = content::Details(details).ptr(); + if (target_tab == GetTabContents()->web_contents()) UpdatePageActions(); break; } diff --git a/chrome/browser/ui/views/location_bar/location_bar_view.cc b/chrome/browser/ui/views/location_bar/location_bar_view.cc index 9736008..fa7feb5 100644 --- a/chrome/browser/ui/views/location_bar/location_bar_view.cc +++ b/chrome/browser/ui/views/location_bar/location_bar_view.cc @@ -1445,8 +1445,8 @@ void LocationBarView::Observe(int type, case chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED: { // Only update if the updated action box was for the active tab contents. - TabContents* target_tab = content::Details(details).ptr(); - if (target_tab == GetTabContents()) + WebContents* target_tab = content::Details(details).ptr(); + if (target_tab == GetTabContents()->web_contents()) UpdatePageActions(); break; } diff --git a/chrome/common/chrome_notification_types.h b/chrome/common/chrome_notification_types.h index 103edcf..453ecbf 100644 --- a/chrome/common/chrome_notification_types.h +++ b/chrome/common/chrome_notification_types.h @@ -477,7 +477,7 @@ enum NotificationType { NOTIFICATION_EXTENSIONS_READY, // Sent when an extension icon being displayed in the location bar is updated. - // The source is the Profile and the details are the TabContents for + // The source is the Profile and the details are the WebContents for // the tab. NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED, -- cgit v1.1