blob: a4833b2e6eb36acca99a934986717906a0606d52 [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]f7817822009-09-24 05:11:5846static const wchar_t kUseChromeNetworking[] = L"UseChromeNetworking";
[email protected]552fb6012010-02-03 17:24:2947static const wchar_t kHandleTopLevelRequests[] = L"HandleTopLevelRequests";
[email protected]f7817822009-09-24 05:11:5848
[email protected]a1800e82009-11-19 00:53:2349DEFINE_GUID(CGID_DocHostCmdPriv, 0x000214D4L, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0,
50 0x46);
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
99 bool chrome_network = GetConfigBool(false, kUseChromeNetworking);
100 bool top_level_requests = GetConfigBool(true, kHandleTopLevelRequests);
101 automation_client_->set_use_chrome_network(chrome_network);
102 automation_client_->set_handle_top_level_requests(top_level_requests);
103
104 find_dialog_.Init(automation_client_.get());
105
[email protected]6fd1a7a2010-07-26 22:09:46106 OLECMDF flags = static_cast<OLECMDF>(OLECMDF_ENABLED | OLECMDF_SUPPORTED);
107
108 null_group_commands_map_[OLECMDID_PRINT] = flags;
109 null_group_commands_map_[OLECMDID_FIND] = flags;
110 null_group_commands_map_[OLECMDID_CUT] = flags;
111 null_group_commands_map_[OLECMDID_COPY] = flags;
112 null_group_commands_map_[OLECMDID_PASTE] = flags;
113 null_group_commands_map_[OLECMDID_SELECTALL] = flags;
114 null_group_commands_map_[OLECMDID_SAVEAS] = flags;
115
116 mshtml_group_commands_map_[IDM_BASELINEFONT1] = flags;
117 mshtml_group_commands_map_[IDM_BASELINEFONT2] = flags;
118 mshtml_group_commands_map_[IDM_BASELINEFONT3] = flags;
119 mshtml_group_commands_map_[IDM_BASELINEFONT4] = flags;
120 mshtml_group_commands_map_[IDM_BASELINEFONT5] = flags;
[email protected]a15d4a42010-02-17 08:21:35121
[email protected]3c6f8e12010-03-24 21:58:21122 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase);
[email protected]a15d4a42010-02-17 08:21:35123 accelerator_table_ =
[email protected]3c6f8e12010-03-24 21:58:21124 LoadAccelerators(this_module,
[email protected]a15d4a42010-02-17 08:21:35125 MAKEINTRESOURCE(IDR_CHROME_FRAME_IE_FULL_TAB));
126 DCHECK(accelerator_table_ != NULL);
[email protected]f7817822009-09-24 05:11:58127 return S_OK;
128}
129
130ChromeActiveDocument::~ChromeActiveDocument() {
131 DLOG(INFO) << __FUNCTION__;
[email protected]bc2ff5192010-06-01 22:05:45132 if (find_dialog_.IsWindow())
[email protected]f7817822009-09-24 05:11:58133 find_dialog_.DestroyWindow();
[email protected]3eb07da2010-02-01 19:48:36134 // ChromeFramePlugin
[email protected]97965e12010-04-09 00:51:10135 BaseActiveX::Uninitialize();
[email protected]04e3f352010-05-10 13:48:24136
137 TRACE_EVENT_END("chromeframe.createactivedocument", this, "");
[email protected]f7817822009-09-24 05:11:58138}
139
140// Override DoVerb
141STDMETHODIMP ChromeActiveDocument::DoVerb(LONG verb,
142 LPMSG msg,
143 IOleClientSite* active_site,
144 LONG index,
145 HWND parent_window,
146 LPCRECT pos) {
147 // IE will try and in-place activate us in some cases. This happens when
148 // the user opens a new IE window with a URL that has us as the DocObject.
149 // Here we refuse to be activated in-place and we will force IE to UIActivate
150 // us.
[email protected]bc2ff5192010-06-01 22:05:45151 if (OLEIVERB_INPLACEACTIVATE == verb)
[email protected]f7817822009-09-24 05:11:58152 return E_NOTIMPL;
[email protected]f7817822009-09-24 05:11:58153 // Check if we should activate as a docobject or not
154 // (client supports IOleDocumentSite)
155 if (doc_site_) {
156 switch (verb) {
[email protected]1bb5f892009-10-06 01:44:57157 case OLEIVERB_SHOW: {
158 ScopedComPtr<IDocHostUIHandler> doc_host_handler;
159 doc_host_handler.QueryFrom(doc_site_);
[email protected]bc2ff5192010-06-01 22:05:45160 if (doc_host_handler.get())
[email protected]1bb5f892009-10-06 01:44:57161 doc_host_handler->ShowUI(DOCHOSTUITYPE_BROWSE, this, this, NULL, NULL);
[email protected]1bb5f892009-10-06 01:44:57162 }
[email protected]f7817822009-09-24 05:11:58163 case OLEIVERB_OPEN:
164 case OLEIVERB_UIACTIVATE:
[email protected]bc2ff5192010-06-01 22:05:45165 if (!m_bUIActive)
[email protected]f7817822009-09-24 05:11:58166 return doc_site_->ActivateMe(NULL);
[email protected]f7817822009-09-24 05:11:58167 break;
168 }
169 }
170 return IOleObjectImpl<ChromeActiveDocument>::DoVerb(verb,
171 msg,
172 active_site,
173 index,
174 parent_window,
175 pos);
176}
177
[email protected]f7817822009-09-24 05:11:58178// Override IOleInPlaceActiveObjectImpl::OnDocWindowActivate
179STDMETHODIMP ChromeActiveDocument::OnDocWindowActivate(BOOL activate) {
180 DLOG(INFO) << __FUNCTION__;
181 return S_OK;
182}
183
184STDMETHODIMP ChromeActiveDocument::TranslateAccelerator(MSG* msg) {
185 DLOG(INFO) << __FUNCTION__;
186 if (msg == NULL)
187 return E_POINTER;
188
189 if (msg->message == WM_KEYDOWN && msg->wParam == VK_TAB) {
190 HWND focus = ::GetFocus();
191 if (focus != m_hWnd && !::IsChild(m_hWnd, focus)) {
192 // The call to SetFocus triggers a WM_SETFOCUS that makes the base class
193 // set focus to the correct element in Chrome.
194 ::SetFocus(m_hWnd);
195 return S_OK;
196 }
197 }
198
199 return S_FALSE;
200}
201// Override IPersistStorageImpl::IsDirty
202STDMETHODIMP ChromeActiveDocument::IsDirty() {
203 DLOG(INFO) << __FUNCTION__;
204 return S_FALSE;
205}
206
[email protected]b95f550f2009-11-19 05:35:22207void ChromeActiveDocument::OnAutomationServerReady() {
[email protected]97965e12010-04-09 00:51:10208 BaseActiveX::OnAutomationServerReady();
[email protected]1fd45692010-04-19 21:01:18209 BaseActiveX::GiveFocusToChrome(true);
[email protected]b95f550f2009-11-19 05:35:22210}
211
[email protected]f7817822009-09-24 05:11:58212STDMETHODIMP ChromeActiveDocument::Load(BOOL fully_avalable,
213 IMoniker* moniker_name,
214 LPBC bind_context,
215 DWORD mode) {
[email protected]bc2ff5192010-06-01 22:05:45216 if (NULL == moniker_name)
[email protected]f7817822009-09-24 05:11:58217 return E_INVALIDARG;
[email protected]a1800e82009-11-19 00:53:23218
219 ScopedComPtr<IOleClientSite> client_site;
220 if (bind_context) {
221 ScopedComPtr<IUnknown> site;
222 bind_context->GetObjectParam(SZ_HTML_CLIENTSITE_OBJECTPARAM,
223 site.Receive());
224 if (site)
225 client_site.QueryFrom(site);
226 }
227
228 if (client_site) {
229 SetClientSite(client_site);
[email protected]b1c55638612010-03-08 16:26:11230 DoQueryService(IID_INewWindowManager, client_site,
231 popup_manager_.Receive());
[email protected]051236f2010-03-12 22:06:14232
233 // See if mshtml parsed the html header for us. If so, we need to
234 // clear the browser service flag that we use to indicate that this
235 // browser instance is navigating to a CF document.
236 ScopedComPtr<IBrowserService> browser_service;
237 DoQueryService(SID_SShellBrowser, client_site, browser_service.Receive());
238 if (browser_service) {
239 bool flagged = CheckForCFNavigation(browser_service, true);
240 DLOG_IF(INFO, flagged) << "Cleared flagged browser service";
241 }
[email protected]a1800e82009-11-19 00:53:23242 }
243
[email protected]051236f2010-03-12 22:06:14244 NavigationManager* mgr = NavigationManager::GetThreadInstance();
[email protected]21b4e6d2010-07-14 18:19:59245 DLOG_IF(ERROR, !mgr) << "Couldn't get instance of NavigationManager";
[email protected]7e3544b2010-01-22 00:02:34246
[email protected]29c32f902010-04-20 23:27:29247 std::wstring url;
248
[email protected]77d7aee2010-05-14 20:31:55249 ScopedComPtr<BindContextInfo> info;
250 BindContextInfo::FromBindContext(bind_context, info.Receive());
[email protected]29c32f902010-04-20 23:27:29251 DCHECK(info);
252 if (info && !info->url().empty()) {
253 url = info->url();
254 } else {
255 // If the original URL contains an anchor, then the URL queried
256 // from the moniker does not contain the anchor. To workaround
257 // this we retrieve the URL from our BHO.
258 url = GetActualUrlFromMoniker(moniker_name, bind_context,
259 mgr ? mgr->url(): std::wstring());
260 }
[email protected]051236f2010-03-12 22:06:14261
[email protected]c4e45b32010-07-28 21:15:15262 ChromeFrameUrl cf_url;
263 if (!cf_url.Parse(url)) {
[email protected]37346bc2009-09-25 22:54:33264 DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
[email protected]f7817822009-09-24 05:11:58265 return E_INVALIDARG;
266 }
267
[email protected]bbfa9a12010-08-10 14:09:37268 std::string referrer(mgr ? mgr->referrer() : EmptyString());
269
[email protected]aaf124502010-07-17 04:02:58270 // With CTransaction patch we have more robust way to grab the referrer for
271 // each top-level-switch-to-CF request by peeking at our sniffing data
272 // object that lives inside the bind context.
273 if (g_patch_helper.state() == PatchHelper::PATCH_PROTOCOL && info) {
274 scoped_refptr<ProtData> prot_data = info->get_prot_data();
275 if (prot_data)
276 referrer = prot_data->referrer();
277 }
278
[email protected]c4e45b32010-07-28 21:15:15279 if (!LaunchUrl(cf_url, referrer)) {
[email protected]d266ce82010-08-13 21:33:40280 DLOG(ERROR) << __FUNCTION__ << " Failed to launch url:" << url;
[email protected]37346bc2009-09-25 22:54:33281 return E_INVALIDARG;
[email protected]f7817822009-09-24 05:11:58282 }
283
[email protected]c4e45b32010-07-28 21:15:15284 if (!cf_url.is_chrome_protocol() && !cf_url.attach_to_external_tab())
[email protected]d266ce82010-08-13 21:33:40285 url_fetcher_->SetInfoForUrl(url.c_str(), moniker_name, bind_context);
[email protected]f7817822009-09-24 05:11:58286
[email protected]333590002010-03-05 18:49:21287 THREAD_SAFE_UMA_HISTOGRAM_CUSTOM_COUNTS("ChromeFrame.FullTabLaunchType",
[email protected]c4e45b32010-07-28 21:15:15288 cf_url.is_chrome_protocol(),
289 0, 1, 2);
[email protected]f7817822009-09-24 05:11:58290 return S_OK;
291}
292
293STDMETHODIMP ChromeActiveDocument::Save(IMoniker* moniker_name,
294 LPBC bind_context,
295 BOOL remember) {
296 return E_NOTIMPL;
297}
298
299STDMETHODIMP ChromeActiveDocument::SaveCompleted(IMoniker* moniker_name,
300 LPBC bind_context) {
301 return E_NOTIMPL;
302}
303
304STDMETHODIMP ChromeActiveDocument::GetCurMoniker(IMoniker** moniker_name) {
305 return E_NOTIMPL;
306}
307
308STDMETHODIMP ChromeActiveDocument::GetClassID(CLSID* class_id) {
[email protected]bc2ff5192010-06-01 22:05:45309 if (NULL == class_id)
[email protected]f7817822009-09-24 05:11:58310 return E_POINTER;
[email protected]f7817822009-09-24 05:11:58311 *class_id = GetObjectCLSID();
312 return S_OK;
313}
314
315STDMETHODIMP ChromeActiveDocument::QueryStatus(const GUID* cmd_group_guid,
316 ULONG number_of_commands,
317 OLECMD commands[],
318 OLECMDTEXT* command_text) {
319 DLOG(INFO) << __FUNCTION__;
[email protected]b509ec82010-04-13 16:53:06320
[email protected]6fd1a7a2010-07-26 22:09:46321 CommandStatusMap* command_map = NULL;
[email protected]b509ec82010-04-13 16:53:06322
[email protected]6fd1a7a2010-07-26 22:09:46323 if (cmd_group_guid) {
324 if (IsEqualGUID(*cmd_group_guid, GUID_NULL)) {
325 command_map = &null_group_commands_map_;
326 } else if (IsEqualGUID(*cmd_group_guid, CGID_MSHTML)) {
327 command_map = &mshtml_group_commands_map_;
328 } else if (IsEqualGUID(*cmd_group_guid, CGID_Explorer)) {
329 command_map = &explorer_group_commands_map_;
330 } else if (IsEqualGUID(*cmd_group_guid, CGID_ShellDocView)) {
331 command_map = &shdoc_view_group_commands_map_;
332 }
333 } else {
334 command_map = &null_group_commands_map_;
335 }
336
337 if (!command_map) {
[email protected]b509ec82010-04-13 16:53:06338 DLOG(INFO) << "unsupported command group: "
339 << GuidToString(*cmd_group_guid);
340 return OLECMDERR_E_NOTSUPPORTED;
341 }
342
[email protected]f7817822009-09-24 05:11:58343 for (ULONG command_index = 0; command_index < number_of_commands;
344 command_index++) {
345 DLOG(INFO) << "Command id = " << commands[command_index].cmdID;
[email protected]6fd1a7a2010-07-26 22:09:46346 CommandStatusMap::iterator index =
347 command_map->find(commands[command_index].cmdID);
348 if (index != command_map->end())
349 commands[command_index].cmdf = index->second;
[email protected]f7817822009-09-24 05:11:58350 }
351 return S_OK;
352}
353
354STDMETHODIMP ChromeActiveDocument::Exec(const GUID* cmd_group_guid,
355 DWORD command_id,
356 DWORD cmd_exec_opt,
357 VARIANT* in_args,
358 VARIANT* out_args) {
359 DLOG(INFO) << __FUNCTION__ << " Cmd id =" << command_id;
360 // Bail out if we have been uninitialized.
361 if (automation_client_.get() && automation_client_->tab()) {
362 return ProcessExecCommand(cmd_group_guid, command_id, cmd_exec_opt,
363 in_args, out_args);
[email protected]bbfa9a12010-08-10 14:09:37364 } else if (command_id == OLECMDID_REFRESH && cmd_group_guid == NULL) {
365 // If the automation server has crashed and the user is refreshing the
366 // page, let OnRefreshPage attempt to recover.
367 OnRefreshPage(cmd_group_guid, command_id, cmd_exec_opt, in_args, out_args);
[email protected]f7817822009-09-24 05:11:58368 }
[email protected]bbfa9a12010-08-10 14:09:37369
[email protected]e3200932009-10-09 21:33:03370 return OLECMDERR_E_NOTSUPPORTED;
[email protected]f7817822009-09-24 05:11:58371}
372
[email protected]a1800e82009-11-19 00:53:23373STDMETHODIMP ChromeActiveDocument::LoadHistory(IStream* stream,
374 IBindCtx* bind_context) {
375 // Read notes in ChromeActiveDocument::SaveHistory
376 DCHECK(stream);
377 LARGE_INTEGER offset = {0};
378 ULARGE_INTEGER cur_pos = {0};
379 STATSTG statstg = {0};
380
381 stream->Seek(offset, STREAM_SEEK_CUR, &cur_pos);
382 stream->Stat(&statstg, STATFLAG_NONAME);
383
384 DWORD url_size = statstg.cbSize.LowPart - cur_pos.LowPart;
385 ScopedBstr url_bstr;
386 DWORD bytes_read = 0;
387 stream->Read(url_bstr.AllocateBytes(url_size), url_size, &bytes_read);
388 std::wstring url(url_bstr);
389
[email protected]c4e45b32010-07-28 21:15:15390 ChromeFrameUrl cf_url;
391 if (!cf_url.Parse(url)) {
[email protected]a1800e82009-11-19 00:53:23392 DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
393 return E_INVALIDARG;
394 }
395
[email protected]c5cbf4e2010-07-15 21:48:25396 const std::string& referrer = EmptyString();
[email protected]c4e45b32010-07-28 21:15:15397 if (!LaunchUrl(cf_url, referrer)) {
[email protected]a1800e82009-11-19 00:53:23398 NOTREACHED() << __FUNCTION__ << " Failed to launch url:" << url;
399 return E_INVALIDARG;
400 }
401 return S_OK;
402}
403
404STDMETHODIMP ChromeActiveDocument::SaveHistory(IStream* stream) {
405 // TODO(sanjeevr): We need to fetch the entire list of navigation entries
406 // from Chrome and persist it in the stream. And in LoadHistory we need to
407 // pass this list back to Chrome which will recreate the list. This will allow
408 // Back-Forward navigation to anchors to work correctly when we navigate to a
409 // page outside of ChromeFrame and then come back.
410 if (!stream) {
411 NOTREACHED();
412 return E_INVALIDARG;
413 }
414
415 LARGE_INTEGER offset = {0};
416 ULARGE_INTEGER new_pos = {0};
417 DWORD written = 0;
418 std::wstring url = UTF8ToWide(navigation_info_.url.spec());
419 return stream->Write(url.c_str(), (url.length() + 1) * sizeof(wchar_t),
420 &written);
421}
422
423STDMETHODIMP ChromeActiveDocument::SetPositionCookie(DWORD position_cookie) {
[email protected]77700832010-04-27 00:06:03424 if (automation_client_.get()) {
425 int index = static_cast<int>(position_cookie);
426 navigation_info_.navigation_index = index;
427 automation_client_->NavigateToIndex(index);
428 } else {
429 DLOG(WARNING) << "Invalid automation client instance";
430 }
[email protected]a1800e82009-11-19 00:53:23431 return S_OK;
432}
433
434STDMETHODIMP ChromeActiveDocument::GetPositionCookie(DWORD* position_cookie) {
435 if (!position_cookie)
436 return E_INVALIDARG;
437
438 *position_cookie = navigation_info_.navigation_index;
439 return S_OK;
440}
441
[email protected]f7817822009-09-24 05:11:58442STDMETHODIMP ChromeActiveDocument::GetUrlForEvents(BSTR* url) {
[email protected]bc2ff5192010-06-01 22:05:45443 if (NULL == url)
[email protected]f7817822009-09-24 05:11:58444 return E_POINTER;
[email protected]f7817822009-09-24 05:11:58445 *url = ::SysAllocString(url_);
446 return S_OK;
447}
448
[email protected]a1800e82009-11-19 00:53:23449STDMETHODIMP ChromeActiveDocument::GetAddressBarUrl(BSTR* url) {
450 return GetUrlForEvents(url);
451}
452
[email protected]80b5a8d2010-03-19 16:50:43453STDMETHODIMP ChromeActiveDocument::Reset() {
454 next_privacy_record_ = privacy_info_.privacy_records.begin();
455 return S_OK;
456}
457
[email protected]00aebd72010-07-16 14:44:32458STDMETHODIMP ChromeActiveDocument::GetSize(DWORD* size) {
[email protected]80b5a8d2010-03-19 16:50:43459 if (!size)
460 return E_POINTER;
461
462 *size = privacy_info_.privacy_records.size();
463 return S_OK;
464}
465
466STDMETHODIMP ChromeActiveDocument::GetPrivacyImpacted(BOOL* privacy_impacted) {
467 if (!privacy_impacted)
468 return E_POINTER;
469
470 *privacy_impacted = privacy_info_.privacy_impacted;
471 return S_OK;
472}
473
474STDMETHODIMP ChromeActiveDocument::Next(BSTR* url, BSTR* policy,
[email protected]00aebd72010-07-16 14:44:32475 LONG* reserved, DWORD* flags) {
[email protected]80b5a8d2010-03-19 16:50:43476 if (!url || !policy || !flags)
477 return E_POINTER;
478
479 if (next_privacy_record_ == privacy_info_.privacy_records.end())
480 return HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS);
481
482 *url = SysAllocString(next_privacy_record_->first.c_str());
483 *policy = SysAllocString(next_privacy_record_->second.policy_ref.c_str());
484 *flags = next_privacy_record_->second.flags;
485
486 next_privacy_record_++;
487 return S_OK;
488}
489
[email protected]3eafbfb92010-08-02 16:54:22490HRESULT ChromeActiveDocument::GetInPlaceFrame(
491 IOleInPlaceFrame** in_place_frame) {
492 DCHECK(in_place_frame);
493 if (in_place_frame_) {
494 *in_place_frame = in_place_frame_.get();
495 (*in_place_frame)->AddRef();
496 return S_OK;
497 } else {
498 return S_FALSE;
499 }
500}
501
[email protected]f7817822009-09-24 05:11:58502HRESULT ChromeActiveDocument::IOleObject_SetClientSite(
503 IOleClientSite* client_site) {
504 if (client_site == NULL) {
505 ChromeActiveDocument* cached_document = g_active_doc_cache.Get();
506 if (cached_document) {
507 DCHECK(this == cached_document);
508 g_active_doc_cache.Set(NULL);
509 cached_document->Release();
510 }
[email protected]dda7d9c2009-11-11 23:01:47511
512 ScopedComPtr<IDocHostUIHandler> doc_host_handler;
[email protected]bc2ff5192010-06-01 22:05:45513 if (doc_site_)
[email protected]77059f72010-02-28 06:16:00514 doc_host_handler.QueryFrom(doc_site_);
[email protected]dda7d9c2009-11-11 23:01:47515
[email protected]bc2ff5192010-06-01 22:05:45516 if (doc_host_handler.get())
[email protected]dda7d9c2009-11-11 23:01:47517 doc_host_handler->HideUI();
[email protected]dda7d9c2009-11-11 23:01:47518
519 doc_site_.Release();
[email protected]f7817822009-09-24 05:11:58520 }
[email protected]a1800e82009-11-19 00:53:23521
[email protected]bc2ff5192010-06-01 22:05:45522 if (client_site != m_spClientSite)
[email protected]97965e12010-04-09 00:51:10523 return BaseActiveX::IOleObject_SetClientSite(client_site);
[email protected]a1800e82009-11-19 00:53:23524
525 return S_OK;
[email protected]f7817822009-09-24 05:11:58526}
527
[email protected]f7817822009-09-24 05:11:58528HRESULT ChromeActiveDocument::ActiveXDocActivate(LONG verb) {
529 HRESULT hr = S_OK;
530 m_bNegotiatedWnd = TRUE;
531 if (!m_bInPlaceActive) {
532 hr = m_spInPlaceSite->CanInPlaceActivate();
[email protected]bc2ff5192010-06-01 22:05:45533 if (FAILED(hr))
[email protected]f7817822009-09-24 05:11:58534 return hr;
[email protected]f7817822009-09-24 05:11:58535 m_spInPlaceSite->OnInPlaceActivate();
536 }
537 m_bInPlaceActive = TRUE;
538 // get location in the parent window,
539 // as well as some information about the parent
540 ScopedComPtr<IOleInPlaceUIWindow> in_place_ui_window;
541 frame_info_.cb = sizeof(OLEINPLACEFRAMEINFO);
542 HWND parent_window = NULL;
543 if (m_spInPlaceSite->GetWindow(&parent_window) == S_OK) {
544 in_place_frame_.Release();
545 RECT position_rect = {0};
546 RECT clip_rect = {0};
547 m_spInPlaceSite->GetWindowContext(in_place_frame_.Receive(),
548 in_place_ui_window.Receive(),
549 &position_rect,
550 &clip_rect,
551 &frame_info_);
552 if (!m_bWndLess) {
553 if (IsWindow()) {
554 ::ShowWindow(m_hWnd, SW_SHOW);
555 SetFocus();
556 } else {
[email protected]c1d19582010-03-03 01:57:13557 m_hWnd = Create(parent_window, position_rect, 0, 0, WS_EX_CLIENTEDGE);
[email protected]dd4beb522010-07-13 18:18:14558 if (!IsWindow()) {
559 // This might happen if the automation server couldn't be
560 // instantiated. If so, a NOTREACHED() will have already been hit.
561 DLOG(ERROR) << "Failed to create Ax window";
562 return AtlHresultFromLastError();
563 }
[email protected]f7817822009-09-24 05:11:58564 }
[email protected]7f860dd82010-08-09 23:18:05565 SetWindowDimensions();
[email protected]f7817822009-09-24 05:11:58566 }
567 SetObjectRects(&position_rect, &clip_rect);
568 }
569
570 ScopedComPtr<IOleInPlaceActiveObject> in_place_active_object(this);
571
572 // Gone active by now, take care of UIACTIVATE
573 if (DoesVerbUIActivate(verb)) {
574 if (!m_bUIActive) {
575 m_bUIActive = TRUE;
576 hr = m_spInPlaceSite->OnUIActivate();
[email protected]bc2ff5192010-06-01 22:05:45577 if (FAILED(hr))
[email protected]f7817822009-09-24 05:11:58578 return hr;
[email protected]f7817822009-09-24 05:11:58579 // set ourselves up in the host
580 if (in_place_active_object) {
[email protected]bc2ff5192010-06-01 22:05:45581 if (in_place_frame_)
[email protected]f7817822009-09-24 05:11:58582 in_place_frame_->SetActiveObject(in_place_active_object, NULL);
[email protected]bc2ff5192010-06-01 22:05:45583 if (in_place_ui_window)
[email protected]f7817822009-09-24 05:11:58584 in_place_ui_window->SetActiveObject(in_place_active_object, NULL);
[email protected]f7817822009-09-24 05:11:58585 }
586 }
587 }
588 m_spClientSite->ShowObject();
589 return S_OK;
590}
591
592void ChromeActiveDocument::OnNavigationStateChanged(int tab_handle, int flags,
593 const IPC::NavigationInfo& nav_info) {
594 // TODO(joshia): handle INVALIDATE_TAB,INVALIDATE_LOAD etc.
595 DLOG(INFO) << __FUNCTION__ << std::endl << " Flags: " << flags
596 << "Url: " << nav_info.url <<
597 ", Title: " << nav_info.title <<
598 ", Type: " << nav_info.navigation_type << ", Relative Offset: " <<
[email protected]62bb18dc12009-11-25 01:34:08599 nav_info.relative_offset << ", Index: " << nav_info.navigation_index;
[email protected]f7817822009-09-24 05:11:58600
601 UpdateNavigationState(nav_info);
602}
603
604void ChromeActiveDocument::OnUpdateTargetUrl(int tab_handle,
605 const std::wstring& new_target_url) {
[email protected]bc2ff5192010-06-01 22:05:45606 if (in_place_frame_)
[email protected]f7817822009-09-24 05:11:58607 in_place_frame_->SetStatusText(new_target_url.c_str());
[email protected]f7817822009-09-24 05:11:58608}
609
610bool IsFindAccelerator(const MSG& msg) {
611 // TODO(robertshield): This may not stand up to localization. Fix if this
612 // is the case.
613 return msg.message == WM_KEYDOWN && msg.wParam == 'F' &&
614 win_util::IsCtrlPressed() &&
615 !(win_util::IsAltPressed() || win_util::IsShiftPressed());
616}
617
618void ChromeActiveDocument::OnAcceleratorPressed(int tab_handle,
619 const MSG& accel_message) {
[email protected]a15d4a42010-02-17 08:21:35620 if (::TranslateAccelerator(m_hWnd, accelerator_table_,
621 const_cast<MSG*>(&accel_message)))
622 return;
623
[email protected]f7817822009-09-24 05:11:58624 bool handled_accel = false;
625 if (in_place_frame_ != NULL) {
626 handled_accel = (S_OK == in_place_frame_->TranslateAcceleratorW(
627 const_cast<MSG*>(&accel_message), 0));
628 }
629
630 if (!handled_accel) {
631 if (IsFindAccelerator(accel_message)) {
632 // Handle the showing of the find dialog explicitly.
633 OnFindInPage();
[email protected]2b8fd322009-10-02 00:00:59634 } else {
[email protected]97965e12010-04-09 00:51:10635 BaseActiveX::OnAcceleratorPressed(tab_handle, accel_message);
[email protected]f7817822009-09-24 05:11:58636 }
637 } else {
638 DLOG(INFO) << "IE handled accel key " << accel_message.wParam;
639 }
640}
641
642void ChromeActiveDocument::OnTabbedOut(int tab_handle, bool reverse) {
643 DLOG(INFO) << __FUNCTION__;
644 if (in_place_frame_) {
645 MSG msg = { NULL, WM_KEYDOWN, VK_TAB };
646 in_place_frame_->TranslateAcceleratorW(&msg, 0);
647 }
648}
649
650void ChromeActiveDocument::OnDidNavigate(int tab_handle,
651 const IPC::NavigationInfo& nav_info) {
652 DLOG(INFO) << __FUNCTION__ << std::endl << "Url: " << nav_info.url <<
653 ", Title: " << nav_info.title <<
654 ", Type: " << nav_info.navigation_type << ", Relative Offset: " <<
655 nav_info.relative_offset << ", Index: " << nav_info.navigation_index;
656
[email protected]897b26272010-06-11 02:23:44657 CrashMetricsReporter::GetInstance()->IncrementMetric(
658 CrashMetricsReporter::CHROME_FRAME_NAVIGATION_COUNT);
659
[email protected]f7817822009-09-24 05:11:58660 // This could be NULL if the active document instance is being destroyed.
661 if (!m_spInPlaceSite) {
662 DLOG(INFO) << __FUNCTION__ << "m_spInPlaceSite is NULL. Returning";
663 return;
664 }
665
666 UpdateNavigationState(nav_info);
667}
668
[email protected]e16dd1672010-06-07 21:40:29669void ChromeActiveDocument::OnCloseTab(int tab_handle) {
670 ScopedComPtr<IWebBrowser2> web_browser2;
671 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
672 if (web_browser2)
673 web_browser2->Quit();
674}
675
[email protected]f7817822009-09-24 05:11:58676void ChromeActiveDocument::UpdateNavigationState(
677 const IPC::NavigationInfo& new_navigation_info) {
[email protected]a1800e82009-11-19 00:53:23678 HRESULT hr = S_OK;
[email protected]f7817822009-09-24 05:11:58679 bool is_title_changed = (navigation_info_.title != new_navigation_info.title);
[email protected]f7817822009-09-24 05:11:58680 bool is_ssl_state_changed =
681 (navigation_info_.security_style != new_navigation_info.security_style) ||
[email protected]b4e75c12010-05-18 18:28:48682 (navigation_info_.displayed_insecure_content !=
683 new_navigation_info.displayed_insecure_content) ||
684 (navigation_info_.ran_insecure_content !=
685 new_navigation_info.ran_insecure_content);
[email protected]f7817822009-09-24 05:11:58686
[email protected]f7817822009-09-24 05:11:58687 if (is_ssl_state_changed) {
688 int lock_status = SECURELOCK_SET_UNSECURE;
[email protected]a1800e82009-11-19 00:53:23689 switch (new_navigation_info.security_style) {
[email protected]f7817822009-09-24 05:11:58690 case SECURITY_STYLE_AUTHENTICATION_BROKEN:
691 lock_status = SECURELOCK_SET_SECUREUNKNOWNBIT;
692 break;
693 case SECURITY_STYLE_AUTHENTICATED:
[email protected]b4e75c12010-05-18 18:28:48694 lock_status = new_navigation_info.displayed_insecure_content ?
[email protected]f7817822009-09-24 05:11:58695 SECURELOCK_SET_MIXED : SECURELOCK_SET_SECUREUNKNOWNBIT;
696 break;
697 default:
698 break;
699 }
700
701 ScopedVariant secure_lock_status(lock_status);
702 IEExec(&CGID_ShellDocView, INTERNAL_CMDID_SET_SSL_LOCK,
703 OLECMDEXECOPT_DODEFAULT, secure_lock_status.AsInput(), NULL);
704 }
705
[email protected]a1800e82009-11-19 00:53:23706 // Ideally all navigations should come to Chrome Frame so that we can call
707 // BeforeNavigate2 on installed BHOs and give them a chance to cancel the
708 // navigation. However, in practice what happens is as below:
709 // The very first navigation that happens in CF happens via a Load or a
710 // LoadHistory call. In this case, IE already has the correct information for
711 // its travel log as well address bar. For other internal navigations (navs
712 // that only happen within Chrome such as anchor navigations) we need to
713 // update IE's internal state after the fact. In the case of internal
714 // navigations, we notify the BHOs but ignore the should_cancel flag.
[email protected]6f526082010-01-28 19:36:58715
716 // Another case where we need to issue BeforeNavigate2 calls is as below:-
717 // We get notified after the fact, when navigations are initiated within
718 // Chrome via window.open calls. These navigations are handled by creating
719 // an external tab container within chrome and then connecting to it from IE.
720 // We still want to update the address bar/history, etc, to ensure that
721 // the special URL used by Chrome to indicate this is updated correctly.
[email protected]d266ce82010-08-13 21:33:40722 ChromeFrameUrl cf_url;
723 bool is_attach_external_tab_url = cf_url.Parse(std::wstring(url_)) &&
724 cf_url.attach_to_external_tab();
[email protected]6f526082010-01-28 19:36:58725 bool is_internal_navigation = ((new_navigation_info.navigation_index > 0) &&
[email protected]a1800e82009-11-19 00:53:23726 (new_navigation_info.navigation_index !=
[email protected]d266ce82010-08-13 21:33:40727 navigation_info_.navigation_index)) || is_attach_external_tab_url;
[email protected]f7817822009-09-24 05:11:58728
[email protected]bc2ff5192010-06-01 22:05:45729 if (new_navigation_info.url.is_valid())
[email protected]38939de2010-05-13 02:32:06730 url_.Allocate(UTF8ToWide(new_navigation_info.url.spec()).c_str());
[email protected]38939de2010-05-13 02:32:06731
[email protected]a1800e82009-11-19 00:53:23732 if (is_internal_navigation) {
733 ScopedComPtr<IDocObjectService> doc_object_svc;
[email protected]f7817822009-09-24 05:11:58734 ScopedComPtr<IWebBrowserEventsService> web_browser_events_svc;
[email protected]ae33d672010-03-04 21:58:06735
[email protected]5ae94d22010-07-21 19:55:36736 buggy_bho::BuggyBhoTls bad_bho_tls;
737 if (GetConfigBool(true, kEnableBuggyBhoIntercept)) {
738 ScopedComPtr<IWebBrowser2> wb2;
739 DoQueryService(SID_SWebBrowserApp, m_spClientSite, wb2.Receive());
740 if (wb2) {
741 buggy_bho::BuggyBhoTls::PatchBuggyBHOs(wb2);
742 }
743 }
744
[email protected]f7817822009-09-24 05:11:58745 DoQueryService(__uuidof(web_browser_events_svc), m_spClientSite,
746 web_browser_events_svc.Receive());
[email protected]ae33d672010-03-04 21:58:06747
748 if (!web_browser_events_svc.get()) {
749 DoQueryService(SID_SShellBrowser, m_spClientSite,
750 doc_object_svc.Receive());
751 }
752
[email protected]62bb18dc12009-11-25 01:34:08753 // web_browser_events_svc can be NULL on IE6.
[email protected]f7817822009-09-24 05:11:58754 if (web_browser_events_svc) {
[email protected]f7817822009-09-24 05:11:58755 VARIANT_BOOL should_cancel = VARIANT_FALSE;
756 web_browser_events_svc->FireBeforeNavigate2Event(&should_cancel);
[email protected]a1800e82009-11-19 00:53:23757 }
758
759 // We need to tell IE that we support navigation so that IE will query us
760 // for IPersistHistory and call GetPositionCookie to save our navigation
761 // index.
762 ScopedVariant html_window(static_cast<IUnknown*>(
763 static_cast<IHTMLWindow2*>(this)));
764 IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0,
765 html_window.AsInput(), NULL);
766
767 // We pass the HLNF_INTERNALJUMP flag to INTERNAL_CMDID_FINALIZE_TRAVEL_LOG
768 // since we want to make IE treat all internal navigations within this page
769 // (including anchor navigations and subframe navigations) as anchor
770 // navigations. This will ensure that IE calls GetPositionCookie
771 // to save the current position cookie in the travel log and then call
772 // SetPositionCookie when the user hits Back/Forward to come back here.
773 ScopedVariant internal_navigation(HLNF_INTERNALJUMP);
774 IEExec(&CGID_Explorer, INTERNAL_CMDID_FINALIZE_TRAVEL_LOG, 0,
775 internal_navigation.AsInput(), NULL);
776
777 // We no longer need to lie to IE. If we lie persistently to IE, then
778 // IE reuses us for new navigations.
779 IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0, NULL, NULL);
780
781 if (doc_object_svc) {
782 // Now call the FireNavigateCompleteEvent which makes IE update the text
783 // in the address-bar.
784 doc_object_svc->FireNavigateComplete2(this, 0);
[email protected]5ae94d22010-07-21 19:55:36785 doc_object_svc->FireDocumentComplete(this, 0);
[email protected]a1800e82009-11-19 00:53:23786 } else if (web_browser_events_svc) {
[email protected]f7817822009-09-24 05:11:58787 web_browser_events_svc->FireNavigateComplete2Event();
[email protected]5ae94d22010-07-21 19:55:36788 web_browser_events_svc->FireDocumentCompleteEvent();
[email protected]f7817822009-09-24 05:11:58789 }
790 }
[email protected]37346bc2009-09-25 22:54:33791
[email protected]a1800e82009-11-19 00:53:23792 if (is_title_changed) {
793 ScopedVariant title(new_navigation_info.title.c_str());
794 IEExec(NULL, OLECMDID_SETTITLE, OLECMDEXECOPT_DONTPROMPTUSER,
795 title.AsInput(), NULL);
796 }
797
798 // It is important that we only update the navigation_info_ after we have
799 // finalized the travel log. This is because IE will ask for information
800 // such as navigation index when the travel log is finalized and we need
801 // supply the old index and not the new one.
802 navigation_info_ = new_navigation_info;
[email protected]37346bc2009-09-25 22:54:33803 // Update the IE zone here. Ideally we would like to do it when the active
804 // document is activated. However that does not work at times as the frame we
805 // get there is not the actual frame which handles the command.
806 IEExec(&CGID_Explorer, SBCMDID_MIXEDZONE, 0, NULL, NULL);
[email protected]f7817822009-09-24 05:11:58807}
808
809void ChromeActiveDocument::OnFindInPage() {
810 TabProxy* tab = GetTabProxy();
811 if (tab) {
[email protected]bc2ff5192010-06-01 22:05:45812 if (!find_dialog_.IsWindow())
[email protected]f7817822009-09-24 05:11:58813 find_dialog_.Create(m_hWnd);
[email protected]f7817822009-09-24 05:11:58814
815 find_dialog_.ShowWindow(SW_SHOW);
816 }
817}
818
819void ChromeActiveDocument::OnViewSource() {
820 DCHECK(navigation_info_.url.is_valid());
[email protected]76e7da22010-06-18 22:44:49821 HostNavigate(GURL(chrome::kViewSourceScheme + std::string(":") +
822 navigation_info_.url.spec()), GURL(), NEW_WINDOW);
[email protected]f7817822009-09-24 05:11:58823}
824
[email protected]37346bc2009-09-25 22:54:33825void ChromeActiveDocument::OnDetermineSecurityZone(const GUID* cmd_group_guid,
826 DWORD command_id,
827 DWORD cmd_exec_opt,
828 VARIANT* in_args,
829 VARIANT* out_args) {
[email protected]7c712c92010-03-24 17:29:22830 // Always return URLZONE_INTERNET since that is the Chrome's behaviour.
831 // Correct step is to use MapUrlToZone().
[email protected]37346bc2009-09-25 22:54:33832 if (out_args != NULL) {
833 out_args->vt = VT_UI4;
834 out_args->ulVal = URLZONE_INTERNET;
835 }
836}
837
[email protected]80b5a8d2010-03-19 16:50:43838void ChromeActiveDocument::OnDisplayPrivacyInfo() {
[email protected]bbfa9a12010-08-10 14:09:37839 privacy_info_ = url_fetcher_->privacy_info();
[email protected]80b5a8d2010-03-19 16:50:43840 Reset();
841 DoPrivacyDlg(m_hWnd, url_, this, TRUE);
842}
843
[email protected]7f860dd82010-08-09 23:18:05844void ChromeActiveDocument::OnGetZoomRange(const GUID* cmd_group_guid,
845 DWORD command_id,
846 DWORD cmd_exec_opt,
847 VARIANT* in_args,
848 VARIANT* out_args) {
849 if (out_args != NULL) {
850 out_args->vt = VT_I4;
851 out_args->lVal = 0;
852 }
853}
854
855void ChromeActiveDocument::OnSetZoomRange(const GUID* cmd_group_guid,
856 DWORD command_id,
857 DWORD cmd_exec_opt,
858 VARIANT* in_args,
859 VARIANT* out_args) {
860 const int kZoomIn = 125;
861 const int kZoomOut = 75;
862
863 if (in_args && V_VT(in_args) == VT_I4 && IsValid()) {
864 if (in_args->lVal == kZoomIn) {
865 automation_client_->SetZoomLevel(PageZoom::ZOOM_IN);
866 } else if (in_args->lVal == kZoomOut) {
867 automation_client_->SetZoomLevel(PageZoom::ZOOM_OUT);
868 } else {
869 DLOG(WARNING) << "Unsupported zoom level:" << in_args->lVal;
870 }
871 }
872}
873
[email protected]b36a9f92009-10-19 17:34:57874void ChromeActiveDocument::OnOpenURL(int tab_handle,
875 const GURL& url_to_open,
876 const GURL& referrer,
[email protected]f7817822009-09-24 05:11:58877 int open_disposition) {
878 // If the disposition indicates that we should be opening the URL in the
879 // current tab, then we can reuse the ChromeFrameAutomationClient instance
880 // maintained by the current ChromeActiveDocument instance. We cache this
881 // instance so that it can be used by the new ChromeActiveDocument instance
882 // which may be instantiated for handling the new URL.
883 if (open_disposition == CURRENT_TAB) {
884 // Grab a reference to ensure that the document remains valid.
885 AddRef();
886 g_active_doc_cache.Set(this);
887 }
888
[email protected]97965e12010-04-09 00:51:10889 BaseActiveX::OnOpenURL(tab_handle, url_to_open, referrer, open_disposition);
[email protected]f7817822009-09-24 05:11:58890}
891
[email protected]b1c55638612010-03-08 16:26:11892void ChromeActiveDocument::OnAttachExternalTab(int tab_handle,
893 const IPC::AttachExternalTabParams& params) {
[email protected]77700832010-04-27 00:06:03894 if (!automation_client_.get()) {
895 DLOG(WARNING) << "Invalid automation client instance";
896 return;
897 }
[email protected]b1c55638612010-03-08 16:26:11898 DWORD flags = 0;
899 if (params.user_gesture)
900 flags = NWMF_USERREQUESTED;
[email protected]355309e2010-03-15 17:01:34901 else if (popup_allowed_)
902 flags = NWMF_USERALLOWED;
[email protected]b1c55638612010-03-08 16:26:11903
904 HRESULT hr = S_OK;
905 if (popup_manager_) {
[email protected]355309e2010-03-15 17:01:34906 LPCWSTR popup_wnd_url = UTF8ToWide(params.url.spec()).c_str();
907 hr = popup_manager_->EvaluateNewWindow(popup_wnd_url, NULL, url_,
908 NULL, FALSE, flags, 0);
[email protected]b1c55638612010-03-08 16:26:11909 }
910 // Allow popup
911 if (hr == S_OK) {
[email protected]97965e12010-04-09 00:51:10912 BaseActiveX::OnAttachExternalTab(tab_handle, params);
[email protected]b1c55638612010-03-08 16:26:11913 return;
914 }
915
916 automation_client_->BlockExternalTab(params.cookie);
917}
918
[email protected]f7817822009-09-24 05:11:58919bool ChromeActiveDocument::PreProcessContextMenu(HMENU menu) {
920 ScopedComPtr<IBrowserService> browser_service;
921 ScopedComPtr<ITravelLog> travel_log;
[email protected]a1800e82009-11-19 00:53:23922 GetBrowserServiceAndTravelLog(browser_service.Receive(),
923 travel_log.Receive());
924 if (!browser_service || !travel_log)
[email protected]f7817822009-09-24 05:11:58925 return true;
926
[email protected]bc2ff5192010-06-01 22:05:45927 EnableMenuItem(menu, IDS_CONTENT_CONTEXT_BACK, MF_BYCOMMAND |
928 (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_BACK, NULL)) ?
929 MF_ENABLED : MF_DISABLED));
930 EnableMenuItem(menu, IDS_CONTENT_CONTEXT_FORWARD, MF_BYCOMMAND |
931 (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_FORE, NULL)) ?
932 MF_ENABLED : MF_DISABLED));
[email protected]f7817822009-09-24 05:11:58933
934 // Call base class (adds 'About' item)
[email protected]97965e12010-04-09 00:51:10935 return BaseActiveX::PreProcessContextMenu(menu);
[email protected]f7817822009-09-24 05:11:58936}
937
[email protected]35f13ab2009-12-16 23:59:17938bool ChromeActiveDocument::HandleContextMenuCommand(UINT cmd,
939 const IPC::ContextMenuParams& params) {
[email protected]f7817822009-09-24 05:11:58940 ScopedComPtr<IWebBrowser2> web_browser2;
941 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
942
[email protected]074283a2010-05-28 23:47:59943 if (cmd == IDC_BACK)
[email protected]b731a142010-05-14 00:03:03944 web_browser2->GoBack();
[email protected]074283a2010-05-28 23:47:59945 else if (cmd == IDC_FORWARD)
[email protected]b731a142010-05-14 00:03:03946 web_browser2->GoForward();
[email protected]074283a2010-05-28 23:47:59947 else if (cmd == IDC_RELOAD)
[email protected]b731a142010-05-14 00:03:03948 web_browser2->Refresh();
[email protected]074283a2010-05-28 23:47:59949 else
[email protected]b731a142010-05-14 00:03:03950 return BaseActiveX::HandleContextMenuCommand(cmd, params);
[email protected]f7817822009-09-24 05:11:58951
952 return true;
953}
954
955HRESULT ChromeActiveDocument::IEExec(const GUID* cmd_group_guid,
956 DWORD command_id, DWORD cmd_exec_opt,
957 VARIANT* in_args, VARIANT* out_args) {
958 HRESULT hr = E_FAIL;
[email protected]37346bc2009-09-25 22:54:33959
[email protected]f7817822009-09-24 05:11:58960 ScopedComPtr<IOleCommandTarget> frame_cmd_target;
[email protected]37346bc2009-09-25 22:54:33961
962 ScopedComPtr<IOleInPlaceSite> in_place_site(m_spInPlaceSite);
[email protected]bc2ff5192010-06-01 22:05:45963 if (!in_place_site.get() && m_spClientSite != NULL)
[email protected]37346bc2009-09-25 22:54:33964 in_place_site.QueryFrom(m_spClientSite);
[email protected]37346bc2009-09-25 22:54:33965
966 if (in_place_site)
967 hr = frame_cmd_target.QueryFrom(in_place_site);
[email protected]f7817822009-09-24 05:11:58968
[email protected]bc2ff5192010-06-01 22:05:45969 if (frame_cmd_target) {
[email protected]f7817822009-09-24 05:11:58970 hr = frame_cmd_target->Exec(cmd_group_guid, command_id, cmd_exec_opt,
971 in_args, out_args);
[email protected]bc2ff5192010-06-01 22:05:45972 }
[email protected]f7817822009-09-24 05:11:58973
974 return hr;
975}
[email protected]37346bc2009-09-25 22:54:33976
[email protected]c4e45b32010-07-28 21:15:15977bool ChromeActiveDocument::LaunchUrl(const ChromeFrameUrl& cf_url,
978 const std::string& referrer) {
[email protected]d266ce82010-08-13 21:33:40979 DCHECK(!cf_url.gurl().is_empty());
[email protected]77700832010-04-27 00:06:03980
[email protected]242ceb032010-08-26 17:06:36981 if (!automation_client_.get()) {
982 // http://code.google.com/p/chromium/issues/detail?id=52894
983 // Still not sure how this happens.
984 DLOG(ERROR) << "No automation client!";
985 if (!Initialize()) {
986 NOTREACHED() << "...and failed to start a new one >:(";
987 return false;
988 }
989 }
990
[email protected]d266ce82010-08-13 21:33:40991 url_.Allocate(UTF8ToWide(cf_url.gurl().spec()).c_str());
[email protected]c4e45b32010-07-28 21:15:15992 if (cf_url.attach_to_external_tab()) {
993 dimensions_ = cf_url.dimensions();
994 automation_client_->AttachExternalTab(cf_url.cookie());
[email protected]7f860dd82010-08-09 23:18:05995 SetWindowDimensions();
[email protected]d266ce82010-08-13 21:33:40996 } else if (!automation_client_->InitiateNavigation(cf_url.gurl().spec(),
[email protected]6f8f31d2010-07-30 08:17:15997 referrer,
998 is_privileged_)) {
999 DLOG(ERROR) << "Invalid URL: " << url_;
1000 Error(L"Invalid URL");
1001 url_.Reset();
1002 return false;
[email protected]37346bc2009-09-25 22:54:331003 }
1004
[email protected]3eb07da2010-02-01 19:48:361005 if (is_automation_client_reused_)
1006 return true;
[email protected]37346bc2009-09-25 22:54:331007
[email protected]bbfa9a12010-08-10 14:09:371008 automation_client_->SetUrlFetcher(url_fetcher_.get());
[email protected]e150e822010-06-03 23:10:551009 return InitializeAutomation(GetHostProcessName(false), L"", IsIEInPrivate(),
[email protected]d266ce82010-08-13 21:33:401010 false, cf_url.gurl(), GURL(referrer));
[email protected]37346bc2009-09-25 22:54:331011}
[email protected]1bb5f892009-10-06 01:44:571012
[email protected]355309e2010-03-15 17:01:341013
1014HRESULT ChromeActiveDocument::OnRefreshPage(const GUID* cmd_group_guid,
1015 DWORD command_id, DWORD cmd_exec_opt, VARIANT* in_args, VARIANT* out_args) {
[email protected]bbfa9a12010-08-10 14:09:371016 DLOG(INFO) << __FUNCTION__;
[email protected]355309e2010-03-15 17:01:341017 popup_allowed_ = false;
1018 if (in_args->vt == VT_I4 &&
1019 in_args->lVal & OLECMDIDF_REFRESH_PAGEACTION_POPUPWINDOW) {
1020 popup_allowed_ = true;
1021
1022 // Ask the yellow security band to change the text and icon and to remain
1023 // visible.
1024 IEExec(&CGID_DocHostCommandHandler, OLECMDID_PAGEACTIONBLOCKED,
[email protected]bbfa9a12010-08-10 14:09:371025 0x80000000 | OLECMDIDF_WINDOWSTATE_USERVISIBLE_VALID, NULL, NULL);
[email protected]355309e2010-03-15 17:01:341026 }
1027
1028 TabProxy* tab_proxy = GetTabProxy();
[email protected]bbfa9a12010-08-10 14:09:371029 if (tab_proxy) {
[email protected]355309e2010-03-15 17:01:341030 tab_proxy->ReloadAsync();
[email protected]bbfa9a12010-08-10 14:09:371031 } else {
1032 DLOG(ERROR) << "No automation proxy";
[email protected]242ceb032010-08-26 17:06:361033 DCHECK(automation_client_.get() != NULL) << "how did it get freed?";
[email protected]bbfa9a12010-08-10 14:09:371034 // The current url request manager (url_fetcher_) has been switched to
1035 // a stopping state so we need to reset it and get a new one for the new
1036 // automation server.
1037 ResetUrlRequestManager();
1038 url_fetcher_->set_frame_busting(false);
1039 // And now launch the current URL again. This starts a new server process.
1040 DCHECK(navigation_info_.url.is_valid());
1041 ChromeFrameUrl cf_url;
1042 cf_url.Parse(UTF8ToWide(navigation_info_.url.spec()));
1043 LaunchUrl(cf_url, navigation_info_.referrer.spec());
1044 }
[email protected]355309e2010-03-15 17:01:341045
1046 return S_OK;
1047}
1048
[email protected]1bb5f892009-10-06 01:44:571049HRESULT ChromeActiveDocument::SetPageFontSize(const GUID* cmd_group_guid,
1050 DWORD command_id,
1051 DWORD cmd_exec_opt,
1052 VARIANT* in_args,
1053 VARIANT* out_args) {
1054 if (!automation_client_.get()) {
[email protected]77700832010-04-27 00:06:031055 NOTREACHED() << "Invalid automation client";
[email protected]1bb5f892009-10-06 01:44:571056 return E_FAIL;
1057 }
1058
1059 switch (command_id) {
1060 case IDM_BASELINEFONT1:
1061 automation_client_->SetPageFontSize(SMALLEST_FONT);
1062 break;
1063
1064 case IDM_BASELINEFONT2:
1065 automation_client_->SetPageFontSize(SMALL_FONT);
1066 break;
1067
1068 case IDM_BASELINEFONT3:
1069 automation_client_->SetPageFontSize(MEDIUM_FONT);
1070 break;
1071
1072 case IDM_BASELINEFONT4:
1073 automation_client_->SetPageFontSize(LARGE_FONT);
1074 break;
1075
1076 case IDM_BASELINEFONT5:
1077 automation_client_->SetPageFontSize(LARGEST_FONT);
1078 break;
1079
1080 default:
1081 NOTREACHED() << "Invalid font size command: "
1082 << command_id;
1083 return E_FAIL;
1084 }
1085
1086 // Forward the command back to IEFrame with group set to
1087 // CGID_ExplorerBarDoc. This is probably needed to update the menu state to
1088 // indicate that the font size was set. This currently fails with error
1089 // 0x80040104.
1090 // TODO(iyengar)
1091 // Do some investigation into why this Exec call fails.
1092 IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
1093 return S_OK;
1094}
1095
[email protected]2f2afba2010-04-01 01:53:191096HRESULT ChromeActiveDocument::OnEncodingChange(const GUID* cmd_group_guid,
1097 DWORD command_id,
1098 DWORD cmd_exec_opt,
1099 VARIANT* in_args,
1100 VARIANT* out_args) {
1101 const struct EncodingMapData {
1102 DWORD ie_encoding_id;
1103 const char* chrome_encoding_name;
1104 } kEncodingTestDatas[] = {
1105#define DEFINE_ENCODING_MAP(encoding_name, id, chrome_name) \
1106 { encoding_name, chrome_name },
1107 INTERNAL_IE_ENCODINGMENU_IDS(DEFINE_ENCODING_MAP)
1108#undef DEFINE_ENCODING_MAP
1109 };
1110
1111 if (!automation_client_.get()) {
1112 NOTREACHED() << "Invalid automtion client";
1113 return E_FAIL;
1114 }
1115
[email protected]e9b35282010-06-02 16:43:191116 // Using ARRAYSIZE_UNSAFE in here is because we define the struct
1117 // EncodingMapData inside function.
[email protected]2f2afba2010-04-01 01:53:191118 const char* chrome_encoding_name = NULL;
1119 for (int i = 0; i < ARRAYSIZE_UNSAFE(kEncodingTestDatas); ++i) {
1120 const struct EncodingMapData* encoding_data = &kEncodingTestDatas[i];
1121 if (command_id == encoding_data->ie_encoding_id) {
1122 chrome_encoding_name = encoding_data->chrome_encoding_name;
1123 break;
1124 }
1125 }
1126 // Return E_FAIL when encountering invalid encoding id.
1127 if (!chrome_encoding_name)
1128 return E_FAIL;
1129
1130 TabProxy* tab = GetTabProxy();
1131 if (!tab) {
1132 NOTREACHED() << "Can not get TabProxy";
1133 return E_FAIL;
1134 }
1135
1136 if (chrome_encoding_name)
1137 tab->OverrideEncoding(chrome_encoding_name);
1138
1139 // Like we did on SetPageFontSize, we may forward the command back to IEFrame
1140 // to update the menu state to indicate that which encoding was set.
1141 // TODO(iyengar)
1142 // Do some investigation into why this Exec call fails.
1143 IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
1144 return S_OK;
1145}
1146
[email protected]f9cc4c452009-10-13 14:56:381147void ChromeActiveDocument::OnGoToHistoryEntryOffset(int tab_handle,
1148 int offset) {
[email protected]a1800e82009-11-19 00:53:231149 DLOG(INFO) << __FUNCTION__ << " - offset:" << offset;
1150
[email protected]f9cc4c452009-10-13 14:56:381151 ScopedComPtr<IBrowserService> browser_service;
[email protected]a1800e82009-11-19 00:53:231152 ScopedComPtr<ITravelLog> travel_log;
1153 GetBrowserServiceAndTravelLog(browser_service.Receive(),
1154 travel_log.Receive());
1155
1156 if (browser_service && travel_log)
1157 travel_log->Travel(browser_service, offset);
1158}
1159
1160HRESULT ChromeActiveDocument::GetBrowserServiceAndTravelLog(
1161 IBrowserService** browser_service, ITravelLog** travel_log) {
1162 DCHECK(browser_service || travel_log);
1163 ScopedComPtr<IBrowserService> browser_service_local;
1164 HRESULT hr = DoQueryService(SID_SShellBrowser, m_spClientSite,
1165 browser_service_local.Receive());
1166 if (!browser_service_local) {
1167 NOTREACHED() << "DoQueryService for IBrowserService failed: " << hr;
1168 return hr;
[email protected]f9cc4c452009-10-13 14:56:381169 }
[email protected]a1800e82009-11-19 00:53:231170
1171 if (travel_log) {
1172 hr = browser_service_local->GetTravelLog(travel_log);
[email protected]bc2ff5192010-06-01 22:05:451173 DLOG_IF(INFO, !travel_log) << "browser_service->GetTravelLog failed: " <<
1174 hr;
[email protected]a1800e82009-11-19 00:53:231175 }
1176
1177 if (browser_service)
1178 *browser_service = browser_service_local.Detach();
1179
1180 return hr;
[email protected]f9cc4c452009-10-13 14:56:381181}
[email protected]a15d4a42010-02-17 08:21:351182
1183LRESULT ChromeActiveDocument::OnForward(WORD notify_code, WORD id,
1184 HWND control_window,
1185 BOOL& bHandled) {
1186 ScopedComPtr<IWebBrowser2> web_browser2;
1187 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
1188 DCHECK(web_browser2);
1189
[email protected]bc2ff5192010-06-01 22:05:451190 if (web_browser2)
[email protected]a15d4a42010-02-17 08:21:351191 web_browser2->GoForward();
[email protected]a15d4a42010-02-17 08:21:351192 return 0;
1193}
1194
1195LRESULT ChromeActiveDocument::OnBack(WORD notify_code, WORD id,
1196 HWND control_window,
1197 BOOL& bHandled) {
1198 ScopedComPtr<IWebBrowser2> web_browser2;
1199 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
1200 DCHECK(web_browser2);
1201
[email protected]bc2ff5192010-06-01 22:05:451202 if (web_browser2)
[email protected]a15d4a42010-02-17 08:21:351203 web_browser2->GoBack();
[email protected]a15d4a42010-02-17 08:21:351204 return 0;
1205}
1206
[email protected]80b5a8d2010-03-19 16:50:431207LRESULT ChromeActiveDocument::OnFirePrivacyChange(UINT message, WPARAM wparam,
1208 LPARAM lparam,
1209 BOOL& handled) {
1210 if (!m_spClientSite)
1211 return 0;
1212
1213 ScopedComPtr<IWebBrowser2> web_browser2;
1214 DoQueryService(SID_SWebBrowserApp, m_spClientSite,
1215 web_browser2.Receive());
1216 if (!web_browser2) {
1217 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
1218 return 0;
1219 }
1220
1221 ScopedComPtr<IShellBrowser> shell_browser;
1222 DoQueryService(SID_STopLevelBrowser, web_browser2,
1223 shell_browser.Receive());
1224 DCHECK(shell_browser.get() != NULL);
1225 ScopedComPtr<ITridentService2> trident_services;
1226 trident_services.QueryFrom(shell_browser);
[email protected]bc2ff5192010-06-01 22:05:451227 if (trident_services)
[email protected]80b5a8d2010-03-19 16:50:431228 trident_services->FirePrivacyImpactedStateChange(wparam);
[email protected]bc2ff5192010-06-01 22:05:451229 else
[email protected]80b5a8d2010-03-19 16:50:431230 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
[email protected]80b5a8d2010-03-19 16:50:431231 return 0;
1232}
[email protected]00aebd72010-07-16 14:44:321233
[email protected]723e5442010-07-21 17:36:051234LRESULT ChromeActiveDocument::OnShowWindow(UINT message, WPARAM wparam,
1235 LPARAM lparam,
1236 BOOL& handled) { // NO_LINT
1237 if (wparam)
1238 SetFocus();
1239 return 0;
1240}
1241
1242LRESULT ChromeActiveDocument::OnSetFocus(UINT message, WPARAM wparam,
1243 LPARAM lparam,
1244 BOOL& handled) { // NO_LINT
[email protected]e7ad9322010-08-02 20:41:291245 if (!ignore_setfocus_)
1246 GiveFocusToChrome(false);
[email protected]723e5442010-07-21 17:36:051247 return 0;
1248}
[email protected]7f860dd82010-08-09 23:18:051249
1250void ChromeActiveDocument::SetWindowDimensions() {
1251 ScopedComPtr<IWebBrowser2> web_browser2;
1252 DoQueryService(SID_SWebBrowserApp, m_spClientSite,
1253 web_browser2.Receive());
1254 if (!web_browser2)
1255 return;
1256 DLOG(INFO) << "this:" << this;
1257 DLOG(INFO) << "dimensions: width:" << dimensions_.width()
1258 << "height:" << dimensions_.height();
1259 if (!dimensions_.IsEmpty()) {
1260 web_browser2->put_Width(dimensions_.width());
1261 web_browser2->put_Height(dimensions_.height());
1262 web_browser2->put_Left(dimensions_.x());
1263 web_browser2->put_Top(dimensions_.y());
1264 web_browser2->put_MenuBar(VARIANT_FALSE);
1265 web_browser2->put_ToolBar(VARIANT_FALSE);
1266
1267 dimensions_.set_height(0);
1268 dimensions_.set_width(0);
1269 }
1270}