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 #ifndef CHROME_FRAME_CHROME_FRAME_PLUGIN_H_
6 #define CHROME_FRAME_CHROME_FRAME_PLUGIN_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/win/win_util.h"
13 #include "chrome_frame/chrome_frame_automation.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/chrome_paths_internal.h"
16 #include "chrome_frame/simple_resource_loader.h"
17 #include "chrome_frame/navigation_constraints.h"
18 #include "chrome_frame/utils.h"
19 #include "grit/chromium_strings.h"
21 #define IDC_ABOUT_CHROME_FRAME 40018
23 // Helper so that this file doesn't include the messages header.
24 void ChromeFramePluginGetParamsCoordinates(
25 const MiniContextMenuParams
& params
,
29 // A class to implement common functionality for all types of
30 // plugins: ActiveX and ActiveDoc
32 class ChromeFramePlugin
33 : public ChromeFrameDelegateImpl
,
34 public NavigationConstraintsImpl
{
36 ChromeFramePlugin() : ignore_setfocus_(false){
38 ~ChromeFramePlugin() {
43 MESSAGE_HANDLER(WM_SETFOCUS
, OnSetFocus
)
44 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
45 MESSAGE_HANDLER(WM_PARENTNOTIFY
, OnParentNotify
)
49 DVLOG(1) << __FUNCTION__
;
50 DCHECK(!automation_client_
.get());
51 automation_client_
= CreateAutomationClient();
52 if (!automation_client_
.get()) {
53 NOTREACHED() << "new ChromeFrameAutomationClient";
61 DVLOG(1) << __FUNCTION__
;
63 automation_client_
->Uninitialize();
64 automation_client_
= NULL
;
68 bool InitializeAutomation(const std::wstring
& profile_name
,
69 bool incognito
, bool is_widget_mode
,
70 const GURL
& url
, const GURL
& referrer
,
71 bool route_all_top_level_navigations
) {
73 DCHECK(launch_params_
== NULL
);
74 // We don't want to do incognito when privileged, since we're
75 // running in browser chrome or some other privileged context.
76 bool incognito_mode
= !is_privileged() && incognito
;
77 base::FilePath profile_path
;
78 GetProfilePath(profile_name
, &profile_path
);
79 // The profile name could change based on the browser version. For e.g. for
80 // IE6/7 the profile is created in a different folder whose last component
81 // is Google Chrome Frame.
82 base::FilePath actual_profile_name
= profile_path
.BaseName();
83 launch_params_
= new ChromeFrameLaunchParams(url
, referrer
, profile_path
,
84 actual_profile_name
.value(), SimpleResourceLoader::GetLanguage(),
85 incognito_mode
, is_widget_mode
, route_all_top_level_navigations
,
86 automation_client_
->send_shutdown_delay_switch());
87 return automation_client_
->Initialize(this, launch_params_
);
90 // ChromeFrameDelegate implementation
91 virtual WindowType
GetWindow() const {
92 return (static_cast<const T
*>(this))->m_hWnd
;
95 virtual void GetBounds(RECT
* bounds
) {
97 if (::IsWindow(GetWindow())) {
98 (static_cast<T
*>(this))->GetClientRect(bounds
);
102 virtual std::string
GetDocumentUrl() {
103 return document_url_
;
105 virtual void OnAutomationServerReady() {
108 virtual bool IsValid() const {
109 return automation_client_
.get() != NULL
;
112 virtual void OnHostMoved() {
114 automation_client_
->OnChromeFrameHostMoved();
118 virtual void OnNavigationFailed(int error_code
, const GURL
& gurl
) {
119 OnLoadFailed(error_code
, gurl
.spec());
122 virtual void OnHandleContextMenu(const ContextMenuModel
& menu_model
,
124 const MiniContextMenuParams
& params
) {
125 if (!automation_client_
.get()) {
130 HMENU menu
= BuildContextMenu(menu_model
);
134 T
* self
= static_cast<T
*>(this);
135 if (self
->PreProcessContextMenu(menu
)) {
136 // In order for the context menu to handle keyboard input, give the
137 // ActiveX window focus.
138 ignore_setfocus_
= true;
139 SetFocus(GetWindow());
140 ignore_setfocus_
= false;
141 UINT flags
= align_flags
| TPM_LEFTBUTTON
| TPM_RETURNCMD
| TPM_RECURSE
;
143 ChromeFramePluginGetParamsCoordinates(params
, &x
, &y
);
144 UINT selected
= TrackPopupMenuEx(menu
, flags
, x
, y
, GetWindow(), NULL
);
145 // Menu is over now give focus back to chrome
146 GiveFocusToChrome(false);
147 if (IsValid() && selected
!= 0 &&
148 !self
->HandleContextMenuCommand(selected
, params
)) {
149 automation_client_
->SendContextMenuCommandToChromeFrame(selected
);
156 LRESULT
OnSetFocus(UINT message
, WPARAM wparam
, LPARAM lparam
,
157 BOOL
& handled
) { // NO_LINT
158 if (!ignore_setfocus_
&& IsValid()) {
159 // Pass false to |restore_focus_view|, because we do not want Chrome
160 // to focus the first focusable element in the current view, only the
162 GiveFocusToChrome(false);
167 LRESULT
OnSize(UINT message
, WPARAM wparam
, LPARAM lparam
,
168 BOOL
& handled
) { // NO_LINT
170 // When we get resized, we need to resize the external tab window too.
172 automation_client_
->Resize(LOWORD(lparam
), HIWORD(lparam
),
173 SWP_NOACTIVATE
| SWP_NOZORDER
);
177 LRESULT
OnParentNotify(UINT message
, WPARAM wparam
, LPARAM lparam
,
178 BOOL
& handled
) { // NO_LINT
179 switch (LOWORD(wparam
)) {
183 case WM_XBUTTONDOWN
: {
184 // If we got activated via mouse click on the external tab,
185 // we need to update the state of this thread and tell the
186 // browser that we now have the focus.
187 HWND focus
= ::GetFocus();
188 HWND plugin_window
= GetWindow();
190 // The Chrome-Frame instance may have launched a popup which currently
191 // has focus. Because experimental extension popups are top-level
192 // windows, we have to check that the focus has shifted to a window
193 // that does not share the same GA_ROOTOWNER as the plugin.
194 if (focus
!= plugin_window
&&
195 ::GetAncestor(plugin_window
, GA_ROOTOWNER
) !=
196 ::GetAncestor(focus
, GA_ROOTOWNER
)) {
197 ignore_setfocus_
= true;
198 SetFocus(plugin_window
);
199 ignore_setfocus_
= false;
208 // Return true if context menu should be displayed. The menu could be
209 // modified as well (enable/disable commands, add/remove items).
210 // Override in most-derived class if needed.
211 bool PreProcessContextMenu(HMENU menu
) {
212 // Add an "About" item.
213 AppendMenu(menu
, MF_STRING
, IDC_ABOUT_CHROME_FRAME
,
214 SimpleResourceLoader::Get(IDS_CHROME_FRAME_MENU_ABOUT
).c_str());
218 // Return true if menu command is processed, otherwise the command will be
219 // passed to Chrome for execution. Override in most-derived class if needed.
220 bool HandleContextMenuCommand(UINT cmd
,
221 const MiniContextMenuParams
& params
) {
225 // Allow overriding the type of automation client used, for unit tests.
226 virtual ChromeFrameAutomationClient
* CreateAutomationClient() {
227 return new ChromeFrameAutomationClient
;
230 void GiveFocusToChrome(bool restore_focus_to_view
) {
232 TabProxy
* tab
= automation_client_
->tab();
233 HWND chrome_window
= automation_client_
->tab_window();
234 if (tab
&& ::IsWindow(chrome_window
)) {
235 DVLOG(1) << "Setting initial focus";
236 tab
->SetInitialFocus(base::win::IsShiftPressed(), restore_focus_to_view
);
241 virtual void GetProfilePath(const std::wstring
& profile_name
,
242 base::FilePath
* profile_path
) {
243 return GetChromeFrameProfilePath(profile_name
, profile_path
);
247 // Our gateway to chrome land
248 scoped_refptr
<ChromeFrameAutomationClient
> automation_client_
;
250 // How we launched Chrome.
251 scoped_refptr
<ChromeFrameLaunchParams
> launch_params_
;
253 // Url of the containing document.
254 std::string document_url_
;
256 // We set this flag when we're taking the focus ourselves
257 // and notifying the host browser that we're doing so.
258 // When the flag is not set, we transfer the focus to chrome.
259 bool ignore_setfocus_
;
262 #endif // CHROME_FRAME_CHROME_FRAME_PLUGIN_H_