blob: a6066f9c3b71a1a46dfadceceb8406057ab7a234 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]f7817822009-09-24 05:11:582// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_FRAME_CHROME_FRAME_PLUGIN_H_
6#define CHROME_FRAME_CHROME_FRAME_PLUGIN_H_
7
[email protected]bbfa9a12010-08-10 14:09:378#include <string>
9#include <vector>
10
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/ref_counted.h"
[email protected]a8e20582010-12-31 17:18:5012#include "base/win/win_util.h"
[email protected]f7817822009-09-24 05:11:5813#include "chrome_frame/chrome_frame_automation.h"
[email protected]bc73b4e52010-03-26 04:16:2014#include "chrome/common/chrome_paths.h"
15#include "chrome/common/chrome_paths_internal.h"
[email protected]36a26ed2010-03-24 03:09:0516#include "chrome_frame/simple_resource_loader.h"
[email protected]0753db592010-12-15 14:45:0517#include "chrome_frame/navigation_constraints.h"
[email protected]f7817822009-09-24 05:11:5818#include "chrome_frame/utils.h"
[email protected]36a26ed2010-03-24 03:09:0519#include "grit/chromium_strings.h"
20
[email protected]f7817822009-09-24 05:11:5821#define IDC_ABOUT_CHROME_FRAME 40018
22
[email protected]a22f7e02011-02-09 07:15:3523// Helper so that this file doesn't include the messages header.
24void ChromeFramePluginGetParamsCoordinates(
25 const MiniContextMenuParams& params,
26 int* x,
27 int* y);
28
[email protected]f7817822009-09-24 05:11:5829// A class to implement common functionality for all types of
[email protected]99d81e5b2011-06-29 02:29:5330// plugins: ActiveX and ActiveDoc
[email protected]f7817822009-09-24 05:11:5831template <typename T>
[email protected]0753db592010-12-15 14:45:0532class ChromeFramePlugin
33 : public ChromeFrameDelegateImpl,
34 public NavigationConstraintsImpl {
[email protected]f7817822009-09-24 05:11:5835 public:
[email protected]0753db592010-12-15 14:45:0536 ChromeFramePlugin() : ignore_setfocus_(false){
[email protected]f7817822009-09-24 05:11:5837 }
38 ~ChromeFramePlugin() {
39 Uninitialize();
40 }
41
[email protected]045229a72010-03-03 22:11:1942BEGIN_MSG_MAP(T)
[email protected]f7817822009-09-24 05:11:5843 MESSAGE_HANDLER(WM_SETFOCUS, OnSetFocus)
44 MESSAGE_HANDLER(WM_SIZE, OnSize)
45 MESSAGE_HANDLER(WM_PARENTNOTIFY, OnParentNotify)
46END_MSG_MAP()
47
48 bool Initialize() {
[email protected]2b9a9f162010-10-19 20:30:4549 DVLOG(1) << __FUNCTION__;
[email protected]f7817822009-09-24 05:11:5850 DCHECK(!automation_client_.get());
[email protected]b0febbf2009-11-12 17:49:3551 automation_client_ = CreateAutomationClient();
[email protected]f7817822009-09-24 05:11:5852 if (!automation_client_.get()) {
53 NOTREACHED() << "new ChromeFrameAutomationClient";
54 return false;
55 }
56
57 return true;
58 }
59
60 void Uninitialize() {
[email protected]2b9a9f162010-10-19 20:30:4561 DVLOG(1) << __FUNCTION__;
[email protected]045229a72010-03-03 22:11:1962 if (IsValid()) {
[email protected]f7817822009-09-24 05:11:5863 automation_client_->Uninitialize();
[email protected]b0febbf2009-11-12 17:49:3564 automation_client_ = NULL;
[email protected]f7817822009-09-24 05:11:5865 }
66 }
67
68 bool InitializeAutomation(const std::wstring& profile_name,
[email protected]e150e822010-06-03 23:10:5569 bool incognito, bool is_widget_mode,
[email protected]9eeb35e2010-09-30 21:38:5070 const GURL& url, const GURL& referrer,
71 bool route_all_top_level_navigations) {
[email protected]045229a72010-03-03 22:11:1972 DCHECK(IsValid());
[email protected]bbfa9a12010-08-10 14:09:3773 DCHECK(launch_params_ == NULL);
[email protected]f7817822009-09-24 05:11:5874 // We don't want to do incognito when privileged, since we're
75 // running in browser chrome or some other privileged context.
[email protected]0753db592010-12-15 14:45:0576 bool incognito_mode = !is_privileged() && incognito;
[email protected]bc73b4e52010-03-26 04:16:2077 FilePath profile_path;
78 GetProfilePath(profile_name, &profile_path);
[email protected]1d27bbe2010-09-10 00:32:0679 // 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 FilePath actual_profile_name = profile_path.BaseName();
[email protected]bbfa9a12010-08-10 14:09:3783 launch_params_ = new ChromeFrameLaunchParams(url, referrer, profile_path,
[email protected]6ae3d492010-10-20 14:01:2184 actual_profile_name.value(), SimpleResourceLoader::GetLanguage(),
[email protected]c70e5582011-12-20 21:22:4585 incognito_mode, is_widget_mode, route_all_top_level_navigations);
[email protected]bbfa9a12010-08-10 14:09:3786 return automation_client_->Initialize(this, launch_params_);
[email protected]f7817822009-09-24 05:11:5887 }
88
89 // ChromeFrameDelegate implementation
90 virtual WindowType GetWindow() const {
91 return (static_cast<const T*>(this))->m_hWnd;
92 }
93
94 virtual void GetBounds(RECT* bounds) {
95 if (bounds) {
96 if (::IsWindow(GetWindow())) {
97 (static_cast<T*>(this))->GetClientRect(bounds);
98 }
99 }
100 }
101 virtual std::string GetDocumentUrl() {
102 return document_url_;
103 }
104 virtual void OnAutomationServerReady() {
[email protected]f7817822009-09-24 05:11:58105 }
106
107 virtual bool IsValid() const {
108 return automation_client_.get() != NULL;
109 }
110
[email protected]2b19e2fe2010-02-16 02:24:18111 virtual void OnHostMoved() {
[email protected]045229a72010-03-03 22:11:19112 if (IsValid())
113 automation_client_->OnChromeFrameHostMoved();
[email protected]2b19e2fe2010-02-16 02:24:18114 }
115
[email protected]f7817822009-09-24 05:11:58116 protected:
[email protected]f5494d42010-12-23 22:15:34117 virtual void OnNavigationFailed(int error_code, const GURL& gurl) {
[email protected]f7817822009-09-24 05:11:58118 OnLoadFailed(error_code, gurl.spec());
119 }
120
[email protected]b516e2d2011-07-12 16:54:12121 virtual void OnHandleContextMenu(const ContextMenuModel& menu_model,
[email protected]35f13ab2009-12-16 23:59:17122 int align_flags,
[email protected]f5494d42010-12-23 22:15:34123 const MiniContextMenuParams& params) {
[email protected]b516e2d2011-07-12 16:54:12124 if (!automation_client_.get()) {
[email protected]f7817822009-09-24 05:11:58125 NOTREACHED();
126 return;
127 }
128
[email protected]b516e2d2011-07-12 16:54:12129 HMENU menu = BuildContextMenu(menu_model);
130 if (!menu)
[email protected]f7817822009-09-24 05:11:58131 return;
132
[email protected]4e676aa2010-02-12 18:19:07133 T* self = static_cast<T*>(this);
[email protected]b516e2d2011-07-12 16:54:12134 if (self->PreProcessContextMenu(menu)) {
[email protected]4e676aa2010-02-12 18:19:07135 // In order for the context menu to handle keyboard input, give the
136 // ActiveX window focus.
137 ignore_setfocus_ = true;
138 SetFocus(GetWindow());
139 ignore_setfocus_ = false;
[email protected]f7817822009-09-24 05:11:58140 UINT flags = align_flags | TPM_LEFTBUTTON | TPM_RETURNCMD | TPM_RECURSE;
[email protected]a22f7e02011-02-09 07:15:35141 int x, y;
142 ChromeFramePluginGetParamsCoordinates(params, &x, &y);
[email protected]b516e2d2011-07-12 16:54:12143 UINT selected = TrackPopupMenuEx(menu, flags, x, y, GetWindow(), NULL);
[email protected]4e676aa2010-02-12 18:19:07144 // Menu is over now give focus back to chrome
[email protected]1fd45692010-04-19 21:01:18145 GiveFocusToChrome(false);
[email protected]045229a72010-03-03 22:11:19146 if (IsValid() && selected != 0 &&
147 !self->HandleContextMenuCommand(selected, params)) {
[email protected]f7817822009-09-24 05:11:58148 automation_client_->SendContextMenuCommandToChromeFrame(selected);
149 }
150 }
151
[email protected]b516e2d2011-07-12 16:54:12152 DestroyMenu(menu);
[email protected]f7817822009-09-24 05:11:58153 }
154
155 LRESULT OnSetFocus(UINT message, WPARAM wparam, LPARAM lparam,
156 BOOL& handled) { // NO_LINT
[email protected]045229a72010-03-03 22:11:19157 if (!ignore_setfocus_ && IsValid()) {
[email protected]26edf1d2010-10-13 17:53:04158 // Pass false to |restore_focus_view|, because we do not want Chrome
159 // to focus the first focusable element in the current view, only the
160 // view itself.
161 GiveFocusToChrome(false);
[email protected]f7817822009-09-24 05:11:58162 }
[email protected]f7817822009-09-24 05:11:58163 return 0;
164 }
165
166 LRESULT OnSize(UINT message, WPARAM wparam, LPARAM lparam,
167 BOOL& handled) { // NO_LINT
168 handled = FALSE;
169 // When we get resized, we need to resize the external tab window too.
[email protected]045229a72010-03-03 22:11:19170 if (IsValid())
[email protected]f7817822009-09-24 05:11:58171 automation_client_->Resize(LOWORD(lparam), HIWORD(lparam),
172 SWP_NOACTIVATE | SWP_NOZORDER);
173 return 0;
174 }
175
176 LRESULT OnParentNotify(UINT message, WPARAM wparam, LPARAM lparam,
177 BOOL& handled) { // NO_LINT
178 switch (LOWORD(wparam)) {
179 case WM_LBUTTONDOWN:
180 case WM_MBUTTONDOWN:
181 case WM_RBUTTONDOWN:
182 case WM_XBUTTONDOWN: {
183 // If we got activated via mouse click on the external tab,
184 // we need to update the state of this thread and tell the
185 // browser that we now have the focus.
186 HWND focus = ::GetFocus();
187 HWND plugin_window = GetWindow();
[email protected]01e79d82010-09-07 20:34:42188
189 // The Chrome-Frame instance may have launched a popup which currently
190 // has focus. Because experimental extension popups are top-level
191 // windows, we have to check that the focus has shifted to a window
192 // that does not share the same GA_ROOTOWNER as the plugin.
193 if (focus != plugin_window &&
194 ::GetAncestor(plugin_window, GA_ROOTOWNER) !=
195 ::GetAncestor(focus, GA_ROOTOWNER)) {
[email protected]f7817822009-09-24 05:11:58196 ignore_setfocus_ = true;
197 SetFocus(plugin_window);
198 ignore_setfocus_ = false;
199 }
200 break;
201 }
202 }
203
204 return 0;
205 }
206
207 // Return true if context menu should be displayed. The menu could be
208 // modified as well (enable/disable commands, add/remove items).
209 // Override in most-derived class if needed.
210 bool PreProcessContextMenu(HMENU menu) {
[email protected]50f53162009-10-23 19:16:20211 // Add an "About" item.
[email protected]f7817822009-09-24 05:11:58212 AppendMenu(menu, MF_STRING, IDC_ABOUT_CHROME_FRAME,
[email protected]36a26ed2010-03-24 03:09:05213 SimpleResourceLoader::Get(IDS_CHROME_FRAME_MENU_ABOUT).c_str());
[email protected]f7817822009-09-24 05:11:58214 return true;
215 }
216
217 // Return true if menu command is processed, otherwise the command will be
218 // passed to Chrome for execution. Override in most-derived class if needed.
[email protected]e150e822010-06-03 23:10:55219 bool HandleContextMenuCommand(UINT cmd,
[email protected]f5494d42010-12-23 22:15:34220 const MiniContextMenuParams& params) {
[email protected]f7817822009-09-24 05:11:58221 return false;
222 }
223
224 // Allow overriding the type of automation client used, for unit tests.
225 virtual ChromeFrameAutomationClient* CreateAutomationClient() {
226 return new ChromeFrameAutomationClient;
227 }
228
[email protected]1fd45692010-04-19 21:01:18229 void GiveFocusToChrome(bool restore_focus_to_view) {
[email protected]045229a72010-03-03 22:11:19230 if (IsValid()) {
231 TabProxy* tab = automation_client_->tab();
232 HWND chrome_window = automation_client_->tab_window();
233 if (tab && ::IsWindow(chrome_window)) {
[email protected]2b9a9f162010-10-19 20:30:45234 DVLOG(1) << "Setting initial focus";
[email protected]a8e20582010-12-31 17:18:50235 tab->SetInitialFocus(base::win::IsShiftPressed(), restore_focus_to_view);
[email protected]045229a72010-03-03 22:11:19236 }
[email protected]b95f550f2009-11-19 05:35:22237 }
238 }
239
[email protected]bc73b4e52010-03-26 04:16:20240 virtual void GetProfilePath(const std::wstring& profile_name,
241 FilePath* profile_path) {
242 chrome::GetChromeFrameUserDataDirectory(profile_path);
243 *profile_path = profile_path->Append(profile_name);
[email protected]2b9a9f162010-10-19 20:30:45244 DVLOG(1) << __FUNCTION__ << ": " << profile_path->value();
[email protected]bc73b4e52010-03-26 04:16:20245 }
246
[email protected]f7817822009-09-24 05:11:58247 protected:
248 // Our gateway to chrome land
[email protected]b0febbf2009-11-12 17:49:35249 scoped_refptr<ChromeFrameAutomationClient> automation_client_;
[email protected]f7817822009-09-24 05:11:58250
[email protected]bbfa9a12010-08-10 14:09:37251 // How we launched Chrome.
252 scoped_refptr<ChromeFrameLaunchParams> launch_params_;
253
[email protected]f7817822009-09-24 05:11:58254 // Url of the containing document.
255 std::string document_url_;
256
257 // We set this flag when we're taking the focus ourselves
258 // and notifying the host browser that we're doing so.
259 // When the flag is not set, we transfer the focus to chrome.
260 bool ignore_setfocus_;
[email protected]f7817822009-09-24 05:11:58261};
262
263#endif // CHROME_FRAME_CHROME_FRAME_PLUGIN_H_