1 // Copyright (c) 2012 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.
5 #include "chrome/browser/extensions/page_action_controller.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
9 #include "chrome/browser/extensions/component_loader.h"
10 #include "chrome/browser/extensions/extension_action.h"
11 #include "chrome/browser/extensions/extension_action_manager.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_system.h"
14 #include "chrome/browser/extensions/extension_tab_util.h"
15 #include "chrome/browser/extensions/tab_helper.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/sessions/session_id.h"
18 #include "content/public/browser/invalidate_type.h"
19 #include "content/public/browser/navigation_details.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/web_contents.h"
22 #include "extensions/common/extension_set.h"
24 namespace extensions
{
26 PageActionController::PageActionController(content::WebContents
* web_contents
)
27 : content::WebContentsObserver(web_contents
) {}
29 PageActionController::~PageActionController() {}
31 std::vector
<ExtensionAction
*> PageActionController::GetCurrentActions() const {
32 ExtensionService
* service
= GetExtensionService();
34 return std::vector
<ExtensionAction
*>();
36 // Accumulate the list of all page actions to display.
37 std::vector
<ExtensionAction
*> current_actions
;
39 ExtensionActionManager
* extension_action_manager
=
40 ExtensionActionManager::Get(profile());
42 for (ExtensionSet::const_iterator i
= service
->extensions()->begin();
43 i
!= service
->extensions()->end(); ++i
) {
44 ExtensionAction
* action
=
45 extension_action_manager
->GetPageAction(*i
->get());
47 current_actions
.push_back(action
);
50 return current_actions
;
53 LocationBarController::Action
PageActionController::OnClicked(
54 const std::string
& extension_id
, int mouse_button
) {
55 ExtensionService
* service
= GetExtensionService();
59 const Extension
* extension
= service
->extensions()->GetByID(extension_id
);
61 ExtensionAction
* page_action
=
62 ExtensionActionManager::Get(profile())->GetPageAction(*extension
);
64 int tab_id
= ExtensionTabUtil::GetTabId(web_contents());
66 extensions::TabHelper::FromWebContents(web_contents())->
67 active_tab_permission_granter()->GrantIfRequested(extension
);
69 switch (mouse_button
) {
72 if (page_action
->HasPopup(tab_id
))
73 return ACTION_SHOW_POPUP
;
75 ExtensionActionAPI::PageActionExecuted(
76 profile(), *page_action
, tab_id
,
77 web_contents()->GetURL().spec(), mouse_button
);
81 return extension
->ShowConfigureContextMenus() ?
82 ACTION_SHOW_CONTEXT_MENU
: ACTION_NONE
;
88 void PageActionController::NotifyChange() {
89 web_contents()->NotifyNavigationStateChanged(
90 content::INVALIDATE_TYPE_PAGE_ACTIONS
);
93 void PageActionController::DidNavigateMainFrame(
94 const content::LoadCommittedDetails
& details
,
95 const content::FrameNavigateParams
& params
) {
96 if (details
.is_in_page
)
99 const std::vector
<ExtensionAction
*> current_actions
= GetCurrentActions();
101 if (current_actions
.empty())
104 for (size_t i
= 0; i
< current_actions
.size(); ++i
) {
105 current_actions
[i
]->ClearAllValuesForTab(
106 SessionID::IdForTab(web_contents()));
112 Profile
* PageActionController::profile() const {
113 content::WebContents
* web_contents
= this->web_contents();
115 return Profile::FromBrowserContext(web_contents
->GetBrowserContext());
120 ExtensionService
* PageActionController::GetExtensionService() const {
121 Profile
* profile
= this->profile();
123 return ExtensionSystem::Get(profile
)->extension_service();
128 } // namespace extensions