blob: 37c089c7a5583e9b8c3d08e78b90d4b04148f1d0 [file] [log] [blame]
[email protected]f17a0ee2010-05-17 17:38:471// Copyright (c) 2010 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// Implementation of ChromeActiveDocument
6#include "chrome_frame/chrome_active_document.h"
7
8#include <hlink.h>
9#include <htiface.h>
[email protected]a1800e82009-11-19 00:53:2310#include <initguid.h>
[email protected]f7817822009-09-24 05:11:5811#include <mshtmcid.h>
12#include <shdeprecated.h>
13#include <shlguid.h>
[email protected]48dc9e12010-08-26 19:49:5714#include <shlobj.h>
[email protected]f7817822009-09-24 05:11:5815#include <shobjidl.h>
16#include <tlogstg.h>
17#include <urlmon.h>
18#include <wininet.h>
19
20#include "base/command_line.h"
21#include "base/file_util.h"
[email protected]f7817822009-09-24 05:11:5822#include "base/logging.h"
23#include "base/path_service.h"
24#include "base/process_util.h"
[email protected]f7817822009-09-24 05:11:5825#include "base/scoped_variant_win.h"
26#include "base/string_tokenizer.h"
27#include "base/string_util.h"
28#include "base/thread.h"
29#include "base/thread_local.h"
[email protected]04e3f352010-05-10 13:48:2430#include "base/trace_event.h"
[email protected]252cad62010-08-18 18:33:5731#include "base/utf_string_conversions.h"
[email protected]f7817822009-09-24 05:11:5832#include "grit/generated_resources.h"
[email protected]074283a2010-05-28 23:47:5933#include "chrome/app/chrome_dll_resource.h"
[email protected]f7817822009-09-24 05:11:5834#include "chrome/browser/tab_contents/tab_contents.h"
35#include "chrome/common/chrome_constants.h"
36#include "chrome/common/navigation_types.h"
[email protected]7f860dd82010-08-09 23:18:0537#include "chrome/common/page_zoom.h"
[email protected]f7817822009-09-24 05:11:5838#include "chrome/test/automation/browser_proxy.h"
39#include "chrome/test/automation/tab_proxy.h"
[email protected]b36a9f92009-10-19 17:34:5740#include "chrome_frame/bho.h"
[email protected]29c32f902010-04-20 23:27:2941#include "chrome_frame/bind_context_info.h"
[email protected]5ae94d22010-07-21 19:55:3642#include "chrome_frame/buggy_bho_handling.h"
[email protected]9607f6a82010-06-12 17:24:4143#include "chrome_frame/crash_reporting/crash_metrics.h"
[email protected]f7817822009-09-24 05:11:5844#include "chrome_frame/utils.h"
45
[email protected]a1800e82009-11-19 00:53:2346DEFINE_GUID(CGID_DocHostCmdPriv, 0x000214D4L, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0,
47 0x46);
48
[email protected]8f56e612010-09-09 17:03:1449static const wchar_t kHandleTopLevelRequests[] = L"HandleTopLevelRequests";
50static const wchar_t kUseChromeNetworking[] = L"UseChromeNetworking";
51
[email protected]f7817822009-09-24 05:11:5852base::ThreadLocalPointer<ChromeActiveDocument> g_active_doc_cache;
53
54bool g_first_launch_by_process_ = true;
55
[email protected]2f2afba2010-04-01 01:53:1956const DWORD kIEEncodingIdArray[] = {
57#define DEFINE_ENCODING_ID_ARRAY(encoding_name, id, chrome_name) encoding_name,
58 INTERNAL_IE_ENCODINGMENU_IDS(DEFINE_ENCODING_ID_ARRAY)
59#undef DEFINE_ENCODING_ID_ARRAY
60 0 // The Last data must be 0 to indicate the end of the encoding id array.
61};
62
[email protected]f7817822009-09-24 05:11:5863ChromeActiveDocument::ChromeActiveDocument()
64 : first_navigation_(true),
[email protected]a15d4a42010-02-17 08:21:3565 is_automation_client_reused_(false),
[email protected]355309e2010-03-15 17:01:3466 popup_allowed_(false),
[email protected]c4e45b32010-07-28 21:15:1567 accelerator_table_(NULL) {
[email protected]04e3f352010-05-10 13:48:2468 TRACE_EVENT_BEGIN("chromeframe.createactivedocument", this, "");
69
[email protected]bbfa9a12010-08-10 14:09:3770 url_fetcher_->set_frame_busting(false);
[email protected]a1800e82009-11-19 00:53:2371 memset(&navigation_info_, 0, sizeof(navigation_info_));
[email protected]f7817822009-09-24 05:11:5872}
73
74HRESULT ChromeActiveDocument::FinalConstruct() {
75 // If we have a cached ChromeActiveDocument instance in TLS, then grab
76 // ownership of the cached document's automation client. This is an
77 // optimization to get Chrome active documents to load faster.
78 ChromeActiveDocument* cached_document = g_active_doc_cache.Get();
[email protected]58174a72010-05-05 21:30:3079 if (cached_document && cached_document->IsValid()) {
[email protected]f7817822009-09-24 05:11:5880 DCHECK(automation_client_.get() == NULL);
[email protected]3148c0ae2010-03-11 18:06:0081 automation_client_.swap(cached_document->automation_client_);
[email protected]f7817822009-09-24 05:11:5882 DLOG(INFO) << "Reusing automation client instance from "
83 << cached_document;
84 DCHECK(automation_client_.get() != NULL);
[email protected]bbfa9a12010-08-10 14:09:3785 automation_client_->Reinitialize(this, url_fetcher_.get());
[email protected]f7817822009-09-24 05:11:5886 is_automation_client_reused_ = true;
[email protected]b5811052010-08-21 01:37:2987 OnAutomationServerReady();
[email protected]f7817822009-09-24 05:11:5888 } else {
89 // The FinalConstruct implementation in the ChromeFrameActivexBase class
90 // i.e. Base creates an instance of the ChromeFrameAutomationClient class
91 // and initializes it, which would spawn a new Chrome process, etc.
92 // We don't want to be doing this if we have a cached document, whose
93 // automation client instance can be reused.
[email protected]97965e12010-04-09 00:51:1094 HRESULT hr = BaseActiveX::FinalConstruct();
[email protected]f7817822009-09-24 05:11:5895 if (FAILED(hr))
96 return hr;
97 }
98
[email protected]8f56e612010-09-09 17:03:1499 // Query and assign the top-level-request routing, and host networking
100 // settings from the registry.
101 bool top_level_requests = GetConfigBool(true, kHandleTopLevelRequests);
102 bool chrome_network = GetConfigBool(false, kUseChromeNetworking);
103 automation_client_->set_handle_top_level_requests(top_level_requests);
104 automation_client_->set_use_chrome_network(chrome_network);
105
[email protected]f7817822009-09-24 05:11:58106 find_dialog_.Init(automation_client_.get());
107
[email protected]6fd1a7a2010-07-26 22:09:46108 OLECMDF flags = static_cast<OLECMDF>(OLECMDF_ENABLED | OLECMDF_SUPPORTED);
109
110 null_group_commands_map_[OLECMDID_PRINT] = flags;
111 null_group_commands_map_[OLECMDID_FIND] = flags;
112 null_group_commands_map_[OLECMDID_CUT] = flags;
113 null_group_commands_map_[OLECMDID_COPY] = flags;
114 null_group_commands_map_[OLECMDID_PASTE] = flags;
115 null_group_commands_map_[OLECMDID_SELECTALL] = flags;
116 null_group_commands_map_[OLECMDID_SAVEAS] = flags;
117
118 mshtml_group_commands_map_[IDM_BASELINEFONT1] = flags;
119 mshtml_group_commands_map_[IDM_BASELINEFONT2] = flags;
120 mshtml_group_commands_map_[IDM_BASELINEFONT3] = flags;
121 mshtml_group_commands_map_[IDM_BASELINEFONT4] = flags;
122 mshtml_group_commands_map_[IDM_BASELINEFONT5] = flags;
[email protected]a15d4a42010-02-17 08:21:35123
[email protected]3c6f8e12010-03-24 21:58:21124 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase);
[email protected]a15d4a42010-02-17 08:21:35125 accelerator_table_ =
[email protected]3c6f8e12010-03-24 21:58:21126 LoadAccelerators(this_module,
[email protected]a15d4a42010-02-17 08:21:35127 MAKEINTRESOURCE(IDR_CHROME_FRAME_IE_FULL_TAB));
128 DCHECK(accelerator_table_ != NULL);
[email protected]f7817822009-09-24 05:11:58129 return S_OK;
130}
131
132ChromeActiveDocument::~ChromeActiveDocument() {
133 DLOG(INFO) << __FUNCTION__;
[email protected]bc2ff5192010-06-01 22:05:45134 if (find_dialog_.IsWindow())
[email protected]f7817822009-09-24 05:11:58135 find_dialog_.DestroyWindow();
[email protected]3eb07da2010-02-01 19:48:36136 // ChromeFramePlugin
[email protected]97965e12010-04-09 00:51:10137 BaseActiveX::Uninitialize();
[email protected]04e3f352010-05-10 13:48:24138
139 TRACE_EVENT_END("chromeframe.createactivedocument", this, "");
[email protected]f7817822009-09-24 05:11:58140}
141
142// Override DoVerb
143STDMETHODIMP ChromeActiveDocument::DoVerb(LONG verb,
144 LPMSG msg,
145 IOleClientSite* active_site,
146 LONG index,
147 HWND parent_window,
148 LPCRECT pos) {
149 // IE will try and in-place activate us in some cases. This happens when
150 // the user opens a new IE window with a URL that has us as the DocObject.
151 // Here we refuse to be activated in-place and we will force IE to UIActivate
152 // us.
[email protected]bc2ff5192010-06-01 22:05:45153 if (OLEIVERB_INPLACEACTIVATE == verb)
[email protected]f7817822009-09-24 05:11:58154 return E_NOTIMPL;
[email protected]f7817822009-09-24 05:11:58155 // Check if we should activate as a docobject or not
156 // (client supports IOleDocumentSite)
157 if (doc_site_) {
158 switch (verb) {
[email protected]1bb5f892009-10-06 01:44:57159 case OLEIVERB_SHOW: {
160 ScopedComPtr<IDocHostUIHandler> doc_host_handler;
161 doc_host_handler.QueryFrom(doc_site_);
[email protected]bc2ff5192010-06-01 22:05:45162 if (doc_host_handler.get())
[email protected]1bb5f892009-10-06 01:44:57163 doc_host_handler->ShowUI(DOCHOSTUITYPE_BROWSE, this, this, NULL, NULL);
[email protected]1bb5f892009-10-06 01:44:57164 }
[email protected]f7817822009-09-24 05:11:58165 case OLEIVERB_OPEN:
166 case OLEIVERB_UIACTIVATE:
[email protected]bc2ff5192010-06-01 22:05:45167 if (!m_bUIActive)
[email protected]f7817822009-09-24 05:11:58168 return doc_site_->ActivateMe(NULL);
[email protected]f7817822009-09-24 05:11:58169 break;
170 }
171 }
172 return IOleObjectImpl<ChromeActiveDocument>::DoVerb(verb,
173 msg,
174 active_site,
175 index,
176 parent_window,
177 pos);
178}
179
[email protected]f7817822009-09-24 05:11:58180// Override IOleInPlaceActiveObjectImpl::OnDocWindowActivate
181STDMETHODIMP ChromeActiveDocument::OnDocWindowActivate(BOOL activate) {
182 DLOG(INFO) << __FUNCTION__;
183 return S_OK;
184}
185
186STDMETHODIMP ChromeActiveDocument::TranslateAccelerator(MSG* msg) {
187 DLOG(INFO) << __FUNCTION__;
188 if (msg == NULL)
189 return E_POINTER;
190
191 if (msg->message == WM_KEYDOWN && msg->wParam == VK_TAB) {
192 HWND focus = ::GetFocus();
193 if (focus != m_hWnd && !::IsChild(m_hWnd, focus)) {
194 // The call to SetFocus triggers a WM_SETFOCUS that makes the base class
195 // set focus to the correct element in Chrome.
196 ::SetFocus(m_hWnd);
197 return S_OK;
198 }
199 }
200
201 return S_FALSE;
202}
203// Override IPersistStorageImpl::IsDirty
204STDMETHODIMP ChromeActiveDocument::IsDirty() {
205 DLOG(INFO) << __FUNCTION__;
206 return S_FALSE;
207}
208
[email protected]b95f550f2009-11-19 05:35:22209void ChromeActiveDocument::OnAutomationServerReady() {
[email protected]97965e12010-04-09 00:51:10210 BaseActiveX::OnAutomationServerReady();
[email protected]1fd45692010-04-19 21:01:18211 BaseActiveX::GiveFocusToChrome(true);
[email protected]b95f550f2009-11-19 05:35:22212}
213
[email protected]f7817822009-09-24 05:11:58214STDMETHODIMP ChromeActiveDocument::Load(BOOL fully_avalable,
215 IMoniker* moniker_name,
216 LPBC bind_context,
217 DWORD mode) {
[email protected]bc2ff5192010-06-01 22:05:45218 if (NULL == moniker_name)
[email protected]f7817822009-09-24 05:11:58219 return E_INVALIDARG;
[email protected]a1800e82009-11-19 00:53:23220
221 ScopedComPtr<IOleClientSite> client_site;
222 if (bind_context) {
223 ScopedComPtr<IUnknown> site;
224 bind_context->GetObjectParam(SZ_HTML_CLIENTSITE_OBJECTPARAM,
225 site.Receive());
226 if (site)
227 client_site.QueryFrom(site);
228 }
229
230 if (client_site) {
231 SetClientSite(client_site);
[email protected]b1c55638612010-03-08 16:26:11232 DoQueryService(IID_INewWindowManager, client_site,
233 popup_manager_.Receive());
[email protected]051236f2010-03-12 22:06:14234
235 // See if mshtml parsed the html header for us. If so, we need to
236 // clear the browser service flag that we use to indicate that this
237 // browser instance is navigating to a CF document.
238 ScopedComPtr<IBrowserService> browser_service;
239 DoQueryService(SID_SShellBrowser, client_site, browser_service.Receive());
240 if (browser_service) {
241 bool flagged = CheckForCFNavigation(browser_service, true);
242 DLOG_IF(INFO, flagged) << "Cleared flagged browser service";
243 }
[email protected]a1800e82009-11-19 00:53:23244 }
245
[email protected]051236f2010-03-12 22:06:14246 NavigationManager* mgr = NavigationManager::GetThreadInstance();
[email protected]21b4e6d2010-07-14 18:19:59247 DLOG_IF(ERROR, !mgr) << "Couldn't get instance of NavigationManager";
[email protected]7e3544b2010-01-22 00:02:34248
[email protected]29c32f902010-04-20 23:27:29249 std::wstring url;
250
[email protected]77d7aee2010-05-14 20:31:55251 ScopedComPtr<BindContextInfo> info;
252 BindContextInfo::FromBindContext(bind_context, info.Receive());
[email protected]29c32f902010-04-20 23:27:29253 DCHECK(info);
254 if (info && !info->url().empty()) {
255 url = info->url();
256 } else {
257 // If the original URL contains an anchor, then the URL queried
258 // from the moniker does not contain the anchor. To workaround
259 // this we retrieve the URL from our BHO.
260 url = GetActualUrlFromMoniker(moniker_name, bind_context,
261 mgr ? mgr->url(): std::wstring());
262 }
[email protected]051236f2010-03-12 22:06:14263
[email protected]c4e45b32010-07-28 21:15:15264 ChromeFrameUrl cf_url;
265 if (!cf_url.Parse(url)) {
[email protected]37346bc2009-09-25 22:54:33266 DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
[email protected]f7817822009-09-24 05:11:58267 return E_INVALIDARG;
268 }
269
[email protected]bbfa9a12010-08-10 14:09:37270 std::string referrer(mgr ? mgr->referrer() : EmptyString());
271
[email protected]aaf124502010-07-17 04:02:58272 // With CTransaction patch we have more robust way to grab the referrer for
273 // each top-level-switch-to-CF request by peeking at our sniffing data
274 // object that lives inside the bind context.
275 if (g_patch_helper.state() == PatchHelper::PATCH_PROTOCOL && info) {
276 scoped_refptr<ProtData> prot_data = info->get_prot_data();
277 if (prot_data)
278 referrer = prot_data->referrer();
279 }
280
[email protected]62ce09732010-09-03 18:32:04281 // For gcf: URLs allow only about and view-source schemes to pass through for
282 // further inspection.
283 bool is_safe_scheme = cf_url.gurl().SchemeIs(chrome::kAboutScheme) ||
284 cf_url.gurl().SchemeIs(chrome::kViewSourceScheme);
285 if (cf_url.is_chrome_protocol() && !is_safe_scheme &&
286 !GetConfigBool(false, kAllowUnsafeURLs)) {
287 DLOG(ERROR) << __FUNCTION__ << " gcf: not allowed:" << url;
288 return E_INVALIDARG;
289 }
290
[email protected]c4e45b32010-07-28 21:15:15291 if (!LaunchUrl(cf_url, referrer)) {
[email protected]d266ce82010-08-13 21:33:40292 DLOG(ERROR) << __FUNCTION__ << " Failed to launch url:" << url;
[email protected]37346bc2009-09-25 22:54:33293 return E_INVALIDARG;
[email protected]f7817822009-09-24 05:11:58294 }
295
[email protected]c4e45b32010-07-28 21:15:15296 if (!cf_url.is_chrome_protocol() && !cf_url.attach_to_external_tab())
[email protected]d266ce82010-08-13 21:33:40297 url_fetcher_->SetInfoForUrl(url.c_str(), moniker_name, bind_context);
[email protected]f7817822009-09-24 05:11:58298
[email protected]333590002010-03-05 18:49:21299 THREAD_SAFE_UMA_HISTOGRAM_CUSTOM_COUNTS("ChromeFrame.FullTabLaunchType",
[email protected]c4e45b32010-07-28 21:15:15300 cf_url.is_chrome_protocol(),
301 0, 1, 2);
[email protected]f7817822009-09-24 05:11:58302 return S_OK;
303}
304
305STDMETHODIMP ChromeActiveDocument::Save(IMoniker* moniker_name,
306 LPBC bind_context,
307 BOOL remember) {
308 return E_NOTIMPL;
309}
310
311STDMETHODIMP ChromeActiveDocument::SaveCompleted(IMoniker* moniker_name,
312 LPBC bind_context) {
313 return E_NOTIMPL;
314}
315
316STDMETHODIMP ChromeActiveDocument::GetCurMoniker(IMoniker** moniker_name) {
317 return E_NOTIMPL;
318}
319
320STDMETHODIMP ChromeActiveDocument::GetClassID(CLSID* class_id) {
[email protected]bc2ff5192010-06-01 22:05:45321 if (NULL == class_id)
[email protected]f7817822009-09-24 05:11:58322 return E_POINTER;
[email protected]f7817822009-09-24 05:11:58323 *class_id = GetObjectCLSID();
324 return S_OK;
325}
326
327STDMETHODIMP ChromeActiveDocument::QueryStatus(const GUID* cmd_group_guid,
328 ULONG number_of_commands,
329 OLECMD commands[],
330 OLECMDTEXT* command_text) {
331 DLOG(INFO) << __FUNCTION__;
[email protected]b509ec82010-04-13 16:53:06332
[email protected]6fd1a7a2010-07-26 22:09:46333 CommandStatusMap* command_map = NULL;
[email protected]b509ec82010-04-13 16:53:06334
[email protected]6fd1a7a2010-07-26 22:09:46335 if (cmd_group_guid) {
336 if (IsEqualGUID(*cmd_group_guid, GUID_NULL)) {
337 command_map = &null_group_commands_map_;
338 } else if (IsEqualGUID(*cmd_group_guid, CGID_MSHTML)) {
339 command_map = &mshtml_group_commands_map_;
340 } else if (IsEqualGUID(*cmd_group_guid, CGID_Explorer)) {
341 command_map = &explorer_group_commands_map_;
342 } else if (IsEqualGUID(*cmd_group_guid, CGID_ShellDocView)) {
343 command_map = &shdoc_view_group_commands_map_;
344 }
345 } else {
346 command_map = &null_group_commands_map_;
347 }
348
349 if (!command_map) {
[email protected]b509ec82010-04-13 16:53:06350 DLOG(INFO) << "unsupported command group: "
351 << GuidToString(*cmd_group_guid);
352 return OLECMDERR_E_NOTSUPPORTED;
353 }
354
[email protected]f7817822009-09-24 05:11:58355 for (ULONG command_index = 0; command_index < number_of_commands;
356 command_index++) {
357 DLOG(INFO) << "Command id = " << commands[command_index].cmdID;
[email protected]6fd1a7a2010-07-26 22:09:46358 CommandStatusMap::iterator index =
359 command_map->find(commands[command_index].cmdID);
360 if (index != command_map->end())
361 commands[command_index].cmdf = index->second;
[email protected]f7817822009-09-24 05:11:58362 }
363 return S_OK;
364}
365
366STDMETHODIMP ChromeActiveDocument::Exec(const GUID* cmd_group_guid,
367 DWORD command_id,
368 DWORD cmd_exec_opt,
369 VARIANT* in_args,
370 VARIANT* out_args) {
371 DLOG(INFO) << __FUNCTION__ << " Cmd id =" << command_id;
372 // Bail out if we have been uninitialized.
373 if (automation_client_.get() && automation_client_->tab()) {
374 return ProcessExecCommand(cmd_group_guid, command_id, cmd_exec_opt,
375 in_args, out_args);
[email protected]bbfa9a12010-08-10 14:09:37376 } else if (command_id == OLECMDID_REFRESH && cmd_group_guid == NULL) {
377 // If the automation server has crashed and the user is refreshing the
378 // page, let OnRefreshPage attempt to recover.
379 OnRefreshPage(cmd_group_guid, command_id, cmd_exec_opt, in_args, out_args);
[email protected]f7817822009-09-24 05:11:58380 }
[email protected]bbfa9a12010-08-10 14:09:37381
[email protected]e3200932009-10-09 21:33:03382 return OLECMDERR_E_NOTSUPPORTED;
[email protected]f7817822009-09-24 05:11:58383}
384
[email protected]a1800e82009-11-19 00:53:23385STDMETHODIMP ChromeActiveDocument::LoadHistory(IStream* stream,
386 IBindCtx* bind_context) {
387 // Read notes in ChromeActiveDocument::SaveHistory
388 DCHECK(stream);
389 LARGE_INTEGER offset = {0};
390 ULARGE_INTEGER cur_pos = {0};
391 STATSTG statstg = {0};
392
393 stream->Seek(offset, STREAM_SEEK_CUR, &cur_pos);
394 stream->Stat(&statstg, STATFLAG_NONAME);
395
396 DWORD url_size = statstg.cbSize.LowPart - cur_pos.LowPart;
397 ScopedBstr url_bstr;
398 DWORD bytes_read = 0;
399 stream->Read(url_bstr.AllocateBytes(url_size), url_size, &bytes_read);
400 std::wstring url(url_bstr);
401
[email protected]c4e45b32010-07-28 21:15:15402 ChromeFrameUrl cf_url;
403 if (!cf_url.Parse(url)) {
[email protected]a1800e82009-11-19 00:53:23404 DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
405 return E_INVALIDARG;
406 }
407
[email protected]c5cbf4e2010-07-15 21:48:25408 const std::string& referrer = EmptyString();
[email protected]c4e45b32010-07-28 21:15:15409 if (!LaunchUrl(cf_url, referrer)) {
[email protected]a1800e82009-11-19 00:53:23410 NOTREACHED() << __FUNCTION__ << " Failed to launch url:" << url;
411 return E_INVALIDARG;
412 }
413 return S_OK;
414}
415
416STDMETHODIMP ChromeActiveDocument::SaveHistory(IStream* stream) {
417 // TODO(sanjeevr): We need to fetch the entire list of navigation entries
418 // from Chrome and persist it in the stream. And in LoadHistory we need to
419 // pass this list back to Chrome which will recreate the list. This will allow
420 // Back-Forward navigation to anchors to work correctly when we navigate to a
421 // page outside of ChromeFrame and then come back.
422 if (!stream) {
423 NOTREACHED();
424 return E_INVALIDARG;
425 }
426
427 LARGE_INTEGER offset = {0};
428 ULARGE_INTEGER new_pos = {0};
429 DWORD written = 0;
430 std::wstring url = UTF8ToWide(navigation_info_.url.spec());
431 return stream->Write(url.c_str(), (url.length() + 1) * sizeof(wchar_t),
432 &written);
433}
434
435STDMETHODIMP ChromeActiveDocument::SetPositionCookie(DWORD position_cookie) {
[email protected]77700832010-04-27 00:06:03436 if (automation_client_.get()) {
437 int index = static_cast<int>(position_cookie);
438 navigation_info_.navigation_index = index;
439 automation_client_->NavigateToIndex(index);
440 } else {
441 DLOG(WARNING) << "Invalid automation client instance";
442 }
[email protected]a1800e82009-11-19 00:53:23443 return S_OK;
444}
445
446STDMETHODIMP ChromeActiveDocument::GetPositionCookie(DWORD* position_cookie) {
447 if (!position_cookie)
448 return E_INVALIDARG;
449
450 *position_cookie = navigation_info_.navigation_index;
451 return S_OK;
452}
453
[email protected]f7817822009-09-24 05:11:58454STDMETHODIMP ChromeActiveDocument::GetUrlForEvents(BSTR* url) {
[email protected]bc2ff5192010-06-01 22:05:45455 if (NULL == url)
[email protected]f7817822009-09-24 05:11:58456 return E_POINTER;
[email protected]f7817822009-09-24 05:11:58457 *url = ::SysAllocString(url_);
458 return S_OK;
459}
460
[email protected]a1800e82009-11-19 00:53:23461STDMETHODIMP ChromeActiveDocument::GetAddressBarUrl(BSTR* url) {
462 return GetUrlForEvents(url);
463}
464
[email protected]80b5a8d2010-03-19 16:50:43465STDMETHODIMP ChromeActiveDocument::Reset() {
466 next_privacy_record_ = privacy_info_.privacy_records.begin();
467 return S_OK;
468}
469
[email protected]00aebd72010-07-16 14:44:32470STDMETHODIMP ChromeActiveDocument::GetSize(DWORD* size) {
[email protected]80b5a8d2010-03-19 16:50:43471 if (!size)
472 return E_POINTER;
473
474 *size = privacy_info_.privacy_records.size();
475 return S_OK;
476}
477
478STDMETHODIMP ChromeActiveDocument::GetPrivacyImpacted(BOOL* privacy_impacted) {
479 if (!privacy_impacted)
480 return E_POINTER;
481
482 *privacy_impacted = privacy_info_.privacy_impacted;
483 return S_OK;
484}
485
486STDMETHODIMP ChromeActiveDocument::Next(BSTR* url, BSTR* policy,
[email protected]00aebd72010-07-16 14:44:32487 LONG* reserved, DWORD* flags) {
[email protected]80b5a8d2010-03-19 16:50:43488 if (!url || !policy || !flags)
489 return E_POINTER;
490
491 if (next_privacy_record_ == privacy_info_.privacy_records.end())
492 return HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS);
493
494 *url = SysAllocString(next_privacy_record_->first.c_str());
495 *policy = SysAllocString(next_privacy_record_->second.policy_ref.c_str());
496 *flags = next_privacy_record_->second.flags;
497
498 next_privacy_record_++;
499 return S_OK;
500}
501
[email protected]3eafbfb92010-08-02 16:54:22502HRESULT ChromeActiveDocument::GetInPlaceFrame(
503 IOleInPlaceFrame** in_place_frame) {
504 DCHECK(in_place_frame);
505 if (in_place_frame_) {
506 *in_place_frame = in_place_frame_.get();
507 (*in_place_frame)->AddRef();
508 return S_OK;
509 } else {
510 return S_FALSE;
511 }
512}
513
[email protected]f7817822009-09-24 05:11:58514HRESULT ChromeActiveDocument::IOleObject_SetClientSite(
515 IOleClientSite* client_site) {
516 if (client_site == NULL) {
517 ChromeActiveDocument* cached_document = g_active_doc_cache.Get();
518 if (cached_document) {
519 DCHECK(this == cached_document);
520 g_active_doc_cache.Set(NULL);
521 cached_document->Release();
522 }
[email protected]dda7d9c2009-11-11 23:01:47523
524 ScopedComPtr<IDocHostUIHandler> doc_host_handler;
[email protected]bc2ff5192010-06-01 22:05:45525 if (doc_site_)
[email protected]77059f72010-02-28 06:16:00526 doc_host_handler.QueryFrom(doc_site_);
[email protected]dda7d9c2009-11-11 23:01:47527
[email protected]bc2ff5192010-06-01 22:05:45528 if (doc_host_handler.get())
[email protected]dda7d9c2009-11-11 23:01:47529 doc_host_handler->HideUI();
[email protected]dda7d9c2009-11-11 23:01:47530
531 doc_site_.Release();
[email protected]f7817822009-09-24 05:11:58532 }
[email protected]a1800e82009-11-19 00:53:23533
[email protected]bc2ff5192010-06-01 22:05:45534 if (client_site != m_spClientSite)
[email protected]97965e12010-04-09 00:51:10535 return BaseActiveX::IOleObject_SetClientSite(client_site);
[email protected]a1800e82009-11-19 00:53:23536
537 return S_OK;
[email protected]f7817822009-09-24 05:11:58538}
539
[email protected]f7817822009-09-24 05:11:58540HRESULT ChromeActiveDocument::ActiveXDocActivate(LONG verb) {
541 HRESULT hr = S_OK;
542 m_bNegotiatedWnd = TRUE;
543 if (!m_bInPlaceActive) {
544 hr = m_spInPlaceSite->CanInPlaceActivate();
[email protected]bc2ff5192010-06-01 22:05:45545 if (FAILED(hr))
[email protected]f7817822009-09-24 05:11:58546 return hr;
[email protected]f7817822009-09-24 05:11:58547 m_spInPlaceSite->OnInPlaceActivate();
548 }
549 m_bInPlaceActive = TRUE;
550 // get location in the parent window,
551 // as well as some information about the parent
552 ScopedComPtr<IOleInPlaceUIWindow> in_place_ui_window;
553 frame_info_.cb = sizeof(OLEINPLACEFRAMEINFO);
554 HWND parent_window = NULL;
555 if (m_spInPlaceSite->GetWindow(&parent_window) == S_OK) {
556 in_place_frame_.Release();
557 RECT position_rect = {0};
558 RECT clip_rect = {0};
559 m_spInPlaceSite->GetWindowContext(in_place_frame_.Receive(),
560 in_place_ui_window.Receive(),
561 &position_rect,
562 &clip_rect,
563 &frame_info_);
564 if (!m_bWndLess) {
565 if (IsWindow()) {
566 ::ShowWindow(m_hWnd, SW_SHOW);
567 SetFocus();
568 } else {
[email protected]c1d19582010-03-03 01:57:13569 m_hWnd = Create(parent_window, position_rect, 0, 0, WS_EX_CLIENTEDGE);
[email protected]dd4beb522010-07-13 18:18:14570 if (!IsWindow()) {
571 // This might happen if the automation server couldn't be
572 // instantiated. If so, a NOTREACHED() will have already been hit.
573 DLOG(ERROR) << "Failed to create Ax window";
574 return AtlHresultFromLastError();
575 }
[email protected]f7817822009-09-24 05:11:58576 }
[email protected]7f860dd82010-08-09 23:18:05577 SetWindowDimensions();
[email protected]f7817822009-09-24 05:11:58578 }
579 SetObjectRects(&position_rect, &clip_rect);
580 }
581
582 ScopedComPtr<IOleInPlaceActiveObject> in_place_active_object(this);
583
584 // Gone active by now, take care of UIACTIVATE
585 if (DoesVerbUIActivate(verb)) {
586 if (!m_bUIActive) {
587 m_bUIActive = TRUE;
588 hr = m_spInPlaceSite->OnUIActivate();
[email protected]bc2ff5192010-06-01 22:05:45589 if (FAILED(hr))
[email protected]f7817822009-09-24 05:11:58590 return hr;
[email protected]f7817822009-09-24 05:11:58591 // set ourselves up in the host
592 if (in_place_active_object) {
[email protected]bc2ff5192010-06-01 22:05:45593 if (in_place_frame_)
[email protected]f7817822009-09-24 05:11:58594 in_place_frame_->SetActiveObject(in_place_active_object, NULL);
[email protected]bc2ff5192010-06-01 22:05:45595 if (in_place_ui_window)
[email protected]f7817822009-09-24 05:11:58596 in_place_ui_window->SetActiveObject(in_place_active_object, NULL);
[email protected]f7817822009-09-24 05:11:58597 }
598 }
599 }
600 m_spClientSite->ShowObject();
601 return S_OK;
602}
603
604void ChromeActiveDocument::OnNavigationStateChanged(int tab_handle, int flags,
605 const IPC::NavigationInfo& nav_info) {
606 // TODO(joshia): handle INVALIDATE_TAB,INVALIDATE_LOAD etc.
607 DLOG(INFO) << __FUNCTION__ << std::endl << " Flags: " << flags
608 << "Url: " << nav_info.url <<
609 ", Title: " << nav_info.title <<
610 ", Type: " << nav_info.navigation_type << ", Relative Offset: " <<
[email protected]62bb18dc12009-11-25 01:34:08611 nav_info.relative_offset << ", Index: " << nav_info.navigation_index;
[email protected]f7817822009-09-24 05:11:58612
613 UpdateNavigationState(nav_info);
614}
615
616void ChromeActiveDocument::OnUpdateTargetUrl(int tab_handle,
617 const std::wstring& new_target_url) {
[email protected]bc2ff5192010-06-01 22:05:45618 if (in_place_frame_)
[email protected]f7817822009-09-24 05:11:58619 in_place_frame_->SetStatusText(new_target_url.c_str());
[email protected]f7817822009-09-24 05:11:58620}
621
622bool IsFindAccelerator(const MSG& msg) {
623 // TODO(robertshield): This may not stand up to localization. Fix if this
624 // is the case.
625 return msg.message == WM_KEYDOWN && msg.wParam == 'F' &&
626 win_util::IsCtrlPressed() &&
627 !(win_util::IsAltPressed() || win_util::IsShiftPressed());
628}
629
630void ChromeActiveDocument::OnAcceleratorPressed(int tab_handle,
631 const MSG& accel_message) {
[email protected]a15d4a42010-02-17 08:21:35632 if (::TranslateAccelerator(m_hWnd, accelerator_table_,
633 const_cast<MSG*>(&accel_message)))
634 return;
635
[email protected]f7817822009-09-24 05:11:58636 bool handled_accel = false;
637 if (in_place_frame_ != NULL) {
638 handled_accel = (S_OK == in_place_frame_->TranslateAcceleratorW(
639 const_cast<MSG*>(&accel_message), 0));
640 }
641
642 if (!handled_accel) {
643 if (IsFindAccelerator(accel_message)) {
644 // Handle the showing of the find dialog explicitly.
645 OnFindInPage();
[email protected]2b8fd322009-10-02 00:00:59646 } else {
[email protected]97965e12010-04-09 00:51:10647 BaseActiveX::OnAcceleratorPressed(tab_handle, accel_message);
[email protected]f7817822009-09-24 05:11:58648 }
649 } else {
650 DLOG(INFO) << "IE handled accel key " << accel_message.wParam;
651 }
652}
653
654void ChromeActiveDocument::OnTabbedOut(int tab_handle, bool reverse) {
655 DLOG(INFO) << __FUNCTION__;
656 if (in_place_frame_) {
657 MSG msg = { NULL, WM_KEYDOWN, VK_TAB };
658 in_place_frame_->TranslateAcceleratorW(&msg, 0);
659 }
660}
661
662void ChromeActiveDocument::OnDidNavigate(int tab_handle,
663 const IPC::NavigationInfo& nav_info) {
664 DLOG(INFO) << __FUNCTION__ << std::endl << "Url: " << nav_info.url <<
665 ", Title: " << nav_info.title <<
666 ", Type: " << nav_info.navigation_type << ", Relative Offset: " <<
667 nav_info.relative_offset << ", Index: " << nav_info.navigation_index;
668
[email protected]897b26272010-06-11 02:23:44669 CrashMetricsReporter::GetInstance()->IncrementMetric(
670 CrashMetricsReporter::CHROME_FRAME_NAVIGATION_COUNT);
671
[email protected]f7817822009-09-24 05:11:58672 // This could be NULL if the active document instance is being destroyed.
673 if (!m_spInPlaceSite) {
674 DLOG(INFO) << __FUNCTION__ << "m_spInPlaceSite is NULL. Returning";
675 return;
676 }
677
678 UpdateNavigationState(nav_info);
679}
680
[email protected]e16dd1672010-06-07 21:40:29681void ChromeActiveDocument::OnCloseTab(int tab_handle) {
682 ScopedComPtr<IWebBrowser2> web_browser2;
683 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
684 if (web_browser2)
685 web_browser2->Quit();
686}
687
[email protected]f7817822009-09-24 05:11:58688void ChromeActiveDocument::UpdateNavigationState(
689 const IPC::NavigationInfo& new_navigation_info) {
[email protected]a1800e82009-11-19 00:53:23690 HRESULT hr = S_OK;
[email protected]f7817822009-09-24 05:11:58691 bool is_title_changed = (navigation_info_.title != new_navigation_info.title);
[email protected]f7817822009-09-24 05:11:58692 bool is_ssl_state_changed =
693 (navigation_info_.security_style != new_navigation_info.security_style) ||
[email protected]b4e75c12010-05-18 18:28:48694 (navigation_info_.displayed_insecure_content !=
695 new_navigation_info.displayed_insecure_content) ||
696 (navigation_info_.ran_insecure_content !=
697 new_navigation_info.ran_insecure_content);
[email protected]f7817822009-09-24 05:11:58698
[email protected]f7817822009-09-24 05:11:58699 if (is_ssl_state_changed) {
700 int lock_status = SECURELOCK_SET_UNSECURE;
[email protected]a1800e82009-11-19 00:53:23701 switch (new_navigation_info.security_style) {
[email protected]f7817822009-09-24 05:11:58702 case SECURITY_STYLE_AUTHENTICATION_BROKEN:
703 lock_status = SECURELOCK_SET_SECUREUNKNOWNBIT;
704 break;
705 case SECURITY_STYLE_AUTHENTICATED:
[email protected]b4e75c12010-05-18 18:28:48706 lock_status = new_navigation_info.displayed_insecure_content ?
[email protected]f7817822009-09-24 05:11:58707 SECURELOCK_SET_MIXED : SECURELOCK_SET_SECUREUNKNOWNBIT;
708 break;
709 default:
710 break;
711 }
712
713 ScopedVariant secure_lock_status(lock_status);
714 IEExec(&CGID_ShellDocView, INTERNAL_CMDID_SET_SSL_LOCK,
715 OLECMDEXECOPT_DODEFAULT, secure_lock_status.AsInput(), NULL);
716 }
717
[email protected]a1800e82009-11-19 00:53:23718 // Ideally all navigations should come to Chrome Frame so that we can call
719 // BeforeNavigate2 on installed BHOs and give them a chance to cancel the
720 // navigation. However, in practice what happens is as below:
721 // The very first navigation that happens in CF happens via a Load or a
722 // LoadHistory call. In this case, IE already has the correct information for
723 // its travel log as well address bar. For other internal navigations (navs
724 // that only happen within Chrome such as anchor navigations) we need to
725 // update IE's internal state after the fact. In the case of internal
726 // navigations, we notify the BHOs but ignore the should_cancel flag.
[email protected]6f526082010-01-28 19:36:58727
728 // Another case where we need to issue BeforeNavigate2 calls is as below:-
729 // We get notified after the fact, when navigations are initiated within
730 // Chrome via window.open calls. These navigations are handled by creating
731 // an external tab container within chrome and then connecting to it from IE.
732 // We still want to update the address bar/history, etc, to ensure that
733 // the special URL used by Chrome to indicate this is updated correctly.
[email protected]d266ce82010-08-13 21:33:40734 ChromeFrameUrl cf_url;
735 bool is_attach_external_tab_url = cf_url.Parse(std::wstring(url_)) &&
736 cf_url.attach_to_external_tab();
[email protected]6f526082010-01-28 19:36:58737 bool is_internal_navigation = ((new_navigation_info.navigation_index > 0) &&
[email protected]a1800e82009-11-19 00:53:23738 (new_navigation_info.navigation_index !=
[email protected]d266ce82010-08-13 21:33:40739 navigation_info_.navigation_index)) || is_attach_external_tab_url;
[email protected]f7817822009-09-24 05:11:58740
[email protected]bc2ff5192010-06-01 22:05:45741 if (new_navigation_info.url.is_valid())
[email protected]38939de2010-05-13 02:32:06742 url_.Allocate(UTF8ToWide(new_navigation_info.url.spec()).c_str());
[email protected]38939de2010-05-13 02:32:06743
[email protected]a1800e82009-11-19 00:53:23744 if (is_internal_navigation) {
745 ScopedComPtr<IDocObjectService> doc_object_svc;
[email protected]f7817822009-09-24 05:11:58746 ScopedComPtr<IWebBrowserEventsService> web_browser_events_svc;
[email protected]ae33d672010-03-04 21:58:06747
[email protected]5ae94d22010-07-21 19:55:36748 buggy_bho::BuggyBhoTls bad_bho_tls;
749 if (GetConfigBool(true, kEnableBuggyBhoIntercept)) {
750 ScopedComPtr<IWebBrowser2> wb2;
751 DoQueryService(SID_SWebBrowserApp, m_spClientSite, wb2.Receive());
752 if (wb2) {
753 buggy_bho::BuggyBhoTls::PatchBuggyBHOs(wb2);
754 }
755 }
756
[email protected]f7817822009-09-24 05:11:58757 DoQueryService(__uuidof(web_browser_events_svc), m_spClientSite,
758 web_browser_events_svc.Receive());
[email protected]ae33d672010-03-04 21:58:06759
760 if (!web_browser_events_svc.get()) {
761 DoQueryService(SID_SShellBrowser, m_spClientSite,
762 doc_object_svc.Receive());
763 }
764
[email protected]62bb18dc12009-11-25 01:34:08765 // web_browser_events_svc can be NULL on IE6.
[email protected]f7817822009-09-24 05:11:58766 if (web_browser_events_svc) {
[email protected]f7817822009-09-24 05:11:58767 VARIANT_BOOL should_cancel = VARIANT_FALSE;
768 web_browser_events_svc->FireBeforeNavigate2Event(&should_cancel);
[email protected]33bd26d2010-08-31 05:05:03769 } else if (doc_object_svc) {
770 BOOL should_cancel = FALSE;
771 doc_object_svc->FireBeforeNavigate2(NULL, url_, 0, NULL, NULL, 0,
772 NULL, FALSE, &should_cancel);
[email protected]a1800e82009-11-19 00:53:23773 }
774
775 // We need to tell IE that we support navigation so that IE will query us
776 // for IPersistHistory and call GetPositionCookie to save our navigation
777 // index.
778 ScopedVariant html_window(static_cast<IUnknown*>(
779 static_cast<IHTMLWindow2*>(this)));
780 IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0,
781 html_window.AsInput(), NULL);
782
783 // We pass the HLNF_INTERNALJUMP flag to INTERNAL_CMDID_FINALIZE_TRAVEL_LOG
784 // since we want to make IE treat all internal navigations within this page
785 // (including anchor navigations and subframe navigations) as anchor
786 // navigations. This will ensure that IE calls GetPositionCookie
787 // to save the current position cookie in the travel log and then call
788 // SetPositionCookie when the user hits Back/Forward to come back here.
789 ScopedVariant internal_navigation(HLNF_INTERNALJUMP);
790 IEExec(&CGID_Explorer, INTERNAL_CMDID_FINALIZE_TRAVEL_LOG, 0,
791 internal_navigation.AsInput(), NULL);
792
793 // We no longer need to lie to IE. If we lie persistently to IE, then
794 // IE reuses us for new navigations.
795 IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0, NULL, NULL);
796
797 if (doc_object_svc) {
798 // Now call the FireNavigateCompleteEvent which makes IE update the text
799 // in the address-bar.
800 doc_object_svc->FireNavigateComplete2(this, 0);
[email protected]5ae94d22010-07-21 19:55:36801 doc_object_svc->FireDocumentComplete(this, 0);
[email protected]a1800e82009-11-19 00:53:23802 } else if (web_browser_events_svc) {
[email protected]f7817822009-09-24 05:11:58803 web_browser_events_svc->FireNavigateComplete2Event();
[email protected]5ae94d22010-07-21 19:55:36804 web_browser_events_svc->FireDocumentCompleteEvent();
[email protected]f7817822009-09-24 05:11:58805 }
806 }
[email protected]37346bc2009-09-25 22:54:33807
[email protected]a1800e82009-11-19 00:53:23808 if (is_title_changed) {
809 ScopedVariant title(new_navigation_info.title.c_str());
810 IEExec(NULL, OLECMDID_SETTITLE, OLECMDEXECOPT_DONTPROMPTUSER,
811 title.AsInput(), NULL);
812 }
813
814 // It is important that we only update the navigation_info_ after we have
815 // finalized the travel log. This is because IE will ask for information
816 // such as navigation index when the travel log is finalized and we need
817 // supply the old index and not the new one.
818 navigation_info_ = new_navigation_info;
[email protected]37346bc2009-09-25 22:54:33819 // Update the IE zone here. Ideally we would like to do it when the active
820 // document is activated. However that does not work at times as the frame we
821 // get there is not the actual frame which handles the command.
822 IEExec(&CGID_Explorer, SBCMDID_MIXEDZONE, 0, NULL, NULL);
[email protected]f7817822009-09-24 05:11:58823}
824
825void ChromeActiveDocument::OnFindInPage() {
826 TabProxy* tab = GetTabProxy();
827 if (tab) {
[email protected]bc2ff5192010-06-01 22:05:45828 if (!find_dialog_.IsWindow())
[email protected]f7817822009-09-24 05:11:58829 find_dialog_.Create(m_hWnd);
[email protected]f7817822009-09-24 05:11:58830
831 find_dialog_.ShowWindow(SW_SHOW);
832 }
833}
834
835void ChromeActiveDocument::OnViewSource() {
836 DCHECK(navigation_info_.url.is_valid());
[email protected]76e7da22010-06-18 22:44:49837 HostNavigate(GURL(chrome::kViewSourceScheme + std::string(":") +
838 navigation_info_.url.spec()), GURL(), NEW_WINDOW);
[email protected]f7817822009-09-24 05:11:58839}
840
[email protected]37346bc2009-09-25 22:54:33841void ChromeActiveDocument::OnDetermineSecurityZone(const GUID* cmd_group_guid,
842 DWORD command_id,
843 DWORD cmd_exec_opt,
844 VARIANT* in_args,
845 VARIANT* out_args) {
[email protected]7c712c92010-03-24 17:29:22846 // Always return URLZONE_INTERNET since that is the Chrome's behaviour.
847 // Correct step is to use MapUrlToZone().
[email protected]37346bc2009-09-25 22:54:33848 if (out_args != NULL) {
849 out_args->vt = VT_UI4;
850 out_args->ulVal = URLZONE_INTERNET;
851 }
852}
853
[email protected]80b5a8d2010-03-19 16:50:43854void ChromeActiveDocument::OnDisplayPrivacyInfo() {
[email protected]bbfa9a12010-08-10 14:09:37855 privacy_info_ = url_fetcher_->privacy_info();
[email protected]80b5a8d2010-03-19 16:50:43856 Reset();
857 DoPrivacyDlg(m_hWnd, url_, this, TRUE);
858}
859
[email protected]7f860dd82010-08-09 23:18:05860void ChromeActiveDocument::OnGetZoomRange(const GUID* cmd_group_guid,
861 DWORD command_id,
862 DWORD cmd_exec_opt,
863 VARIANT* in_args,
864 VARIANT* out_args) {
865 if (out_args != NULL) {
866 out_args->vt = VT_I4;
867 out_args->lVal = 0;
868 }
869}
870
871void ChromeActiveDocument::OnSetZoomRange(const GUID* cmd_group_guid,
872 DWORD command_id,
873 DWORD cmd_exec_opt,
874 VARIANT* in_args,
875 VARIANT* out_args) {
876 const int kZoomIn = 125;
877 const int kZoomOut = 75;
878
879 if (in_args && V_VT(in_args) == VT_I4 && IsValid()) {
880 if (in_args->lVal == kZoomIn) {
881 automation_client_->SetZoomLevel(PageZoom::ZOOM_IN);
882 } else if (in_args->lVal == kZoomOut) {
883 automation_client_->SetZoomLevel(PageZoom::ZOOM_OUT);
884 } else {
885 DLOG(WARNING) << "Unsupported zoom level:" << in_args->lVal;
886 }
887 }
888}
889
[email protected]b36a9f92009-10-19 17:34:57890void ChromeActiveDocument::OnOpenURL(int tab_handle,
891 const GURL& url_to_open,
892 const GURL& referrer,
[email protected]f7817822009-09-24 05:11:58893 int open_disposition) {
894 // If the disposition indicates that we should be opening the URL in the
895 // current tab, then we can reuse the ChromeFrameAutomationClient instance
896 // maintained by the current ChromeActiveDocument instance. We cache this
897 // instance so that it can be used by the new ChromeActiveDocument instance
898 // which may be instantiated for handling the new URL.
899 if (open_disposition == CURRENT_TAB) {
900 // Grab a reference to ensure that the document remains valid.
901 AddRef();
902 g_active_doc_cache.Set(this);
903 }
904
[email protected]97965e12010-04-09 00:51:10905 BaseActiveX::OnOpenURL(tab_handle, url_to_open, referrer, open_disposition);
[email protected]f7817822009-09-24 05:11:58906}
907
[email protected]b1c55638612010-03-08 16:26:11908void ChromeActiveDocument::OnAttachExternalTab(int tab_handle,
909 const IPC::AttachExternalTabParams& params) {
[email protected]77700832010-04-27 00:06:03910 if (!automation_client_.get()) {
911 DLOG(WARNING) << "Invalid automation client instance";
912 return;
913 }
[email protected]b1c55638612010-03-08 16:26:11914 DWORD flags = 0;
915 if (params.user_gesture)
916 flags = NWMF_USERREQUESTED;
[email protected]355309e2010-03-15 17:01:34917 else if (popup_allowed_)
918 flags = NWMF_USERALLOWED;
[email protected]b1c55638612010-03-08 16:26:11919
920 HRESULT hr = S_OK;
921 if (popup_manager_) {
[email protected]355309e2010-03-15 17:01:34922 LPCWSTR popup_wnd_url = UTF8ToWide(params.url.spec()).c_str();
923 hr = popup_manager_->EvaluateNewWindow(popup_wnd_url, NULL, url_,
924 NULL, FALSE, flags, 0);
[email protected]b1c55638612010-03-08 16:26:11925 }
926 // Allow popup
927 if (hr == S_OK) {
[email protected]97965e12010-04-09 00:51:10928 BaseActiveX::OnAttachExternalTab(tab_handle, params);
[email protected]b1c55638612010-03-08 16:26:11929 return;
930 }
931
932 automation_client_->BlockExternalTab(params.cookie);
933}
934
[email protected]f7817822009-09-24 05:11:58935bool ChromeActiveDocument::PreProcessContextMenu(HMENU menu) {
936 ScopedComPtr<IBrowserService> browser_service;
937 ScopedComPtr<ITravelLog> travel_log;
[email protected]a1800e82009-11-19 00:53:23938 GetBrowserServiceAndTravelLog(browser_service.Receive(),
939 travel_log.Receive());
940 if (!browser_service || !travel_log)
[email protected]f7817822009-09-24 05:11:58941 return true;
942
[email protected]bc2ff5192010-06-01 22:05:45943 EnableMenuItem(menu, IDS_CONTENT_CONTEXT_BACK, MF_BYCOMMAND |
944 (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_BACK, NULL)) ?
945 MF_ENABLED : MF_DISABLED));
946 EnableMenuItem(menu, IDS_CONTENT_CONTEXT_FORWARD, MF_BYCOMMAND |
947 (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_FORE, NULL)) ?
948 MF_ENABLED : MF_DISABLED));
[email protected]f7817822009-09-24 05:11:58949
950 // Call base class (adds 'About' item)
[email protected]97965e12010-04-09 00:51:10951 return BaseActiveX::PreProcessContextMenu(menu);
[email protected]f7817822009-09-24 05:11:58952}
953
[email protected]35f13ab2009-12-16 23:59:17954bool ChromeActiveDocument::HandleContextMenuCommand(UINT cmd,
955 const IPC::ContextMenuParams& params) {
[email protected]f7817822009-09-24 05:11:58956 ScopedComPtr<IWebBrowser2> web_browser2;
957 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
958
[email protected]074283a2010-05-28 23:47:59959 if (cmd == IDC_BACK)
[email protected]b731a142010-05-14 00:03:03960 web_browser2->GoBack();
[email protected]074283a2010-05-28 23:47:59961 else if (cmd == IDC_FORWARD)
[email protected]b731a142010-05-14 00:03:03962 web_browser2->GoForward();
[email protected]074283a2010-05-28 23:47:59963 else if (cmd == IDC_RELOAD)
[email protected]b731a142010-05-14 00:03:03964 web_browser2->Refresh();
[email protected]074283a2010-05-28 23:47:59965 else
[email protected]b731a142010-05-14 00:03:03966 return BaseActiveX::HandleContextMenuCommand(cmd, params);
[email protected]f7817822009-09-24 05:11:58967
968 return true;
969}
970
971HRESULT ChromeActiveDocument::IEExec(const GUID* cmd_group_guid,
972 DWORD command_id, DWORD cmd_exec_opt,
973 VARIANT* in_args, VARIANT* out_args) {
974 HRESULT hr = E_FAIL;
[email protected]37346bc2009-09-25 22:54:33975
[email protected]f7817822009-09-24 05:11:58976 ScopedComPtr<IOleCommandTarget> frame_cmd_target;
[email protected]37346bc2009-09-25 22:54:33977
978 ScopedComPtr<IOleInPlaceSite> in_place_site(m_spInPlaceSite);
[email protected]bc2ff5192010-06-01 22:05:45979 if (!in_place_site.get() && m_spClientSite != NULL)
[email protected]37346bc2009-09-25 22:54:33980 in_place_site.QueryFrom(m_spClientSite);
[email protected]37346bc2009-09-25 22:54:33981
982 if (in_place_site)
983 hr = frame_cmd_target.QueryFrom(in_place_site);
[email protected]f7817822009-09-24 05:11:58984
[email protected]bc2ff5192010-06-01 22:05:45985 if (frame_cmd_target) {
[email protected]f7817822009-09-24 05:11:58986 hr = frame_cmd_target->Exec(cmd_group_guid, command_id, cmd_exec_opt,
987 in_args, out_args);
[email protected]bc2ff5192010-06-01 22:05:45988 }
[email protected]f7817822009-09-24 05:11:58989
990 return hr;
991}
[email protected]37346bc2009-09-25 22:54:33992
[email protected]c4e45b32010-07-28 21:15:15993bool ChromeActiveDocument::LaunchUrl(const ChromeFrameUrl& cf_url,
994 const std::string& referrer) {
[email protected]d266ce82010-08-13 21:33:40995 DCHECK(!cf_url.gurl().is_empty());
[email protected]77700832010-04-27 00:06:03996
[email protected]242ceb032010-08-26 17:06:36997 if (!automation_client_.get()) {
998 // http://code.google.com/p/chromium/issues/detail?id=52894
999 // Still not sure how this happens.
1000 DLOG(ERROR) << "No automation client!";
1001 if (!Initialize()) {
1002 NOTREACHED() << "...and failed to start a new one >:(";
1003 return false;
1004 }
1005 }
1006
[email protected]d266ce82010-08-13 21:33:401007 url_.Allocate(UTF8ToWide(cf_url.gurl().spec()).c_str());
[email protected]c4e45b32010-07-28 21:15:151008 if (cf_url.attach_to_external_tab()) {
1009 dimensions_ = cf_url.dimensions();
1010 automation_client_->AttachExternalTab(cf_url.cookie());
[email protected]7f860dd82010-08-09 23:18:051011 SetWindowDimensions();
[email protected]d266ce82010-08-13 21:33:401012 } else if (!automation_client_->InitiateNavigation(cf_url.gurl().spec(),
[email protected]6f8f31d2010-07-30 08:17:151013 referrer,
1014 is_privileged_)) {
1015 DLOG(ERROR) << "Invalid URL: " << url_;
1016 Error(L"Invalid URL");
1017 url_.Reset();
1018 return false;
[email protected]37346bc2009-09-25 22:54:331019 }
1020
[email protected]3eb07da2010-02-01 19:48:361021 if (is_automation_client_reused_)
1022 return true;
[email protected]37346bc2009-09-25 22:54:331023
[email protected]bbfa9a12010-08-10 14:09:371024 automation_client_->SetUrlFetcher(url_fetcher_.get());
[email protected]701142a2010-08-31 20:57:071025 if (launch_params_) {
1026 return automation_client_->Initialize(this, launch_params_);
1027 } else {
[email protected]8103c7f2010-09-08 22:36:091028 std::wstring profile = UTF8ToWide(cf_url.profile_name());
1029 // If no profile was given, then make use of the host process's name.
1030 if (profile.empty())
1031 profile = GetHostProcessName(false);
1032 return InitializeAutomation(profile, L"", IsIEInPrivate(),
[email protected]701142a2010-08-31 20:57:071033 false, cf_url.gurl(), GURL(referrer));
1034 }
[email protected]37346bc2009-09-25 22:54:331035}
[email protected]1bb5f892009-10-06 01:44:571036
[email protected]355309e2010-03-15 17:01:341037
1038HRESULT ChromeActiveDocument::OnRefreshPage(const GUID* cmd_group_guid,
1039 DWORD command_id, DWORD cmd_exec_opt, VARIANT* in_args, VARIANT* out_args) {
[email protected]bbfa9a12010-08-10 14:09:371040 DLOG(INFO) << __FUNCTION__;
[email protected]355309e2010-03-15 17:01:341041 popup_allowed_ = false;
1042 if (in_args->vt == VT_I4 &&
1043 in_args->lVal & OLECMDIDF_REFRESH_PAGEACTION_POPUPWINDOW) {
1044 popup_allowed_ = true;
1045
1046 // Ask the yellow security band to change the text and icon and to remain
1047 // visible.
1048 IEExec(&CGID_DocHostCommandHandler, OLECMDID_PAGEACTIONBLOCKED,
[email protected]bbfa9a12010-08-10 14:09:371049 0x80000000 | OLECMDIDF_WINDOWSTATE_USERVISIBLE_VALID, NULL, NULL);
[email protected]355309e2010-03-15 17:01:341050 }
1051
1052 TabProxy* tab_proxy = GetTabProxy();
[email protected]bbfa9a12010-08-10 14:09:371053 if (tab_proxy) {
[email protected]355309e2010-03-15 17:01:341054 tab_proxy->ReloadAsync();
[email protected]bbfa9a12010-08-10 14:09:371055 } else {
1056 DLOG(ERROR) << "No automation proxy";
[email protected]242ceb032010-08-26 17:06:361057 DCHECK(automation_client_.get() != NULL) << "how did it get freed?";
[email protected]bbfa9a12010-08-10 14:09:371058 // The current url request manager (url_fetcher_) has been switched to
1059 // a stopping state so we need to reset it and get a new one for the new
1060 // automation server.
1061 ResetUrlRequestManager();
1062 url_fetcher_->set_frame_busting(false);
1063 // And now launch the current URL again. This starts a new server process.
1064 DCHECK(navigation_info_.url.is_valid());
1065 ChromeFrameUrl cf_url;
1066 cf_url.Parse(UTF8ToWide(navigation_info_.url.spec()));
1067 LaunchUrl(cf_url, navigation_info_.referrer.spec());
1068 }
[email protected]355309e2010-03-15 17:01:341069
1070 return S_OK;
1071}
1072
[email protected]1bb5f892009-10-06 01:44:571073HRESULT ChromeActiveDocument::SetPageFontSize(const GUID* cmd_group_guid,
1074 DWORD command_id,
1075 DWORD cmd_exec_opt,
1076 VARIANT* in_args,
1077 VARIANT* out_args) {
1078 if (!automation_client_.get()) {
[email protected]77700832010-04-27 00:06:031079 NOTREACHED() << "Invalid automation client";
[email protected]1bb5f892009-10-06 01:44:571080 return E_FAIL;
1081 }
1082
1083 switch (command_id) {
1084 case IDM_BASELINEFONT1:
1085 automation_client_->SetPageFontSize(SMALLEST_FONT);
1086 break;
1087
1088 case IDM_BASELINEFONT2:
1089 automation_client_->SetPageFontSize(SMALL_FONT);
1090 break;
1091
1092 case IDM_BASELINEFONT3:
1093 automation_client_->SetPageFontSize(MEDIUM_FONT);
1094 break;
1095
1096 case IDM_BASELINEFONT4:
1097 automation_client_->SetPageFontSize(LARGE_FONT);
1098 break;
1099
1100 case IDM_BASELINEFONT5:
1101 automation_client_->SetPageFontSize(LARGEST_FONT);
1102 break;
1103
1104 default:
1105 NOTREACHED() << "Invalid font size command: "
1106 << command_id;
1107 return E_FAIL;
1108 }
1109
1110 // Forward the command back to IEFrame with group set to
1111 // CGID_ExplorerBarDoc. This is probably needed to update the menu state to
1112 // indicate that the font size was set. This currently fails with error
1113 // 0x80040104.
1114 // TODO(iyengar)
1115 // Do some investigation into why this Exec call fails.
1116 IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
1117 return S_OK;
1118}
1119
[email protected]2f2afba2010-04-01 01:53:191120HRESULT ChromeActiveDocument::OnEncodingChange(const GUID* cmd_group_guid,
1121 DWORD command_id,
1122 DWORD cmd_exec_opt,
1123 VARIANT* in_args,
1124 VARIANT* out_args) {
1125 const struct EncodingMapData {
1126 DWORD ie_encoding_id;
1127 const char* chrome_encoding_name;
1128 } kEncodingTestDatas[] = {
1129#define DEFINE_ENCODING_MAP(encoding_name, id, chrome_name) \
1130 { encoding_name, chrome_name },
1131 INTERNAL_IE_ENCODINGMENU_IDS(DEFINE_ENCODING_MAP)
1132#undef DEFINE_ENCODING_MAP
1133 };
1134
1135 if (!automation_client_.get()) {
1136 NOTREACHED() << "Invalid automtion client";
1137 return E_FAIL;
1138 }
1139
[email protected]e9b35282010-06-02 16:43:191140 // Using ARRAYSIZE_UNSAFE in here is because we define the struct
1141 // EncodingMapData inside function.
[email protected]2f2afba2010-04-01 01:53:191142 const char* chrome_encoding_name = NULL;
1143 for (int i = 0; i < ARRAYSIZE_UNSAFE(kEncodingTestDatas); ++i) {
1144 const struct EncodingMapData* encoding_data = &kEncodingTestDatas[i];
1145 if (command_id == encoding_data->ie_encoding_id) {
1146 chrome_encoding_name = encoding_data->chrome_encoding_name;
1147 break;
1148 }
1149 }
1150 // Return E_FAIL when encountering invalid encoding id.
1151 if (!chrome_encoding_name)
1152 return E_FAIL;
1153
1154 TabProxy* tab = GetTabProxy();
1155 if (!tab) {
1156 NOTREACHED() << "Can not get TabProxy";
1157 return E_FAIL;
1158 }
1159
1160 if (chrome_encoding_name)
1161 tab->OverrideEncoding(chrome_encoding_name);
1162
1163 // Like we did on SetPageFontSize, we may forward the command back to IEFrame
1164 // to update the menu state to indicate that which encoding was set.
1165 // TODO(iyengar)
1166 // Do some investigation into why this Exec call fails.
1167 IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
1168 return S_OK;
1169}
1170
[email protected]f9cc4c452009-10-13 14:56:381171void ChromeActiveDocument::OnGoToHistoryEntryOffset(int tab_handle,
1172 int offset) {
[email protected]a1800e82009-11-19 00:53:231173 DLOG(INFO) << __FUNCTION__ << " - offset:" << offset;
1174
[email protected]f9cc4c452009-10-13 14:56:381175 ScopedComPtr<IBrowserService> browser_service;
[email protected]a1800e82009-11-19 00:53:231176 ScopedComPtr<ITravelLog> travel_log;
1177 GetBrowserServiceAndTravelLog(browser_service.Receive(),
1178 travel_log.Receive());
1179
1180 if (browser_service && travel_log)
1181 travel_log->Travel(browser_service, offset);
1182}
1183
1184HRESULT ChromeActiveDocument::GetBrowserServiceAndTravelLog(
1185 IBrowserService** browser_service, ITravelLog** travel_log) {
1186 DCHECK(browser_service || travel_log);
1187 ScopedComPtr<IBrowserService> browser_service_local;
1188 HRESULT hr = DoQueryService(SID_SShellBrowser, m_spClientSite,
1189 browser_service_local.Receive());
1190 if (!browser_service_local) {
1191 NOTREACHED() << "DoQueryService for IBrowserService failed: " << hr;
1192 return hr;
[email protected]f9cc4c452009-10-13 14:56:381193 }
[email protected]a1800e82009-11-19 00:53:231194
1195 if (travel_log) {
1196 hr = browser_service_local->GetTravelLog(travel_log);
[email protected]bc2ff5192010-06-01 22:05:451197 DLOG_IF(INFO, !travel_log) << "browser_service->GetTravelLog failed: " <<
1198 hr;
[email protected]a1800e82009-11-19 00:53:231199 }
1200
1201 if (browser_service)
1202 *browser_service = browser_service_local.Detach();
1203
1204 return hr;
[email protected]f9cc4c452009-10-13 14:56:381205}
[email protected]a15d4a42010-02-17 08:21:351206
1207LRESULT ChromeActiveDocument::OnForward(WORD notify_code, WORD id,
1208 HWND control_window,
1209 BOOL& bHandled) {
1210 ScopedComPtr<IWebBrowser2> web_browser2;
1211 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
1212 DCHECK(web_browser2);
1213
[email protected]bc2ff5192010-06-01 22:05:451214 if (web_browser2)
[email protected]a15d4a42010-02-17 08:21:351215 web_browser2->GoForward();
[email protected]a15d4a42010-02-17 08:21:351216 return 0;
1217}
1218
1219LRESULT ChromeActiveDocument::OnBack(WORD notify_code, WORD id,
1220 HWND control_window,
1221 BOOL& bHandled) {
1222 ScopedComPtr<IWebBrowser2> web_browser2;
1223 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
1224 DCHECK(web_browser2);
1225
[email protected]bc2ff5192010-06-01 22:05:451226 if (web_browser2)
[email protected]a15d4a42010-02-17 08:21:351227 web_browser2->GoBack();
[email protected]a15d4a42010-02-17 08:21:351228 return 0;
1229}
1230
[email protected]80b5a8d2010-03-19 16:50:431231LRESULT ChromeActiveDocument::OnFirePrivacyChange(UINT message, WPARAM wparam,
1232 LPARAM lparam,
1233 BOOL& handled) {
1234 if (!m_spClientSite)
1235 return 0;
1236
1237 ScopedComPtr<IWebBrowser2> web_browser2;
1238 DoQueryService(SID_SWebBrowserApp, m_spClientSite,
1239 web_browser2.Receive());
1240 if (!web_browser2) {
1241 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
1242 return 0;
1243 }
1244
1245 ScopedComPtr<IShellBrowser> shell_browser;
1246 DoQueryService(SID_STopLevelBrowser, web_browser2,
1247 shell_browser.Receive());
1248 DCHECK(shell_browser.get() != NULL);
1249 ScopedComPtr<ITridentService2> trident_services;
1250 trident_services.QueryFrom(shell_browser);
[email protected]bc2ff5192010-06-01 22:05:451251 if (trident_services)
[email protected]80b5a8d2010-03-19 16:50:431252 trident_services->FirePrivacyImpactedStateChange(wparam);
[email protected]bc2ff5192010-06-01 22:05:451253 else
[email protected]80b5a8d2010-03-19 16:50:431254 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
[email protected]80b5a8d2010-03-19 16:50:431255 return 0;
1256}
[email protected]00aebd72010-07-16 14:44:321257
[email protected]723e5442010-07-21 17:36:051258LRESULT ChromeActiveDocument::OnShowWindow(UINT message, WPARAM wparam,
1259 LPARAM lparam,
1260 BOOL& handled) { // NO_LINT
1261 if (wparam)
1262 SetFocus();
1263 return 0;
1264}
1265
1266LRESULT ChromeActiveDocument::OnSetFocus(UINT message, WPARAM wparam,
1267 LPARAM lparam,
1268 BOOL& handled) { // NO_LINT
[email protected]e7ad9322010-08-02 20:41:291269 if (!ignore_setfocus_)
1270 GiveFocusToChrome(false);
[email protected]723e5442010-07-21 17:36:051271 return 0;
1272}
[email protected]7f860dd82010-08-09 23:18:051273
1274void ChromeActiveDocument::SetWindowDimensions() {
1275 ScopedComPtr<IWebBrowser2> web_browser2;
1276 DoQueryService(SID_SWebBrowserApp, m_spClientSite,
1277 web_browser2.Receive());
1278 if (!web_browser2)
1279 return;
1280 DLOG(INFO) << "this:" << this;
1281 DLOG(INFO) << "dimensions: width:" << dimensions_.width()
1282 << "height:" << dimensions_.height();
1283 if (!dimensions_.IsEmpty()) {
1284 web_browser2->put_Width(dimensions_.width());
1285 web_browser2->put_Height(dimensions_.height());
1286 web_browser2->put_Left(dimensions_.x());
1287 web_browser2->put_Top(dimensions_.y());
1288 web_browser2->put_MenuBar(VARIANT_FALSE);
1289 web_browser2->put_ToolBar(VARIANT_FALSE);
1290
1291 dimensions_.set_height(0);
1292 dimensions_.set_width(0);
1293 }
1294}