blob: 1fac95b84356dc879e7d2506ca7b95145144bf30 [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>
14#include <shobjidl.h>
15#include <tlogstg.h>
16#include <urlmon.h>
17#include <wininet.h>
18
19#include "base/command_line.h"
20#include "base/file_util.h"
[email protected]f7817822009-09-24 05:11:5821#include "base/logging.h"
22#include "base/path_service.h"
23#include "base/process_util.h"
24#include "base/registry.h"
25#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]f7817822009-09-24 05:11:5831
32#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"
37#include "chrome/test/automation/browser_proxy.h"
38#include "chrome/test/automation/tab_proxy.h"
[email protected]b36a9f92009-10-19 17:34:5739#include "chrome_frame/bho.h"
[email protected]29c32f902010-04-20 23:27:2940#include "chrome_frame/bind_context_info.h"
[email protected]5ae94d22010-07-21 19:55:3641#include "chrome_frame/buggy_bho_handling.h"
[email protected]9607f6a82010-06-12 17:24:4142#include "chrome_frame/crash_reporting/crash_metrics.h"
[email protected]f7817822009-09-24 05:11:5843#include "chrome_frame/utils.h"
44
45const wchar_t kChromeAttachExternalTabPrefix[] = L"attach_external_tab";
46
47static const wchar_t kUseChromeNetworking[] = L"UseChromeNetworking";
[email protected]552fb6012010-02-03 17:24:2948static const wchar_t kHandleTopLevelRequests[] = L"HandleTopLevelRequests";
[email protected]f7817822009-09-24 05:11:5849
[email protected]a1800e82009-11-19 00:53:2350DEFINE_GUID(CGID_DocHostCmdPriv, 0x000214D4L, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0,
51 0x46);
52
[email protected]f7817822009-09-24 05:11:5853base::ThreadLocalPointer<ChromeActiveDocument> g_active_doc_cache;
54
55bool g_first_launch_by_process_ = true;
56
[email protected]2f2afba2010-04-01 01:53:1957const DWORD kIEEncodingIdArray[] = {
58#define DEFINE_ENCODING_ID_ARRAY(encoding_name, id, chrome_name) encoding_name,
59 INTERNAL_IE_ENCODINGMENU_IDS(DEFINE_ENCODING_ID_ARRAY)
60#undef DEFINE_ENCODING_ID_ARRAY
61 0 // The Last data must be 0 to indicate the end of the encoding id array.
62};
63
[email protected]f7817822009-09-24 05:11:5864ChromeActiveDocument::ChromeActiveDocument()
65 : first_navigation_(true),
[email protected]a15d4a42010-02-17 08:21:3566 is_automation_client_reused_(false),
[email protected]355309e2010-03-15 17:01:3467 popup_allowed_(false),
[email protected]c4e45b32010-07-28 21:15:1568 accelerator_table_(NULL) {
[email protected]04e3f352010-05-10 13:48:2469 TRACE_EVENT_BEGIN("chromeframe.createactivedocument", this, "");
70
[email protected]3eb07da2010-02-01 19:48:3671 url_fetcher_.set_frame_busting(false);
[email protected]a1800e82009-11-19 00:53:2372 memset(&navigation_info_, 0, sizeof(navigation_info_));
[email protected]f7817822009-09-24 05:11:5873}
74
75HRESULT ChromeActiveDocument::FinalConstruct() {
76 // If we have a cached ChromeActiveDocument instance in TLS, then grab
77 // ownership of the cached document's automation client. This is an
78 // optimization to get Chrome active documents to load faster.
79 ChromeActiveDocument* cached_document = g_active_doc_cache.Get();
[email protected]58174a72010-05-05 21:30:3080 if (cached_document && cached_document->IsValid()) {
[email protected]f7817822009-09-24 05:11:5881 DCHECK(automation_client_.get() == NULL);
[email protected]3148c0ae2010-03-11 18:06:0082 automation_client_.swap(cached_document->automation_client_);
[email protected]f7817822009-09-24 05:11:5883 DLOG(INFO) << "Reusing automation client instance from "
84 << cached_document;
85 DCHECK(automation_client_.get() != NULL);
[email protected]3eb07da2010-02-01 19:48:3686 automation_client_->Reinitialize(this, &url_fetcher_);
[email protected]f7817822009-09-24 05:11:5887 is_automation_client_reused_ = true;
88 } 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]c4e45b32010-07-28 21:15:15127
128 HRESULT hr = security_manager_.CreateInstance(CLSID_InternetSecurityManager);
129 if (FAILED(hr)) {
130 NOTREACHED() << __FUNCTION__
131 << " Failed to create InternetSecurityManager. Error: 0x%x"
132 << hr;
133 }
134
[email protected]f7817822009-09-24 05:11:58135 return S_OK;
136}
137
138ChromeActiveDocument::~ChromeActiveDocument() {
139 DLOG(INFO) << __FUNCTION__;
[email protected]bc2ff5192010-06-01 22:05:45140 if (find_dialog_.IsWindow())
[email protected]f7817822009-09-24 05:11:58141 find_dialog_.DestroyWindow();
[email protected]3eb07da2010-02-01 19:48:36142 // ChromeFramePlugin
[email protected]97965e12010-04-09 00:51:10143 BaseActiveX::Uninitialize();
[email protected]04e3f352010-05-10 13:48:24144
145 TRACE_EVENT_END("chromeframe.createactivedocument", this, "");
[email protected]f7817822009-09-24 05:11:58146}
147
148// Override DoVerb
149STDMETHODIMP ChromeActiveDocument::DoVerb(LONG verb,
150 LPMSG msg,
151 IOleClientSite* active_site,
152 LONG index,
153 HWND parent_window,
154 LPCRECT pos) {
155 // IE will try and in-place activate us in some cases. This happens when
156 // the user opens a new IE window with a URL that has us as the DocObject.
157 // Here we refuse to be activated in-place and we will force IE to UIActivate
158 // us.
[email protected]bc2ff5192010-06-01 22:05:45159 if (OLEIVERB_INPLACEACTIVATE == verb)
[email protected]f7817822009-09-24 05:11:58160 return E_NOTIMPL;
[email protected]f7817822009-09-24 05:11:58161 // Check if we should activate as a docobject or not
162 // (client supports IOleDocumentSite)
163 if (doc_site_) {
164 switch (verb) {
[email protected]1bb5f892009-10-06 01:44:57165 case OLEIVERB_SHOW: {
166 ScopedComPtr<IDocHostUIHandler> doc_host_handler;
167 doc_host_handler.QueryFrom(doc_site_);
[email protected]bc2ff5192010-06-01 22:05:45168 if (doc_host_handler.get())
[email protected]1bb5f892009-10-06 01:44:57169 doc_host_handler->ShowUI(DOCHOSTUITYPE_BROWSE, this, this, NULL, NULL);
[email protected]1bb5f892009-10-06 01:44:57170 }
[email protected]f7817822009-09-24 05:11:58171 case OLEIVERB_OPEN:
172 case OLEIVERB_UIACTIVATE:
[email protected]bc2ff5192010-06-01 22:05:45173 if (!m_bUIActive)
[email protected]f7817822009-09-24 05:11:58174 return doc_site_->ActivateMe(NULL);
[email protected]f7817822009-09-24 05:11:58175 break;
176 }
177 }
178 return IOleObjectImpl<ChromeActiveDocument>::DoVerb(verb,
179 msg,
180 active_site,
181 index,
182 parent_window,
183 pos);
184}
185
[email protected]f7817822009-09-24 05:11:58186// Override IOleInPlaceActiveObjectImpl::OnDocWindowActivate
187STDMETHODIMP ChromeActiveDocument::OnDocWindowActivate(BOOL activate) {
188 DLOG(INFO) << __FUNCTION__;
189 return S_OK;
190}
191
192STDMETHODIMP ChromeActiveDocument::TranslateAccelerator(MSG* msg) {
193 DLOG(INFO) << __FUNCTION__;
194 if (msg == NULL)
195 return E_POINTER;
196
197 if (msg->message == WM_KEYDOWN && msg->wParam == VK_TAB) {
198 HWND focus = ::GetFocus();
199 if (focus != m_hWnd && !::IsChild(m_hWnd, focus)) {
200 // The call to SetFocus triggers a WM_SETFOCUS that makes the base class
201 // set focus to the correct element in Chrome.
202 ::SetFocus(m_hWnd);
203 return S_OK;
204 }
205 }
206
207 return S_FALSE;
208}
209// Override IPersistStorageImpl::IsDirty
210STDMETHODIMP ChromeActiveDocument::IsDirty() {
211 DLOG(INFO) << __FUNCTION__;
212 return S_FALSE;
213}
214
[email protected]b95f550f2009-11-19 05:35:22215void ChromeActiveDocument::OnAutomationServerReady() {
[email protected]97965e12010-04-09 00:51:10216 BaseActiveX::OnAutomationServerReady();
[email protected]1fd45692010-04-19 21:01:18217 BaseActiveX::GiveFocusToChrome(true);
[email protected]b95f550f2009-11-19 05:35:22218}
219
[email protected]f7817822009-09-24 05:11:58220STDMETHODIMP ChromeActiveDocument::Load(BOOL fully_avalable,
221 IMoniker* moniker_name,
222 LPBC bind_context,
223 DWORD mode) {
[email protected]bc2ff5192010-06-01 22:05:45224 if (NULL == moniker_name)
[email protected]f7817822009-09-24 05:11:58225 return E_INVALIDARG;
[email protected]a1800e82009-11-19 00:53:23226
227 ScopedComPtr<IOleClientSite> client_site;
228 if (bind_context) {
229 ScopedComPtr<IUnknown> site;
230 bind_context->GetObjectParam(SZ_HTML_CLIENTSITE_OBJECTPARAM,
231 site.Receive());
232 if (site)
233 client_site.QueryFrom(site);
234 }
235
236 if (client_site) {
237 SetClientSite(client_site);
[email protected]b1c55638612010-03-08 16:26:11238 DoQueryService(IID_INewWindowManager, client_site,
239 popup_manager_.Receive());
[email protected]051236f2010-03-12 22:06:14240
241 // See if mshtml parsed the html header for us. If so, we need to
242 // clear the browser service flag that we use to indicate that this
243 // browser instance is navigating to a CF document.
244 ScopedComPtr<IBrowserService> browser_service;
245 DoQueryService(SID_SShellBrowser, client_site, browser_service.Receive());
246 if (browser_service) {
247 bool flagged = CheckForCFNavigation(browser_service, true);
248 DLOG_IF(INFO, flagged) << "Cleared flagged browser service";
249 }
[email protected]a1800e82009-11-19 00:53:23250 }
251
[email protected]051236f2010-03-12 22:06:14252 NavigationManager* mgr = NavigationManager::GetThreadInstance();
[email protected]21b4e6d2010-07-14 18:19:59253 DLOG_IF(ERROR, !mgr) << "Couldn't get instance of NavigationManager";
[email protected]7e3544b2010-01-22 00:02:34254
[email protected]29c32f902010-04-20 23:27:29255 std::wstring url;
256
[email protected]77d7aee2010-05-14 20:31:55257 ScopedComPtr<BindContextInfo> info;
258 BindContextInfo::FromBindContext(bind_context, info.Receive());
[email protected]29c32f902010-04-20 23:27:29259 DCHECK(info);
260 if (info && !info->url().empty()) {
261 url = info->url();
262 } else {
263 // If the original URL contains an anchor, then the URL queried
264 // from the moniker does not contain the anchor. To workaround
265 // this we retrieve the URL from our BHO.
266 url = GetActualUrlFromMoniker(moniker_name, bind_context,
267 mgr ? mgr->url(): std::wstring());
268 }
[email protected]051236f2010-03-12 22:06:14269
[email protected]c4e45b32010-07-28 21:15:15270 ChromeFrameUrl cf_url;
271 if (!cf_url.Parse(url)) {
[email protected]37346bc2009-09-25 22:54:33272 DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
[email protected]f7817822009-09-24 05:11:58273 return E_INVALIDARG;
274 }
275
[email protected]c4e45b32010-07-28 21:15:15276 if (!CanNavigateInFullTabMode(cf_url, security_manager_)) {
277 return E_INVALIDARG;
278 }
279
[email protected]aaf124502010-07-17 04:02:58280 std::string referrer = mgr ? mgr->referrer() : EmptyString();
281 // With CTransaction patch we have more robust way to grab the referrer for
282 // each top-level-switch-to-CF request by peeking at our sniffing data
283 // object that lives inside the bind context.
284 if (g_patch_helper.state() == PatchHelper::PATCH_PROTOCOL && info) {
285 scoped_refptr<ProtData> prot_data = info->get_prot_data();
286 if (prot_data)
287 referrer = prot_data->referrer();
288 }
289
[email protected]c4e45b32010-07-28 21:15:15290 if (!LaunchUrl(cf_url, referrer)) {
[email protected]37346bc2009-09-25 22:54:33291 NOTREACHED() << __FUNCTION__ << " Failed to launch url:" << url;
292 return E_INVALIDARG;
[email protected]f7817822009-09-24 05:11:58293 }
294
[email protected]c4e45b32010-07-28 21:15:15295 if (!cf_url.is_chrome_protocol() && !cf_url.attach_to_external_tab())
296 url_fetcher_.SetInfoForUrl(cf_url.url(), moniker_name, bind_context);
[email protected]f7817822009-09-24 05:11:58297
[email protected]333590002010-03-05 18:49:21298 THREAD_SAFE_UMA_HISTOGRAM_CUSTOM_COUNTS("ChromeFrame.FullTabLaunchType",
[email protected]c4e45b32010-07-28 21:15:15299 cf_url.is_chrome_protocol(),
300 0, 1, 2);
[email protected]f7817822009-09-24 05:11:58301 return S_OK;
302}
303
304STDMETHODIMP ChromeActiveDocument::Save(IMoniker* moniker_name,
305 LPBC bind_context,
306 BOOL remember) {
307 return E_NOTIMPL;
308}
309
310STDMETHODIMP ChromeActiveDocument::SaveCompleted(IMoniker* moniker_name,
311 LPBC bind_context) {
312 return E_NOTIMPL;
313}
314
315STDMETHODIMP ChromeActiveDocument::GetCurMoniker(IMoniker** moniker_name) {
316 return E_NOTIMPL;
317}
318
319STDMETHODIMP ChromeActiveDocument::GetClassID(CLSID* class_id) {
[email protected]bc2ff5192010-06-01 22:05:45320 if (NULL == class_id)
[email protected]f7817822009-09-24 05:11:58321 return E_POINTER;
[email protected]f7817822009-09-24 05:11:58322 *class_id = GetObjectCLSID();
323 return S_OK;
324}
325
326STDMETHODIMP ChromeActiveDocument::QueryStatus(const GUID* cmd_group_guid,
327 ULONG number_of_commands,
328 OLECMD commands[],
329 OLECMDTEXT* command_text) {
330 DLOG(INFO) << __FUNCTION__;
[email protected]b509ec82010-04-13 16:53:06331
[email protected]6fd1a7a2010-07-26 22:09:46332 CommandStatusMap* command_map = NULL;
[email protected]b509ec82010-04-13 16:53:06333
[email protected]6fd1a7a2010-07-26 22:09:46334 if (cmd_group_guid) {
335 if (IsEqualGUID(*cmd_group_guid, GUID_NULL)) {
336 command_map = &null_group_commands_map_;
337 } else if (IsEqualGUID(*cmd_group_guid, CGID_MSHTML)) {
338 command_map = &mshtml_group_commands_map_;
339 } else if (IsEqualGUID(*cmd_group_guid, CGID_Explorer)) {
340 command_map = &explorer_group_commands_map_;
341 } else if (IsEqualGUID(*cmd_group_guid, CGID_ShellDocView)) {
342 command_map = &shdoc_view_group_commands_map_;
343 }
344 } else {
345 command_map = &null_group_commands_map_;
346 }
347
348 if (!command_map) {
[email protected]b509ec82010-04-13 16:53:06349 DLOG(INFO) << "unsupported command group: "
350 << GuidToString(*cmd_group_guid);
351 return OLECMDERR_E_NOTSUPPORTED;
352 }
353
[email protected]f7817822009-09-24 05:11:58354 for (ULONG command_index = 0; command_index < number_of_commands;
355 command_index++) {
356 DLOG(INFO) << "Command id = " << commands[command_index].cmdID;
[email protected]6fd1a7a2010-07-26 22:09:46357 CommandStatusMap::iterator index =
358 command_map->find(commands[command_index].cmdID);
359 if (index != command_map->end())
360 commands[command_index].cmdf = index->second;
[email protected]f7817822009-09-24 05:11:58361 }
362 return S_OK;
363}
364
365STDMETHODIMP ChromeActiveDocument::Exec(const GUID* cmd_group_guid,
366 DWORD command_id,
367 DWORD cmd_exec_opt,
368 VARIANT* in_args,
369 VARIANT* out_args) {
370 DLOG(INFO) << __FUNCTION__ << " Cmd id =" << command_id;
371 // Bail out if we have been uninitialized.
372 if (automation_client_.get() && automation_client_->tab()) {
373 return ProcessExecCommand(cmd_group_guid, command_id, cmd_exec_opt,
374 in_args, out_args);
375 }
[email protected]e3200932009-10-09 21:33:03376 return OLECMDERR_E_NOTSUPPORTED;
[email protected]f7817822009-09-24 05:11:58377}
378
[email protected]a1800e82009-11-19 00:53:23379STDMETHODIMP ChromeActiveDocument::LoadHistory(IStream* stream,
380 IBindCtx* bind_context) {
381 // Read notes in ChromeActiveDocument::SaveHistory
382 DCHECK(stream);
383 LARGE_INTEGER offset = {0};
384 ULARGE_INTEGER cur_pos = {0};
385 STATSTG statstg = {0};
386
387 stream->Seek(offset, STREAM_SEEK_CUR, &cur_pos);
388 stream->Stat(&statstg, STATFLAG_NONAME);
389
390 DWORD url_size = statstg.cbSize.LowPart - cur_pos.LowPart;
391 ScopedBstr url_bstr;
392 DWORD bytes_read = 0;
393 stream->Read(url_bstr.AllocateBytes(url_size), url_size, &bytes_read);
394 std::wstring url(url_bstr);
395
[email protected]c4e45b32010-07-28 21:15:15396 ChromeFrameUrl cf_url;
397 if (!cf_url.Parse(url)) {
[email protected]a1800e82009-11-19 00:53:23398 DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
399 return E_INVALIDARG;
400 }
401
[email protected]c4e45b32010-07-28 21:15:15402 if (!CanNavigateInFullTabMode(cf_url, security_manager_)) {
403 return E_INVALIDARG;
404 }
405
[email protected]c5cbf4e2010-07-15 21:48:25406 const std::string& referrer = EmptyString();
[email protected]c4e45b32010-07-28 21:15:15407 if (!LaunchUrl(cf_url, referrer)) {
[email protected]a1800e82009-11-19 00:53:23408 NOTREACHED() << __FUNCTION__ << " Failed to launch url:" << url;
409 return E_INVALIDARG;
410 }
411 return S_OK;
412}
413
414STDMETHODIMP ChromeActiveDocument::SaveHistory(IStream* stream) {
415 // TODO(sanjeevr): We need to fetch the entire list of navigation entries
416 // from Chrome and persist it in the stream. And in LoadHistory we need to
417 // pass this list back to Chrome which will recreate the list. This will allow
418 // Back-Forward navigation to anchors to work correctly when we navigate to a
419 // page outside of ChromeFrame and then come back.
420 if (!stream) {
421 NOTREACHED();
422 return E_INVALIDARG;
423 }
424
425 LARGE_INTEGER offset = {0};
426 ULARGE_INTEGER new_pos = {0};
427 DWORD written = 0;
428 std::wstring url = UTF8ToWide(navigation_info_.url.spec());
429 return stream->Write(url.c_str(), (url.length() + 1) * sizeof(wchar_t),
430 &written);
431}
432
433STDMETHODIMP ChromeActiveDocument::SetPositionCookie(DWORD position_cookie) {
[email protected]77700832010-04-27 00:06:03434 if (automation_client_.get()) {
435 int index = static_cast<int>(position_cookie);
436 navigation_info_.navigation_index = index;
437 automation_client_->NavigateToIndex(index);
438 } else {
439 DLOG(WARNING) << "Invalid automation client instance";
440 }
[email protected]a1800e82009-11-19 00:53:23441 return S_OK;
442}
443
444STDMETHODIMP ChromeActiveDocument::GetPositionCookie(DWORD* position_cookie) {
445 if (!position_cookie)
446 return E_INVALIDARG;
447
448 *position_cookie = navigation_info_.navigation_index;
449 return S_OK;
450}
451
[email protected]f7817822009-09-24 05:11:58452STDMETHODIMP ChromeActiveDocument::GetUrlForEvents(BSTR* url) {
[email protected]bc2ff5192010-06-01 22:05:45453 if (NULL == url)
[email protected]f7817822009-09-24 05:11:58454 return E_POINTER;
[email protected]f7817822009-09-24 05:11:58455 *url = ::SysAllocString(url_);
456 return S_OK;
457}
458
[email protected]a1800e82009-11-19 00:53:23459STDMETHODIMP ChromeActiveDocument::GetAddressBarUrl(BSTR* url) {
460 return GetUrlForEvents(url);
461}
462
[email protected]80b5a8d2010-03-19 16:50:43463STDMETHODIMP ChromeActiveDocument::Reset() {
464 next_privacy_record_ = privacy_info_.privacy_records.begin();
465 return S_OK;
466}
467
[email protected]00aebd72010-07-16 14:44:32468STDMETHODIMP ChromeActiveDocument::GetSize(DWORD* size) {
[email protected]80b5a8d2010-03-19 16:50:43469 if (!size)
470 return E_POINTER;
471
472 *size = privacy_info_.privacy_records.size();
473 return S_OK;
474}
475
476STDMETHODIMP ChromeActiveDocument::GetPrivacyImpacted(BOOL* privacy_impacted) {
477 if (!privacy_impacted)
478 return E_POINTER;
479
480 *privacy_impacted = privacy_info_.privacy_impacted;
481 return S_OK;
482}
483
484STDMETHODIMP ChromeActiveDocument::Next(BSTR* url, BSTR* policy,
[email protected]00aebd72010-07-16 14:44:32485 LONG* reserved, DWORD* flags) {
[email protected]80b5a8d2010-03-19 16:50:43486 if (!url || !policy || !flags)
487 return E_POINTER;
488
489 if (next_privacy_record_ == privacy_info_.privacy_records.end())
490 return HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS);
491
492 *url = SysAllocString(next_privacy_record_->first.c_str());
493 *policy = SysAllocString(next_privacy_record_->second.policy_ref.c_str());
494 *flags = next_privacy_record_->second.flags;
495
496 next_privacy_record_++;
497 return S_OK;
498}
499
[email protected]f7817822009-09-24 05:11:58500HRESULT ChromeActiveDocument::IOleObject_SetClientSite(
501 IOleClientSite* client_site) {
502 if (client_site == NULL) {
503 ChromeActiveDocument* cached_document = g_active_doc_cache.Get();
504 if (cached_document) {
505 DCHECK(this == cached_document);
506 g_active_doc_cache.Set(NULL);
507 cached_document->Release();
508 }
[email protected]dda7d9c2009-11-11 23:01:47509
510 ScopedComPtr<IDocHostUIHandler> doc_host_handler;
[email protected]bc2ff5192010-06-01 22:05:45511 if (doc_site_)
[email protected]77059f72010-02-28 06:16:00512 doc_host_handler.QueryFrom(doc_site_);
[email protected]dda7d9c2009-11-11 23:01:47513
[email protected]bc2ff5192010-06-01 22:05:45514 if (doc_host_handler.get())
[email protected]dda7d9c2009-11-11 23:01:47515 doc_host_handler->HideUI();
[email protected]dda7d9c2009-11-11 23:01:47516
517 doc_site_.Release();
518 in_place_frame_.Release();
[email protected]f7817822009-09-24 05:11:58519 }
[email protected]a1800e82009-11-19 00:53:23520
[email protected]bc2ff5192010-06-01 22:05:45521 if (client_site != m_spClientSite)
[email protected]97965e12010-04-09 00:51:10522 return BaseActiveX::IOleObject_SetClientSite(client_site);
[email protected]a1800e82009-11-19 00:53:23523
524 return S_OK;
[email protected]f7817822009-09-24 05:11:58525}
526
[email protected]f7817822009-09-24 05:11:58527HRESULT ChromeActiveDocument::ActiveXDocActivate(LONG verb) {
528 HRESULT hr = S_OK;
529 m_bNegotiatedWnd = TRUE;
530 if (!m_bInPlaceActive) {
531 hr = m_spInPlaceSite->CanInPlaceActivate();
[email protected]bc2ff5192010-06-01 22:05:45532 if (FAILED(hr))
[email protected]f7817822009-09-24 05:11:58533 return hr;
[email protected]f7817822009-09-24 05:11:58534 m_spInPlaceSite->OnInPlaceActivate();
535 }
536 m_bInPlaceActive = TRUE;
537 // get location in the parent window,
538 // as well as some information about the parent
539 ScopedComPtr<IOleInPlaceUIWindow> in_place_ui_window;
540 frame_info_.cb = sizeof(OLEINPLACEFRAMEINFO);
541 HWND parent_window = NULL;
542 if (m_spInPlaceSite->GetWindow(&parent_window) == S_OK) {
543 in_place_frame_.Release();
544 RECT position_rect = {0};
545 RECT clip_rect = {0};
546 m_spInPlaceSite->GetWindowContext(in_place_frame_.Receive(),
547 in_place_ui_window.Receive(),
548 &position_rect,
549 &clip_rect,
550 &frame_info_);
551 if (!m_bWndLess) {
552 if (IsWindow()) {
553 ::ShowWindow(m_hWnd, SW_SHOW);
554 SetFocus();
555 } else {
[email protected]c1d19582010-03-03 01:57:13556 m_hWnd = Create(parent_window, position_rect, 0, 0, WS_EX_CLIENTEDGE);
[email protected]dd4beb522010-07-13 18:18:14557 if (!IsWindow()) {
558 // This might happen if the automation server couldn't be
559 // instantiated. If so, a NOTREACHED() will have already been hit.
560 DLOG(ERROR) << "Failed to create Ax window";
561 return AtlHresultFromLastError();
562 }
[email protected]f7817822009-09-24 05:11:58563 }
[email protected]e3a91e72010-06-17 01:19:04564
565 ScopedComPtr<IWebBrowser2> web_browser2;
566 DoQueryService(SID_SWebBrowserApp, m_spClientSite,
567 web_browser2.Receive());
568 if (web_browser2) {
569 if (!dimensions_.IsEmpty()) {
570 web_browser2->put_Width(dimensions_.width());
571 web_browser2->put_Height(dimensions_.height());
572 web_browser2->put_Left(dimensions_.x());
573 web_browser2->put_Top(dimensions_.y());
574 web_browser2->put_MenuBar(VARIANT_FALSE);
575 web_browser2->put_ToolBar(VARIANT_FALSE);
576
577 dimensions_.set_height(0);
578 dimensions_.set_width(0);
579 }
580 }
[email protected]f7817822009-09-24 05:11:58581 }
582 SetObjectRects(&position_rect, &clip_rect);
583 }
584
585 ScopedComPtr<IOleInPlaceActiveObject> in_place_active_object(this);
586
587 // Gone active by now, take care of UIACTIVATE
588 if (DoesVerbUIActivate(verb)) {
589 if (!m_bUIActive) {
590 m_bUIActive = TRUE;
591 hr = m_spInPlaceSite->OnUIActivate();
[email protected]bc2ff5192010-06-01 22:05:45592 if (FAILED(hr))
[email protected]f7817822009-09-24 05:11:58593 return hr;
[email protected]f7817822009-09-24 05:11:58594 // set ourselves up in the host
595 if (in_place_active_object) {
[email protected]bc2ff5192010-06-01 22:05:45596 if (in_place_frame_)
[email protected]f7817822009-09-24 05:11:58597 in_place_frame_->SetActiveObject(in_place_active_object, NULL);
[email protected]bc2ff5192010-06-01 22:05:45598 if (in_place_ui_window)
[email protected]f7817822009-09-24 05:11:58599 in_place_ui_window->SetActiveObject(in_place_active_object, NULL);
[email protected]f7817822009-09-24 05:11:58600 }
601 }
602 }
603 m_spClientSite->ShowObject();
604 return S_OK;
605}
606
607void ChromeActiveDocument::OnNavigationStateChanged(int tab_handle, int flags,
608 const IPC::NavigationInfo& nav_info) {
609 // TODO(joshia): handle INVALIDATE_TAB,INVALIDATE_LOAD etc.
610 DLOG(INFO) << __FUNCTION__ << std::endl << " Flags: " << flags
611 << "Url: " << nav_info.url <<
612 ", Title: " << nav_info.title <<
613 ", Type: " << nav_info.navigation_type << ", Relative Offset: " <<
[email protected]62bb18dc12009-11-25 01:34:08614 nav_info.relative_offset << ", Index: " << nav_info.navigation_index;
[email protected]f7817822009-09-24 05:11:58615
616 UpdateNavigationState(nav_info);
617}
618
619void ChromeActiveDocument::OnUpdateTargetUrl(int tab_handle,
620 const std::wstring& new_target_url) {
[email protected]bc2ff5192010-06-01 22:05:45621 if (in_place_frame_)
[email protected]f7817822009-09-24 05:11:58622 in_place_frame_->SetStatusText(new_target_url.c_str());
[email protected]f7817822009-09-24 05:11:58623}
624
625bool IsFindAccelerator(const MSG& msg) {
626 // TODO(robertshield): This may not stand up to localization. Fix if this
627 // is the case.
628 return msg.message == WM_KEYDOWN && msg.wParam == 'F' &&
629 win_util::IsCtrlPressed() &&
630 !(win_util::IsAltPressed() || win_util::IsShiftPressed());
631}
632
633void ChromeActiveDocument::OnAcceleratorPressed(int tab_handle,
634 const MSG& accel_message) {
[email protected]a15d4a42010-02-17 08:21:35635 if (::TranslateAccelerator(m_hWnd, accelerator_table_,
636 const_cast<MSG*>(&accel_message)))
637 return;
638
[email protected]f7817822009-09-24 05:11:58639 bool handled_accel = false;
640 if (in_place_frame_ != NULL) {
641 handled_accel = (S_OK == in_place_frame_->TranslateAcceleratorW(
642 const_cast<MSG*>(&accel_message), 0));
643 }
644
645 if (!handled_accel) {
646 if (IsFindAccelerator(accel_message)) {
647 // Handle the showing of the find dialog explicitly.
648 OnFindInPage();
[email protected]2b8fd322009-10-02 00:00:59649 } else {
[email protected]97965e12010-04-09 00:51:10650 BaseActiveX::OnAcceleratorPressed(tab_handle, accel_message);
[email protected]f7817822009-09-24 05:11:58651 }
652 } else {
653 DLOG(INFO) << "IE handled accel key " << accel_message.wParam;
654 }
655}
656
657void ChromeActiveDocument::OnTabbedOut(int tab_handle, bool reverse) {
658 DLOG(INFO) << __FUNCTION__;
659 if (in_place_frame_) {
660 MSG msg = { NULL, WM_KEYDOWN, VK_TAB };
661 in_place_frame_->TranslateAcceleratorW(&msg, 0);
662 }
663}
664
665void ChromeActiveDocument::OnDidNavigate(int tab_handle,
666 const IPC::NavigationInfo& nav_info) {
667 DLOG(INFO) << __FUNCTION__ << std::endl << "Url: " << nav_info.url <<
668 ", Title: " << nav_info.title <<
669 ", Type: " << nav_info.navigation_type << ", Relative Offset: " <<
670 nav_info.relative_offset << ", Index: " << nav_info.navigation_index;
671
[email protected]897b26272010-06-11 02:23:44672 CrashMetricsReporter::GetInstance()->IncrementMetric(
673 CrashMetricsReporter::CHROME_FRAME_NAVIGATION_COUNT);
674
[email protected]f7817822009-09-24 05:11:58675 // This could be NULL if the active document instance is being destroyed.
676 if (!m_spInPlaceSite) {
677 DLOG(INFO) << __FUNCTION__ << "m_spInPlaceSite is NULL. Returning";
678 return;
679 }
680
681 UpdateNavigationState(nav_info);
682}
683
[email protected]e16dd1672010-06-07 21:40:29684void ChromeActiveDocument::OnCloseTab(int tab_handle) {
685 ScopedComPtr<IWebBrowser2> web_browser2;
686 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
687 if (web_browser2)
688 web_browser2->Quit();
689}
690
[email protected]f7817822009-09-24 05:11:58691void ChromeActiveDocument::UpdateNavigationState(
692 const IPC::NavigationInfo& new_navigation_info) {
[email protected]a1800e82009-11-19 00:53:23693 HRESULT hr = S_OK;
[email protected]f7817822009-09-24 05:11:58694 bool is_title_changed = (navigation_info_.title != new_navigation_info.title);
[email protected]f7817822009-09-24 05:11:58695 bool is_ssl_state_changed =
696 (navigation_info_.security_style != new_navigation_info.security_style) ||
[email protected]b4e75c12010-05-18 18:28:48697 (navigation_info_.displayed_insecure_content !=
698 new_navigation_info.displayed_insecure_content) ||
699 (navigation_info_.ran_insecure_content !=
700 new_navigation_info.ran_insecure_content);
[email protected]f7817822009-09-24 05:11:58701
[email protected]f7817822009-09-24 05:11:58702 if (is_ssl_state_changed) {
703 int lock_status = SECURELOCK_SET_UNSECURE;
[email protected]a1800e82009-11-19 00:53:23704 switch (new_navigation_info.security_style) {
[email protected]f7817822009-09-24 05:11:58705 case SECURITY_STYLE_AUTHENTICATION_BROKEN:
706 lock_status = SECURELOCK_SET_SECUREUNKNOWNBIT;
707 break;
708 case SECURITY_STYLE_AUTHENTICATED:
[email protected]b4e75c12010-05-18 18:28:48709 lock_status = new_navigation_info.displayed_insecure_content ?
[email protected]f7817822009-09-24 05:11:58710 SECURELOCK_SET_MIXED : SECURELOCK_SET_SECUREUNKNOWNBIT;
711 break;
712 default:
713 break;
714 }
715
716 ScopedVariant secure_lock_status(lock_status);
717 IEExec(&CGID_ShellDocView, INTERNAL_CMDID_SET_SSL_LOCK,
718 OLECMDEXECOPT_DODEFAULT, secure_lock_status.AsInput(), NULL);
719 }
720
[email protected]a1800e82009-11-19 00:53:23721 // Ideally all navigations should come to Chrome Frame so that we can call
722 // BeforeNavigate2 on installed BHOs and give them a chance to cancel the
723 // navigation. However, in practice what happens is as below:
724 // The very first navigation that happens in CF happens via a Load or a
725 // LoadHistory call. In this case, IE already has the correct information for
726 // its travel log as well address bar. For other internal navigations (navs
727 // that only happen within Chrome such as anchor navigations) we need to
728 // update IE's internal state after the fact. In the case of internal
729 // navigations, we notify the BHOs but ignore the should_cancel flag.
[email protected]6f526082010-01-28 19:36:58730
731 // Another case where we need to issue BeforeNavigate2 calls is as below:-
732 // We get notified after the fact, when navigations are initiated within
733 // Chrome via window.open calls. These navigations are handled by creating
734 // an external tab container within chrome and then connecting to it from IE.
735 // We still want to update the address bar/history, etc, to ensure that
736 // the special URL used by Chrome to indicate this is updated correctly.
737 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]6f526082010-01-28 19:36:58739 navigation_info_.navigation_index)) ||
[email protected]c4e45b32010-07-28 21:15:15740 MatchPatternWide(static_cast<BSTR>(url_), kChromeFrameAttachTabPattern);
[email protected]f7817822009-09-24 05:11:58741
[email protected]bc2ff5192010-06-01 22:05:45742 if (new_navigation_info.url.is_valid())
[email protected]38939de2010-05-13 02:32:06743 url_.Allocate(UTF8ToWide(new_navigation_info.url.spec()).c_str());
[email protected]38939de2010-05-13 02:32:06744
[email protected]a1800e82009-11-19 00:53:23745 if (is_internal_navigation) {
746 ScopedComPtr<IDocObjectService> doc_object_svc;
[email protected]f7817822009-09-24 05:11:58747 ScopedComPtr<IWebBrowserEventsService> web_browser_events_svc;
[email protected]ae33d672010-03-04 21:58:06748
[email protected]5ae94d22010-07-21 19:55:36749 buggy_bho::BuggyBhoTls bad_bho_tls;
750 if (GetConfigBool(true, kEnableBuggyBhoIntercept)) {
751 ScopedComPtr<IWebBrowser2> wb2;
752 DoQueryService(SID_SWebBrowserApp, m_spClientSite, wb2.Receive());
753 if (wb2) {
754 buggy_bho::BuggyBhoTls::PatchBuggyBHOs(wb2);
755 }
756 }
757
[email protected]f7817822009-09-24 05:11:58758 DoQueryService(__uuidof(web_browser_events_svc), m_spClientSite,
759 web_browser_events_svc.Receive());
[email protected]ae33d672010-03-04 21:58:06760
761 if (!web_browser_events_svc.get()) {
762 DoQueryService(SID_SShellBrowser, m_spClientSite,
763 doc_object_svc.Receive());
764 }
765
[email protected]62bb18dc12009-11-25 01:34:08766 // web_browser_events_svc can be NULL on IE6.
[email protected]f7817822009-09-24 05:11:58767 if (web_browser_events_svc) {
[email protected]f7817822009-09-24 05:11:58768 VARIANT_BOOL should_cancel = VARIANT_FALSE;
769 web_browser_events_svc->FireBeforeNavigate2Event(&should_cancel);
[email protected]a1800e82009-11-19 00:53:23770 }
771
772 // We need to tell IE that we support navigation so that IE will query us
773 // for IPersistHistory and call GetPositionCookie to save our navigation
774 // index.
775 ScopedVariant html_window(static_cast<IUnknown*>(
776 static_cast<IHTMLWindow2*>(this)));
777 IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0,
778 html_window.AsInput(), NULL);
779
780 // We pass the HLNF_INTERNALJUMP flag to INTERNAL_CMDID_FINALIZE_TRAVEL_LOG
781 // since we want to make IE treat all internal navigations within this page
782 // (including anchor navigations and subframe navigations) as anchor
783 // navigations. This will ensure that IE calls GetPositionCookie
784 // to save the current position cookie in the travel log and then call
785 // SetPositionCookie when the user hits Back/Forward to come back here.
786 ScopedVariant internal_navigation(HLNF_INTERNALJUMP);
787 IEExec(&CGID_Explorer, INTERNAL_CMDID_FINALIZE_TRAVEL_LOG, 0,
788 internal_navigation.AsInput(), NULL);
789
790 // We no longer need to lie to IE. If we lie persistently to IE, then
791 // IE reuses us for new navigations.
792 IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0, NULL, NULL);
793
794 if (doc_object_svc) {
795 // Now call the FireNavigateCompleteEvent which makes IE update the text
796 // in the address-bar.
797 doc_object_svc->FireNavigateComplete2(this, 0);
[email protected]5ae94d22010-07-21 19:55:36798 doc_object_svc->FireDocumentComplete(this, 0);
[email protected]a1800e82009-11-19 00:53:23799 } else if (web_browser_events_svc) {
[email protected]f7817822009-09-24 05:11:58800 web_browser_events_svc->FireNavigateComplete2Event();
[email protected]5ae94d22010-07-21 19:55:36801 web_browser_events_svc->FireDocumentCompleteEvent();
[email protected]f7817822009-09-24 05:11:58802 }
803 }
[email protected]37346bc2009-09-25 22:54:33804
[email protected]a1800e82009-11-19 00:53:23805 if (is_title_changed) {
806 ScopedVariant title(new_navigation_info.title.c_str());
807 IEExec(NULL, OLECMDID_SETTITLE, OLECMDEXECOPT_DONTPROMPTUSER,
808 title.AsInput(), NULL);
809 }
810
811 // It is important that we only update the navigation_info_ after we have
812 // finalized the travel log. This is because IE will ask for information
813 // such as navigation index when the travel log is finalized and we need
814 // supply the old index and not the new one.
815 navigation_info_ = new_navigation_info;
[email protected]37346bc2009-09-25 22:54:33816 // Update the IE zone here. Ideally we would like to do it when the active
817 // document is activated. However that does not work at times as the frame we
818 // get there is not the actual frame which handles the command.
819 IEExec(&CGID_Explorer, SBCMDID_MIXEDZONE, 0, NULL, NULL);
[email protected]f7817822009-09-24 05:11:58820}
821
822void ChromeActiveDocument::OnFindInPage() {
823 TabProxy* tab = GetTabProxy();
824 if (tab) {
[email protected]bc2ff5192010-06-01 22:05:45825 if (!find_dialog_.IsWindow())
[email protected]f7817822009-09-24 05:11:58826 find_dialog_.Create(m_hWnd);
[email protected]f7817822009-09-24 05:11:58827
828 find_dialog_.ShowWindow(SW_SHOW);
829 }
830}
831
832void ChromeActiveDocument::OnViewSource() {
833 DCHECK(navigation_info_.url.is_valid());
[email protected]76e7da22010-06-18 22:44:49834 HostNavigate(GURL(chrome::kViewSourceScheme + std::string(":") +
835 navigation_info_.url.spec()), GURL(), NEW_WINDOW);
[email protected]f7817822009-09-24 05:11:58836}
837
[email protected]37346bc2009-09-25 22:54:33838void ChromeActiveDocument::OnDetermineSecurityZone(const GUID* cmd_group_guid,
839 DWORD command_id,
840 DWORD cmd_exec_opt,
841 VARIANT* in_args,
842 VARIANT* out_args) {
[email protected]7c712c92010-03-24 17:29:22843 // Always return URLZONE_INTERNET since that is the Chrome's behaviour.
844 // Correct step is to use MapUrlToZone().
[email protected]37346bc2009-09-25 22:54:33845 if (out_args != NULL) {
846 out_args->vt = VT_UI4;
847 out_args->ulVal = URLZONE_INTERNET;
848 }
849}
850
[email protected]80b5a8d2010-03-19 16:50:43851void ChromeActiveDocument::OnDisplayPrivacyInfo() {
852 privacy_info_ = url_fetcher_.privacy_info();
853 Reset();
854 DoPrivacyDlg(m_hWnd, url_, this, TRUE);
855}
856
[email protected]b36a9f92009-10-19 17:34:57857void ChromeActiveDocument::OnOpenURL(int tab_handle,
858 const GURL& url_to_open,
859 const GURL& referrer,
[email protected]f7817822009-09-24 05:11:58860 int open_disposition) {
861 // If the disposition indicates that we should be opening the URL in the
862 // current tab, then we can reuse the ChromeFrameAutomationClient instance
863 // maintained by the current ChromeActiveDocument instance. We cache this
864 // instance so that it can be used by the new ChromeActiveDocument instance
865 // which may be instantiated for handling the new URL.
866 if (open_disposition == CURRENT_TAB) {
867 // Grab a reference to ensure that the document remains valid.
868 AddRef();
869 g_active_doc_cache.Set(this);
870 }
871
[email protected]97965e12010-04-09 00:51:10872 BaseActiveX::OnOpenURL(tab_handle, url_to_open, referrer, open_disposition);
[email protected]f7817822009-09-24 05:11:58873}
874
[email protected]b1c55638612010-03-08 16:26:11875void ChromeActiveDocument::OnAttachExternalTab(int tab_handle,
876 const IPC::AttachExternalTabParams& params) {
[email protected]77700832010-04-27 00:06:03877 if (!automation_client_.get()) {
878 DLOG(WARNING) << "Invalid automation client instance";
879 return;
880 }
[email protected]b1c55638612010-03-08 16:26:11881 DWORD flags = 0;
882 if (params.user_gesture)
883 flags = NWMF_USERREQUESTED;
[email protected]355309e2010-03-15 17:01:34884 else if (popup_allowed_)
885 flags = NWMF_USERALLOWED;
[email protected]b1c55638612010-03-08 16:26:11886
887 HRESULT hr = S_OK;
888 if (popup_manager_) {
[email protected]355309e2010-03-15 17:01:34889 LPCWSTR popup_wnd_url = UTF8ToWide(params.url.spec()).c_str();
890 hr = popup_manager_->EvaluateNewWindow(popup_wnd_url, NULL, url_,
891 NULL, FALSE, flags, 0);
[email protected]b1c55638612010-03-08 16:26:11892 }
893 // Allow popup
894 if (hr == S_OK) {
[email protected]97965e12010-04-09 00:51:10895 BaseActiveX::OnAttachExternalTab(tab_handle, params);
[email protected]b1c55638612010-03-08 16:26:11896 return;
897 }
898
899 automation_client_->BlockExternalTab(params.cookie);
900}
901
[email protected]f7817822009-09-24 05:11:58902bool ChromeActiveDocument::PreProcessContextMenu(HMENU menu) {
903 ScopedComPtr<IBrowserService> browser_service;
904 ScopedComPtr<ITravelLog> travel_log;
[email protected]a1800e82009-11-19 00:53:23905 GetBrowserServiceAndTravelLog(browser_service.Receive(),
906 travel_log.Receive());
907 if (!browser_service || !travel_log)
[email protected]f7817822009-09-24 05:11:58908 return true;
909
[email protected]bc2ff5192010-06-01 22:05:45910 EnableMenuItem(menu, IDS_CONTENT_CONTEXT_BACK, MF_BYCOMMAND |
911 (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_BACK, NULL)) ?
912 MF_ENABLED : MF_DISABLED));
913 EnableMenuItem(menu, IDS_CONTENT_CONTEXT_FORWARD, MF_BYCOMMAND |
914 (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_FORE, NULL)) ?
915 MF_ENABLED : MF_DISABLED));
[email protected]f7817822009-09-24 05:11:58916
917 // Call base class (adds 'About' item)
[email protected]97965e12010-04-09 00:51:10918 return BaseActiveX::PreProcessContextMenu(menu);
[email protected]f7817822009-09-24 05:11:58919}
920
[email protected]35f13ab2009-12-16 23:59:17921bool ChromeActiveDocument::HandleContextMenuCommand(UINT cmd,
922 const IPC::ContextMenuParams& params) {
[email protected]f7817822009-09-24 05:11:58923 ScopedComPtr<IWebBrowser2> web_browser2;
924 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
925
[email protected]074283a2010-05-28 23:47:59926 if (cmd == IDC_BACK)
[email protected]b731a142010-05-14 00:03:03927 web_browser2->GoBack();
[email protected]074283a2010-05-28 23:47:59928 else if (cmd == IDC_FORWARD)
[email protected]b731a142010-05-14 00:03:03929 web_browser2->GoForward();
[email protected]074283a2010-05-28 23:47:59930 else if (cmd == IDC_RELOAD)
[email protected]b731a142010-05-14 00:03:03931 web_browser2->Refresh();
[email protected]074283a2010-05-28 23:47:59932 else
[email protected]b731a142010-05-14 00:03:03933 return BaseActiveX::HandleContextMenuCommand(cmd, params);
[email protected]f7817822009-09-24 05:11:58934
935 return true;
936}
937
938HRESULT ChromeActiveDocument::IEExec(const GUID* cmd_group_guid,
939 DWORD command_id, DWORD cmd_exec_opt,
940 VARIANT* in_args, VARIANT* out_args) {
941 HRESULT hr = E_FAIL;
[email protected]37346bc2009-09-25 22:54:33942
[email protected]f7817822009-09-24 05:11:58943 ScopedComPtr<IOleCommandTarget> frame_cmd_target;
[email protected]37346bc2009-09-25 22:54:33944
945 ScopedComPtr<IOleInPlaceSite> in_place_site(m_spInPlaceSite);
[email protected]bc2ff5192010-06-01 22:05:45946 if (!in_place_site.get() && m_spClientSite != NULL)
[email protected]37346bc2009-09-25 22:54:33947 in_place_site.QueryFrom(m_spClientSite);
[email protected]37346bc2009-09-25 22:54:33948
949 if (in_place_site)
950 hr = frame_cmd_target.QueryFrom(in_place_site);
[email protected]f7817822009-09-24 05:11:58951
[email protected]bc2ff5192010-06-01 22:05:45952 if (frame_cmd_target) {
[email protected]f7817822009-09-24 05:11:58953 hr = frame_cmd_target->Exec(cmd_group_guid, command_id, cmd_exec_opt,
954 in_args, out_args);
[email protected]bc2ff5192010-06-01 22:05:45955 }
[email protected]f7817822009-09-24 05:11:58956
957 return hr;
958}
[email protected]37346bc2009-09-25 22:54:33959
[email protected]c4e45b32010-07-28 21:15:15960bool ChromeActiveDocument::LaunchUrl(const ChromeFrameUrl& cf_url,
961 const std::string& referrer) {
[email protected]77700832010-04-27 00:06:03962 DCHECK(automation_client_.get() != NULL);
[email protected]c4e45b32010-07-28 21:15:15963 DCHECK(!cf_url.url().empty());
[email protected]77700832010-04-27 00:06:03964
[email protected]c4e45b32010-07-28 21:15:15965 url_.Allocate(cf_url.url().c_str());
[email protected]6f526082010-01-28 19:36:58966
[email protected]e150e822010-06-03 23:10:55967 std::string utf8_url;
[email protected]c4e45b32010-07-28 21:15:15968 WideToUTF8(url_, url_.Length(), &utf8_url);
[email protected]e150e822010-06-03 23:10:55969
[email protected]c4e45b32010-07-28 21:15:15970 DLOG(INFO) << "Url is " << url_;
[email protected]e3a91e72010-06-17 01:19:04971
[email protected]c4e45b32010-07-28 21:15:15972 // Initiate navigation before launching chrome so that the url will be
973 // cached and sent with launch settings.
974 if (cf_url.attach_to_external_tab()) {
975 dimensions_ = cf_url.dimensions();
976 automation_client_->AttachExternalTab(cf_url.cookie());
977 } else if (!automation_client_->InitiateNavigation(utf8_url,
978 referrer,
979 is_privileged_)) {
980 DLOG(ERROR) << "Invalid URL: " << url_;
981 Error(L"Invalid URL");
982 url_.Reset();
983 return false;
[email protected]37346bc2009-09-25 22:54:33984 }
985
[email protected]3eb07da2010-02-01 19:48:36986 if (is_automation_client_reused_)
987 return true;
[email protected]37346bc2009-09-25 22:54:33988
[email protected]3eb07da2010-02-01 19:48:36989 automation_client_->SetUrlFetcher(&url_fetcher_);
990
[email protected]e150e822010-06-03 23:10:55991 return InitializeAutomation(GetHostProcessName(false), L"", IsIEInPrivate(),
992 false, GURL(utf8_url), GURL(referrer));
[email protected]37346bc2009-09-25 22:54:33993}
[email protected]1bb5f892009-10-06 01:44:57994
[email protected]355309e2010-03-15 17:01:34995
996HRESULT ChromeActiveDocument::OnRefreshPage(const GUID* cmd_group_guid,
997 DWORD command_id, DWORD cmd_exec_opt, VARIANT* in_args, VARIANT* out_args) {
998 popup_allowed_ = false;
999 if (in_args->vt == VT_I4 &&
1000 in_args->lVal & OLECMDIDF_REFRESH_PAGEACTION_POPUPWINDOW) {
1001 popup_allowed_ = true;
1002
1003 // Ask the yellow security band to change the text and icon and to remain
1004 // visible.
1005 IEExec(&CGID_DocHostCommandHandler, OLECMDID_PAGEACTIONBLOCKED,
1006 0x80000000 | OLECMDIDF_WINDOWSTATE_USERVISIBLE_VALID, NULL, NULL);
1007 }
1008
1009 TabProxy* tab_proxy = GetTabProxy();
1010 if (tab_proxy)
1011 tab_proxy->ReloadAsync();
1012
1013 return S_OK;
1014}
1015
1016
[email protected]1bb5f892009-10-06 01:44:571017HRESULT ChromeActiveDocument::SetPageFontSize(const GUID* cmd_group_guid,
1018 DWORD command_id,
1019 DWORD cmd_exec_opt,
1020 VARIANT* in_args,
1021 VARIANT* out_args) {
1022 if (!automation_client_.get()) {
[email protected]77700832010-04-27 00:06:031023 NOTREACHED() << "Invalid automation client";
[email protected]1bb5f892009-10-06 01:44:571024 return E_FAIL;
1025 }
1026
1027 switch (command_id) {
1028 case IDM_BASELINEFONT1:
1029 automation_client_->SetPageFontSize(SMALLEST_FONT);
1030 break;
1031
1032 case IDM_BASELINEFONT2:
1033 automation_client_->SetPageFontSize(SMALL_FONT);
1034 break;
1035
1036 case IDM_BASELINEFONT3:
1037 automation_client_->SetPageFontSize(MEDIUM_FONT);
1038 break;
1039
1040 case IDM_BASELINEFONT4:
1041 automation_client_->SetPageFontSize(LARGE_FONT);
1042 break;
1043
1044 case IDM_BASELINEFONT5:
1045 automation_client_->SetPageFontSize(LARGEST_FONT);
1046 break;
1047
1048 default:
1049 NOTREACHED() << "Invalid font size command: "
1050 << command_id;
1051 return E_FAIL;
1052 }
1053
1054 // Forward the command back to IEFrame with group set to
1055 // CGID_ExplorerBarDoc. This is probably needed to update the menu state to
1056 // indicate that the font size was set. This currently fails with error
1057 // 0x80040104.
1058 // TODO(iyengar)
1059 // Do some investigation into why this Exec call fails.
1060 IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
1061 return S_OK;
1062}
1063
[email protected]2f2afba2010-04-01 01:53:191064HRESULT ChromeActiveDocument::OnEncodingChange(const GUID* cmd_group_guid,
1065 DWORD command_id,
1066 DWORD cmd_exec_opt,
1067 VARIANT* in_args,
1068 VARIANT* out_args) {
1069 const struct EncodingMapData {
1070 DWORD ie_encoding_id;
1071 const char* chrome_encoding_name;
1072 } kEncodingTestDatas[] = {
1073#define DEFINE_ENCODING_MAP(encoding_name, id, chrome_name) \
1074 { encoding_name, chrome_name },
1075 INTERNAL_IE_ENCODINGMENU_IDS(DEFINE_ENCODING_MAP)
1076#undef DEFINE_ENCODING_MAP
1077 };
1078
1079 if (!automation_client_.get()) {
1080 NOTREACHED() << "Invalid automtion client";
1081 return E_FAIL;
1082 }
1083
[email protected]e9b35282010-06-02 16:43:191084 // Using ARRAYSIZE_UNSAFE in here is because we define the struct
1085 // EncodingMapData inside function.
[email protected]2f2afba2010-04-01 01:53:191086 const char* chrome_encoding_name = NULL;
1087 for (int i = 0; i < ARRAYSIZE_UNSAFE(kEncodingTestDatas); ++i) {
1088 const struct EncodingMapData* encoding_data = &kEncodingTestDatas[i];
1089 if (command_id == encoding_data->ie_encoding_id) {
1090 chrome_encoding_name = encoding_data->chrome_encoding_name;
1091 break;
1092 }
1093 }
1094 // Return E_FAIL when encountering invalid encoding id.
1095 if (!chrome_encoding_name)
1096 return E_FAIL;
1097
1098 TabProxy* tab = GetTabProxy();
1099 if (!tab) {
1100 NOTREACHED() << "Can not get TabProxy";
1101 return E_FAIL;
1102 }
1103
1104 if (chrome_encoding_name)
1105 tab->OverrideEncoding(chrome_encoding_name);
1106
1107 // Like we did on SetPageFontSize, we may forward the command back to IEFrame
1108 // to update the menu state to indicate that which encoding was set.
1109 // TODO(iyengar)
1110 // Do some investigation into why this Exec call fails.
1111 IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
1112 return S_OK;
1113}
1114
[email protected]f9cc4c452009-10-13 14:56:381115void ChromeActiveDocument::OnGoToHistoryEntryOffset(int tab_handle,
1116 int offset) {
[email protected]a1800e82009-11-19 00:53:231117 DLOG(INFO) << __FUNCTION__ << " - offset:" << offset;
1118
[email protected]f9cc4c452009-10-13 14:56:381119 ScopedComPtr<IBrowserService> browser_service;
[email protected]a1800e82009-11-19 00:53:231120 ScopedComPtr<ITravelLog> travel_log;
1121 GetBrowserServiceAndTravelLog(browser_service.Receive(),
1122 travel_log.Receive());
1123
1124 if (browser_service && travel_log)
1125 travel_log->Travel(browser_service, offset);
1126}
1127
1128HRESULT ChromeActiveDocument::GetBrowserServiceAndTravelLog(
1129 IBrowserService** browser_service, ITravelLog** travel_log) {
1130 DCHECK(browser_service || travel_log);
1131 ScopedComPtr<IBrowserService> browser_service_local;
1132 HRESULT hr = DoQueryService(SID_SShellBrowser, m_spClientSite,
1133 browser_service_local.Receive());
1134 if (!browser_service_local) {
1135 NOTREACHED() << "DoQueryService for IBrowserService failed: " << hr;
1136 return hr;
[email protected]f9cc4c452009-10-13 14:56:381137 }
[email protected]a1800e82009-11-19 00:53:231138
1139 if (travel_log) {
1140 hr = browser_service_local->GetTravelLog(travel_log);
[email protected]bc2ff5192010-06-01 22:05:451141 DLOG_IF(INFO, !travel_log) << "browser_service->GetTravelLog failed: " <<
1142 hr;
[email protected]a1800e82009-11-19 00:53:231143 }
1144
1145 if (browser_service)
1146 *browser_service = browser_service_local.Detach();
1147
1148 return hr;
[email protected]f9cc4c452009-10-13 14:56:381149}
[email protected]a15d4a42010-02-17 08:21:351150
1151LRESULT ChromeActiveDocument::OnForward(WORD notify_code, WORD id,
1152 HWND control_window,
1153 BOOL& bHandled) {
1154 ScopedComPtr<IWebBrowser2> web_browser2;
1155 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
1156 DCHECK(web_browser2);
1157
[email protected]bc2ff5192010-06-01 22:05:451158 if (web_browser2)
[email protected]a15d4a42010-02-17 08:21:351159 web_browser2->GoForward();
[email protected]a15d4a42010-02-17 08:21:351160 return 0;
1161}
1162
1163LRESULT ChromeActiveDocument::OnBack(WORD notify_code, WORD id,
1164 HWND control_window,
1165 BOOL& bHandled) {
1166 ScopedComPtr<IWebBrowser2> web_browser2;
1167 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
1168 DCHECK(web_browser2);
1169
[email protected]bc2ff5192010-06-01 22:05:451170 if (web_browser2)
[email protected]a15d4a42010-02-17 08:21:351171 web_browser2->GoBack();
[email protected]a15d4a42010-02-17 08:21:351172 return 0;
1173}
1174
[email protected]80b5a8d2010-03-19 16:50:431175LRESULT ChromeActiveDocument::OnFirePrivacyChange(UINT message, WPARAM wparam,
1176 LPARAM lparam,
1177 BOOL& handled) {
1178 if (!m_spClientSite)
1179 return 0;
1180
1181 ScopedComPtr<IWebBrowser2> web_browser2;
1182 DoQueryService(SID_SWebBrowserApp, m_spClientSite,
1183 web_browser2.Receive());
1184 if (!web_browser2) {
1185 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
1186 return 0;
1187 }
1188
1189 ScopedComPtr<IShellBrowser> shell_browser;
1190 DoQueryService(SID_STopLevelBrowser, web_browser2,
1191 shell_browser.Receive());
1192 DCHECK(shell_browser.get() != NULL);
1193 ScopedComPtr<ITridentService2> trident_services;
1194 trident_services.QueryFrom(shell_browser);
[email protected]bc2ff5192010-06-01 22:05:451195 if (trident_services)
[email protected]80b5a8d2010-03-19 16:50:431196 trident_services->FirePrivacyImpactedStateChange(wparam);
[email protected]bc2ff5192010-06-01 22:05:451197 else
[email protected]80b5a8d2010-03-19 16:50:431198 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
[email protected]80b5a8d2010-03-19 16:50:431199 return 0;
1200}
[email protected]00aebd72010-07-16 14:44:321201
[email protected]723e5442010-07-21 17:36:051202LRESULT ChromeActiveDocument::OnShowWindow(UINT message, WPARAM wparam,
1203 LPARAM lparam,
1204 BOOL& handled) { // NO_LINT
1205 if (wparam)
1206 SetFocus();
1207 return 0;
1208}
1209
1210LRESULT ChromeActiveDocument::OnSetFocus(UINT message, WPARAM wparam,
1211 LPARAM lparam,
1212 BOOL& handled) { // NO_LINT
1213 GiveFocusToChrome(false);
1214 return 0;
1215}