blob: 67235783bdb3bd8d8d6a1802e8a9d5ba9e7e5978 (
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
|
// 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/extensions/page_action_controller.h"
#include "chrome/browser/extensions/browser_event_router.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/sessions/session_id.h"
#include "chrome/browser/sessions/session_tab_helper.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "chrome/common/extensions/extension_set.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/invalidate_type.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
namespace extensions {
PageActionController::PageActionController(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {}
PageActionController::~PageActionController() {}
std::vector<ExtensionAction*> PageActionController::GetCurrentActions() const {
ExtensionService* service = GetExtensionService();
if (!service)
return std::vector<ExtensionAction*>();
std::vector<ExtensionAction*> current_actions;
for (ExtensionSet::const_iterator i = service->extensions()->begin();
i != service->extensions()->end(); ++i) {
ExtensionAction* action = (*i)->page_action();
if (action)
current_actions.push_back(action);
}
return current_actions;
}
LocationBarController::Action PageActionController::OnClicked(
const std::string& extension_id, int mouse_button) {
ExtensionService* service = GetExtensionService();
if (!service)
return ACTION_NONE;
const Extension* extension = service->extensions()->GetByID(extension_id);
CHECK(extension);
ExtensionAction* page_action = extension->page_action();
CHECK(page_action);
int tab_id = ExtensionTabUtil::GetTabId(web_contents());
extensions::TabHelper::FromWebContents(web_contents())->
active_tab_permission_manager()->GrantIfRequested(extension);
switch (mouse_button) {
case 1: // left
case 2: // middle
if (page_action->HasPopup(tab_id))
return ACTION_SHOW_POPUP;
GetExtensionService()->browser_event_router()->PageActionExecuted(
Profile::FromBrowserContext(web_contents()->GetBrowserContext()),
*page_action,
tab_id,
web_contents()->GetURL().spec(),
mouse_button);
return ACTION_NONE;
case 3: // right
return extension->ShowConfigureContextMenus() ?
ACTION_SHOW_CONTEXT_MENU : ACTION_NONE;
}
return ACTION_NONE;
}
void PageActionController::NotifyChange() {
web_contents()->NotifyNavigationStateChanged(
content::INVALIDATE_TYPE_PAGE_ACTIONS);
}
void PageActionController::DidNavigateMainFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) {
if (details.is_in_page)
return;
const std::vector<ExtensionAction*> current_actions = GetCurrentActions();
if (current_actions.empty())
return;
// TODO(avi): Make IdForTab return -1 for non-tabs.
if (SessionTabHelper::FromWebContents(web_contents()))
for (size_t i = 0; i < current_actions.size(); ++i) {
current_actions[i]->ClearAllValuesForTab(
SessionID::IdForTab(web_contents()));
}
NotifyChange();
}
ExtensionService* PageActionController::GetExtensionService() const {
TabContents* tab_contents = TabContents::FromWebContents(web_contents());
return ExtensionSystem::Get(tab_contents->profile())->extension_service();
}
} // namespace extensions
|