blob: da3da636ea7f8ff16dbc7d3d5e3a0d8c950156b7 [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"
21#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]9607f6a82010-06-12 17:24:4141#include "chrome_frame/crash_reporting/crash_metrics.h"
[email protected]f7817822009-09-24 05:11:5842#include "chrome_frame/utils.h"
43
44const wchar_t kChromeAttachExternalTabPrefix[] = L"attach_external_tab";
45
46static 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
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]e3a91e72010-06-17 01:19:0468 accelerator_table_(NULL),
69 is_new_navigation_(false) {
[email protected]04e3f352010-05-10 13:48:2470 TRACE_EVENT_BEGIN("chromeframe.createactivedocument", this, "");
71
[email protected]3eb07da2010-02-01 19:48:3672 url_fetcher_.set_frame_busting(false);
[email protected]a1800e82009-11-19 00:53:2373 memset(&navigation_info_, 0, sizeof(navigation_info_));
[email protected]f7817822009-09-24 05:11:5874}
75
76HRESULT ChromeActiveDocument::FinalConstruct() {
77 // If we have a cached ChromeActiveDocument instance in TLS, then grab
78 // ownership of the cached document's automation client. This is an
79 // optimization to get Chrome active documents to load faster.
80 ChromeActiveDocument* cached_document = g_active_doc_cache.Get();
[email protected]58174a72010-05-05 21:30:3081 if (cached_document && cached_document->IsValid()) {
[email protected]f7817822009-09-24 05:11:5882 DCHECK(automation_client_.get() == NULL);
[email protected]3148c0ae2010-03-11 18:06:0083 automation_client_.swap(cached_document->automation_client_);
[email protected]f7817822009-09-24 05:11:5884 DLOG(INFO) << "Reusing automation client instance from "
85 << cached_document;
86 DCHECK(automation_client_.get() != NULL);
[email protected]3eb07da2010-02-01 19:48:3687 automation_client_->Reinitialize(this, &url_fetcher_);
[email protected]f7817822009-09-24 05:11:5888 is_automation_client_reused_ = true;
89 } else {
90 // The FinalConstruct implementation in the ChromeFrameActivexBase class
91 // i.e. Base creates an instance of the ChromeFrameAutomationClient class
92 // and initializes it, which would spawn a new Chrome process, etc.
93 // We don't want to be doing this if we have a cached document, whose
94 // automation client instance can be reused.
[email protected]97965e12010-04-09 00:51:1095 HRESULT hr = BaseActiveX::FinalConstruct();
[email protected]f7817822009-09-24 05:11:5896 if (FAILED(hr))
97 return hr;
98 }
99
100 bool chrome_network = GetConfigBool(false, kUseChromeNetworking);
101 bool top_level_requests = GetConfigBool(true, kHandleTopLevelRequests);
102 automation_client_->set_use_chrome_network(chrome_network);
103 automation_client_->set_handle_top_level_requests(top_level_requests);
104
105 find_dialog_.Init(automation_client_.get());
106
107 enabled_commands_map_[OLECMDID_PRINT] = true;
108 enabled_commands_map_[OLECMDID_FIND] = true;
109 enabled_commands_map_[OLECMDID_CUT] = true;
110 enabled_commands_map_[OLECMDID_COPY] = true;
111 enabled_commands_map_[OLECMDID_PASTE] = true;
112 enabled_commands_map_[OLECMDID_SELECTALL] = true;
[email protected]673fd2c02010-02-04 23:10:00113 enabled_commands_map_[OLECMDID_SAVEAS] = true;
[email protected]a15d4a42010-02-17 08:21:35114
[email protected]3c6f8e12010-03-24 21:58:21115 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase);
[email protected]a15d4a42010-02-17 08:21:35116 accelerator_table_ =
[email protected]3c6f8e12010-03-24 21:58:21117 LoadAccelerators(this_module,
[email protected]a15d4a42010-02-17 08:21:35118 MAKEINTRESOURCE(IDR_CHROME_FRAME_IE_FULL_TAB));
119 DCHECK(accelerator_table_ != NULL);
[email protected]f7817822009-09-24 05:11:58120 return S_OK;
121}
122
123ChromeActiveDocument::~ChromeActiveDocument() {
124 DLOG(INFO) << __FUNCTION__;
[email protected]bc2ff5192010-06-01 22:05:45125 if (find_dialog_.IsWindow())
[email protected]f7817822009-09-24 05:11:58126 find_dialog_.DestroyWindow();
[email protected]3eb07da2010-02-01 19:48:36127 // ChromeFramePlugin
[email protected]97965e12010-04-09 00:51:10128 BaseActiveX::Uninitialize();
[email protected]04e3f352010-05-10 13:48:24129
130 TRACE_EVENT_END("chromeframe.createactivedocument", this, "");
[email protected]f7817822009-09-24 05:11:58131}
132
133// Override DoVerb
134STDMETHODIMP ChromeActiveDocument::DoVerb(LONG verb,
135 LPMSG msg,
136 IOleClientSite* active_site,
137 LONG index,
138 HWND parent_window,
139 LPCRECT pos) {
140 // IE will try and in-place activate us in some cases. This happens when
141 // the user opens a new IE window with a URL that has us as the DocObject.
142 // Here we refuse to be activated in-place and we will force IE to UIActivate
143 // us.
[email protected]bc2ff5192010-06-01 22:05:45144 if (OLEIVERB_INPLACEACTIVATE == verb)
[email protected]f7817822009-09-24 05:11:58145 return E_NOTIMPL;
[email protected]f7817822009-09-24 05:11:58146 // Check if we should activate as a docobject or not
147 // (client supports IOleDocumentSite)
148 if (doc_site_) {
149 switch (verb) {
[email protected]1bb5f892009-10-06 01:44:57150 case OLEIVERB_SHOW: {
151 ScopedComPtr<IDocHostUIHandler> doc_host_handler;
152 doc_host_handler.QueryFrom(doc_site_);
[email protected]bc2ff5192010-06-01 22:05:45153 if (doc_host_handler.get())
[email protected]1bb5f892009-10-06 01:44:57154 doc_host_handler->ShowUI(DOCHOSTUITYPE_BROWSE, this, this, NULL, NULL);
[email protected]1bb5f892009-10-06 01:44:57155 }
[email protected]f7817822009-09-24 05:11:58156 case OLEIVERB_OPEN:
157 case OLEIVERB_UIACTIVATE:
[email protected]bc2ff5192010-06-01 22:05:45158 if (!m_bUIActive)
[email protected]f7817822009-09-24 05:11:58159 return doc_site_->ActivateMe(NULL);
[email protected]f7817822009-09-24 05:11:58160 break;
161 }
162 }
163 return IOleObjectImpl<ChromeActiveDocument>::DoVerb(verb,
164 msg,
165 active_site,
166 index,
167 parent_window,
168 pos);
169}
170
[email protected]f7817822009-09-24 05:11:58171// Override IOleInPlaceActiveObjectImpl::OnDocWindowActivate
172STDMETHODIMP ChromeActiveDocument::OnDocWindowActivate(BOOL activate) {
173 DLOG(INFO) << __FUNCTION__;
174 return S_OK;
175}
176
177STDMETHODIMP ChromeActiveDocument::TranslateAccelerator(MSG* msg) {
178 DLOG(INFO) << __FUNCTION__;
179 if (msg == NULL)
180 return E_POINTER;
181
182 if (msg->message == WM_KEYDOWN && msg->wParam == VK_TAB) {
183 HWND focus = ::GetFocus();
184 if (focus != m_hWnd && !::IsChild(m_hWnd, focus)) {
185 // The call to SetFocus triggers a WM_SETFOCUS that makes the base class
186 // set focus to the correct element in Chrome.
187 ::SetFocus(m_hWnd);
188 return S_OK;
189 }
190 }
191
192 return S_FALSE;
193}
194// Override IPersistStorageImpl::IsDirty
195STDMETHODIMP ChromeActiveDocument::IsDirty() {
196 DLOG(INFO) << __FUNCTION__;
197 return S_FALSE;
198}
199
[email protected]b95f550f2009-11-19 05:35:22200void ChromeActiveDocument::OnAutomationServerReady() {
[email protected]97965e12010-04-09 00:51:10201 BaseActiveX::OnAutomationServerReady();
[email protected]1fd45692010-04-19 21:01:18202 BaseActiveX::GiveFocusToChrome(true);
[email protected]b95f550f2009-11-19 05:35:22203}
204
[email protected]f7817822009-09-24 05:11:58205STDMETHODIMP ChromeActiveDocument::Load(BOOL fully_avalable,
206 IMoniker* moniker_name,
207 LPBC bind_context,
208 DWORD mode) {
[email protected]bc2ff5192010-06-01 22:05:45209 if (NULL == moniker_name)
[email protected]f7817822009-09-24 05:11:58210 return E_INVALIDARG;
[email protected]a1800e82009-11-19 00:53:23211
212 ScopedComPtr<IOleClientSite> client_site;
213 if (bind_context) {
214 ScopedComPtr<IUnknown> site;
215 bind_context->GetObjectParam(SZ_HTML_CLIENTSITE_OBJECTPARAM,
216 site.Receive());
217 if (site)
218 client_site.QueryFrom(site);
219 }
220
221 if (client_site) {
222 SetClientSite(client_site);
[email protected]b1c55638612010-03-08 16:26:11223 DoQueryService(IID_INewWindowManager, client_site,
224 popup_manager_.Receive());
[email protected]051236f2010-03-12 22:06:14225
226 // See if mshtml parsed the html header for us. If so, we need to
227 // clear the browser service flag that we use to indicate that this
228 // browser instance is navigating to a CF document.
229 ScopedComPtr<IBrowserService> browser_service;
230 DoQueryService(SID_SShellBrowser, client_site, browser_service.Receive());
231 if (browser_service) {
232 bool flagged = CheckForCFNavigation(browser_service, true);
233 DLOG_IF(INFO, flagged) << "Cleared flagged browser service";
234 }
[email protected]a1800e82009-11-19 00:53:23235 }
236
[email protected]051236f2010-03-12 22:06:14237 NavigationManager* mgr = NavigationManager::GetThreadInstance();
238 DCHECK(mgr);
[email protected]7e3544b2010-01-22 00:02:34239
[email protected]29c32f902010-04-20 23:27:29240 std::wstring url;
241
[email protected]77d7aee2010-05-14 20:31:55242 ScopedComPtr<BindContextInfo> info;
243 BindContextInfo::FromBindContext(bind_context, info.Receive());
[email protected]29c32f902010-04-20 23:27:29244 DCHECK(info);
245 if (info && !info->url().empty()) {
246 url = info->url();
247 } else {
248 // If the original URL contains an anchor, then the URL queried
249 // from the moniker does not contain the anchor. To workaround
250 // this we retrieve the URL from our BHO.
251 url = GetActualUrlFromMoniker(moniker_name, bind_context,
252 mgr ? mgr->url(): std::wstring());
253 }
[email protected]051236f2010-03-12 22:06:14254
[email protected]37346bc2009-09-25 22:54:33255 // The is_new_navigation variable indicates if this a navigation initiated
256 // by typing in a URL for e.g. in the IE address bar, or from Chrome by
257 // a window.open call from javascript, in which case the current IE tab
258 // will attach to an existing ExternalTabContainer instance.
[email protected]f7817822009-09-24 05:11:58259 bool is_new_navigation = true;
[email protected]37346bc2009-09-25 22:54:33260 bool is_chrome_protocol = false;
[email protected]f7817822009-09-24 05:11:58261
[email protected]37346bc2009-09-25 22:54:33262 if (!ParseUrl(url, &is_new_navigation, &is_chrome_protocol, &url)) {
263 DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
[email protected]f7817822009-09-24 05:11:58264 return E_INVALIDARG;
265 }
266
[email protected]37346bc2009-09-25 22:54:33267 if (!LaunchUrl(url, is_new_navigation)) {
268 NOTREACHED() << __FUNCTION__ << " Failed to launch url:" << url;
269 return E_INVALIDARG;
[email protected]f7817822009-09-24 05:11:58270 }
271
[email protected]bc2ff5192010-06-01 22:05:45272 if (!is_chrome_protocol)
[email protected]0ce46402010-04-08 16:53:57273 url_fetcher_.SetInfoForUrl(url, moniker_name, bind_context);
[email protected]f7817822009-09-24 05:11:58274
[email protected]333590002010-03-05 18:49:21275 THREAD_SAFE_UMA_HISTOGRAM_CUSTOM_COUNTS("ChromeFrame.FullTabLaunchType",
276 is_chrome_protocol, 0, 1, 2);
[email protected]f7817822009-09-24 05:11:58277 return S_OK;
278}
279
280STDMETHODIMP ChromeActiveDocument::Save(IMoniker* moniker_name,
281 LPBC bind_context,
282 BOOL remember) {
283 return E_NOTIMPL;
284}
285
286STDMETHODIMP ChromeActiveDocument::SaveCompleted(IMoniker* moniker_name,
287 LPBC bind_context) {
288 return E_NOTIMPL;
289}
290
291STDMETHODIMP ChromeActiveDocument::GetCurMoniker(IMoniker** moniker_name) {
292 return E_NOTIMPL;
293}
294
295STDMETHODIMP ChromeActiveDocument::GetClassID(CLSID* class_id) {
[email protected]bc2ff5192010-06-01 22:05:45296 if (NULL == class_id)
[email protected]f7817822009-09-24 05:11:58297 return E_POINTER;
[email protected]f7817822009-09-24 05:11:58298 *class_id = GetObjectCLSID();
299 return S_OK;
300}
301
302STDMETHODIMP ChromeActiveDocument::QueryStatus(const GUID* cmd_group_guid,
303 ULONG number_of_commands,
304 OLECMD commands[],
305 OLECMDTEXT* command_text) {
306 DLOG(INFO) << __FUNCTION__;
[email protected]b509ec82010-04-13 16:53:06307 const GUID* supported_groups[] = {
308 &GUID_NULL,
309 &CGID_MSHTML,
310 &CGID_Explorer,
311 };
312
313 bool supported = (cmd_group_guid == NULL);
[email protected]bc2ff5192010-06-01 22:05:45314 for (int i = 0; !supported && i < arraysize(supported_groups); ++i)
[email protected]b509ec82010-04-13 16:53:06315 supported = (IsEqualGUID(*cmd_group_guid, *supported_groups[i]) != FALSE);
[email protected]b509ec82010-04-13 16:53:06316
317 if (!supported) {
318 DLOG(INFO) << "unsupported command group: "
319 << GuidToString(*cmd_group_guid);
320 return OLECMDERR_E_NOTSUPPORTED;
321 }
322
[email protected]f7817822009-09-24 05:11:58323 for (ULONG command_index = 0; command_index < number_of_commands;
324 command_index++) {
325 DLOG(INFO) << "Command id = " << commands[command_index].cmdID;
326 if (enabled_commands_map_.find(commands[command_index].cmdID) !=
[email protected]bc2ff5192010-06-01 22:05:45327 enabled_commands_map_.end())
[email protected]f7817822009-09-24 05:11:58328 commands[command_index].cmdf = OLECMDF_ENABLED;
[email protected]f7817822009-09-24 05:11:58329 }
330 return S_OK;
331}
332
333STDMETHODIMP ChromeActiveDocument::Exec(const GUID* cmd_group_guid,
334 DWORD command_id,
335 DWORD cmd_exec_opt,
336 VARIANT* in_args,
337 VARIANT* out_args) {
338 DLOG(INFO) << __FUNCTION__ << " Cmd id =" << command_id;
339 // Bail out if we have been uninitialized.
340 if (automation_client_.get() && automation_client_->tab()) {
341 return ProcessExecCommand(cmd_group_guid, command_id, cmd_exec_opt,
342 in_args, out_args);
343 }
[email protected]e3200932009-10-09 21:33:03344 return OLECMDERR_E_NOTSUPPORTED;
[email protected]f7817822009-09-24 05:11:58345}
346
[email protected]a1800e82009-11-19 00:53:23347STDMETHODIMP ChromeActiveDocument::LoadHistory(IStream* stream,
348 IBindCtx* bind_context) {
349 // Read notes in ChromeActiveDocument::SaveHistory
350 DCHECK(stream);
351 LARGE_INTEGER offset = {0};
352 ULARGE_INTEGER cur_pos = {0};
353 STATSTG statstg = {0};
354
355 stream->Seek(offset, STREAM_SEEK_CUR, &cur_pos);
356 stream->Stat(&statstg, STATFLAG_NONAME);
357
358 DWORD url_size = statstg.cbSize.LowPart - cur_pos.LowPart;
359 ScopedBstr url_bstr;
360 DWORD bytes_read = 0;
361 stream->Read(url_bstr.AllocateBytes(url_size), url_size, &bytes_read);
362 std::wstring url(url_bstr);
363
364 bool is_new_navigation = true;
365 bool is_chrome_protocol = false;
366
367 if (!ParseUrl(url, &is_new_navigation, &is_chrome_protocol, &url)) {
368 DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
369 return E_INVALIDARG;
370 }
371
372 if (!LaunchUrl(url, is_new_navigation)) {
373 NOTREACHED() << __FUNCTION__ << " Failed to launch url:" << url;
374 return E_INVALIDARG;
375 }
376 return S_OK;
377}
378
379STDMETHODIMP ChromeActiveDocument::SaveHistory(IStream* stream) {
380 // TODO(sanjeevr): We need to fetch the entire list of navigation entries
381 // from Chrome and persist it in the stream. And in LoadHistory we need to
382 // pass this list back to Chrome which will recreate the list. This will allow
383 // Back-Forward navigation to anchors to work correctly when we navigate to a
384 // page outside of ChromeFrame and then come back.
385 if (!stream) {
386 NOTREACHED();
387 return E_INVALIDARG;
388 }
389
390 LARGE_INTEGER offset = {0};
391 ULARGE_INTEGER new_pos = {0};
392 DWORD written = 0;
393 std::wstring url = UTF8ToWide(navigation_info_.url.spec());
394 return stream->Write(url.c_str(), (url.length() + 1) * sizeof(wchar_t),
395 &written);
396}
397
398STDMETHODIMP ChromeActiveDocument::SetPositionCookie(DWORD position_cookie) {
[email protected]77700832010-04-27 00:06:03399 if (automation_client_.get()) {
400 int index = static_cast<int>(position_cookie);
401 navigation_info_.navigation_index = index;
402 automation_client_->NavigateToIndex(index);
403 } else {
404 DLOG(WARNING) << "Invalid automation client instance";
405 }
[email protected]a1800e82009-11-19 00:53:23406 return S_OK;
407}
408
409STDMETHODIMP ChromeActiveDocument::GetPositionCookie(DWORD* position_cookie) {
410 if (!position_cookie)
411 return E_INVALIDARG;
412
413 *position_cookie = navigation_info_.navigation_index;
414 return S_OK;
415}
416
[email protected]f7817822009-09-24 05:11:58417STDMETHODIMP ChromeActiveDocument::GetUrlForEvents(BSTR* url) {
[email protected]bc2ff5192010-06-01 22:05:45418 if (NULL == url)
[email protected]f7817822009-09-24 05:11:58419 return E_POINTER;
[email protected]f7817822009-09-24 05:11:58420 *url = ::SysAllocString(url_);
421 return S_OK;
422}
423
[email protected]a1800e82009-11-19 00:53:23424STDMETHODIMP ChromeActiveDocument::GetAddressBarUrl(BSTR* url) {
425 return GetUrlForEvents(url);
426}
427
[email protected]80b5a8d2010-03-19 16:50:43428STDMETHODIMP ChromeActiveDocument::Reset() {
429 next_privacy_record_ = privacy_info_.privacy_records.begin();
430 return S_OK;
431}
432
433STDMETHODIMP ChromeActiveDocument::GetSize(unsigned long* size) {
434 if (!size)
435 return E_POINTER;
436
437 *size = privacy_info_.privacy_records.size();
438 return S_OK;
439}
440
441STDMETHODIMP ChromeActiveDocument::GetPrivacyImpacted(BOOL* privacy_impacted) {
442 if (!privacy_impacted)
443 return E_POINTER;
444
445 *privacy_impacted = privacy_info_.privacy_impacted;
446 return S_OK;
447}
448
449STDMETHODIMP ChromeActiveDocument::Next(BSTR* url, BSTR* policy,
450 long* reserved, unsigned long* flags) {
451 if (!url || !policy || !flags)
452 return E_POINTER;
453
454 if (next_privacy_record_ == privacy_info_.privacy_records.end())
455 return HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS);
456
457 *url = SysAllocString(next_privacy_record_->first.c_str());
458 *policy = SysAllocString(next_privacy_record_->second.policy_ref.c_str());
459 *flags = next_privacy_record_->second.flags;
460
461 next_privacy_record_++;
462 return S_OK;
463}
464
[email protected]f7817822009-09-24 05:11:58465HRESULT ChromeActiveDocument::IOleObject_SetClientSite(
466 IOleClientSite* client_site) {
467 if (client_site == NULL) {
468 ChromeActiveDocument* cached_document = g_active_doc_cache.Get();
469 if (cached_document) {
470 DCHECK(this == cached_document);
471 g_active_doc_cache.Set(NULL);
472 cached_document->Release();
473 }
[email protected]dda7d9c2009-11-11 23:01:47474
475 ScopedComPtr<IDocHostUIHandler> doc_host_handler;
[email protected]bc2ff5192010-06-01 22:05:45476 if (doc_site_)
[email protected]77059f72010-02-28 06:16:00477 doc_host_handler.QueryFrom(doc_site_);
[email protected]dda7d9c2009-11-11 23:01:47478
[email protected]bc2ff5192010-06-01 22:05:45479 if (doc_host_handler.get())
[email protected]dda7d9c2009-11-11 23:01:47480 doc_host_handler->HideUI();
[email protected]dda7d9c2009-11-11 23:01:47481
482 doc_site_.Release();
483 in_place_frame_.Release();
[email protected]f7817822009-09-24 05:11:58484 }
[email protected]a1800e82009-11-19 00:53:23485
[email protected]bc2ff5192010-06-01 22:05:45486 if (client_site != m_spClientSite)
[email protected]97965e12010-04-09 00:51:10487 return BaseActiveX::IOleObject_SetClientSite(client_site);
[email protected]a1800e82009-11-19 00:53:23488
489 return S_OK;
[email protected]f7817822009-09-24 05:11:58490}
491
[email protected]f7817822009-09-24 05:11:58492HRESULT ChromeActiveDocument::ActiveXDocActivate(LONG verb) {
493 HRESULT hr = S_OK;
494 m_bNegotiatedWnd = TRUE;
495 if (!m_bInPlaceActive) {
496 hr = m_spInPlaceSite->CanInPlaceActivate();
[email protected]bc2ff5192010-06-01 22:05:45497 if (FAILED(hr))
[email protected]f7817822009-09-24 05:11:58498 return hr;
[email protected]f7817822009-09-24 05:11:58499 m_spInPlaceSite->OnInPlaceActivate();
500 }
501 m_bInPlaceActive = TRUE;
502 // get location in the parent window,
503 // as well as some information about the parent
504 ScopedComPtr<IOleInPlaceUIWindow> in_place_ui_window;
505 frame_info_.cb = sizeof(OLEINPLACEFRAMEINFO);
506 HWND parent_window = NULL;
507 if (m_spInPlaceSite->GetWindow(&parent_window) == S_OK) {
508 in_place_frame_.Release();
509 RECT position_rect = {0};
510 RECT clip_rect = {0};
511 m_spInPlaceSite->GetWindowContext(in_place_frame_.Receive(),
512 in_place_ui_window.Receive(),
513 &position_rect,
514 &clip_rect,
515 &frame_info_);
516 if (!m_bWndLess) {
517 if (IsWindow()) {
518 ::ShowWindow(m_hWnd, SW_SHOW);
519 SetFocus();
520 } else {
[email protected]c1d19582010-03-03 01:57:13521 m_hWnd = Create(parent_window, position_rect, 0, 0, WS_EX_CLIENTEDGE);
[email protected]dd4beb522010-07-13 18:18:14522 if (!IsWindow()) {
523 // This might happen if the automation server couldn't be
524 // instantiated. If so, a NOTREACHED() will have already been hit.
525 DLOG(ERROR) << "Failed to create Ax window";
526 return AtlHresultFromLastError();
527 }
[email protected]f7817822009-09-24 05:11:58528 }
[email protected]e3a91e72010-06-17 01:19:04529
530 ScopedComPtr<IWebBrowser2> web_browser2;
531 DoQueryService(SID_SWebBrowserApp, m_spClientSite,
532 web_browser2.Receive());
533 if (web_browser2) {
534 if (!dimensions_.IsEmpty()) {
535 web_browser2->put_Width(dimensions_.width());
536 web_browser2->put_Height(dimensions_.height());
537 web_browser2->put_Left(dimensions_.x());
538 web_browser2->put_Top(dimensions_.y());
539 web_browser2->put_MenuBar(VARIANT_FALSE);
540 web_browser2->put_ToolBar(VARIANT_FALSE);
541
542 dimensions_.set_height(0);
543 dimensions_.set_width(0);
544 }
545 }
[email protected]f7817822009-09-24 05:11:58546 }
547 SetObjectRects(&position_rect, &clip_rect);
548 }
549
550 ScopedComPtr<IOleInPlaceActiveObject> in_place_active_object(this);
551
552 // Gone active by now, take care of UIACTIVATE
553 if (DoesVerbUIActivate(verb)) {
554 if (!m_bUIActive) {
555 m_bUIActive = TRUE;
556 hr = m_spInPlaceSite->OnUIActivate();
[email protected]bc2ff5192010-06-01 22:05:45557 if (FAILED(hr))
[email protected]f7817822009-09-24 05:11:58558 return hr;
[email protected]f7817822009-09-24 05:11:58559 // set ourselves up in the host
560 if (in_place_active_object) {
[email protected]bc2ff5192010-06-01 22:05:45561 if (in_place_frame_)
[email protected]f7817822009-09-24 05:11:58562 in_place_frame_->SetActiveObject(in_place_active_object, NULL);
[email protected]bc2ff5192010-06-01 22:05:45563 if (in_place_ui_window)
[email protected]f7817822009-09-24 05:11:58564 in_place_ui_window->SetActiveObject(in_place_active_object, NULL);
[email protected]f7817822009-09-24 05:11:58565 }
566 }
567 }
568 m_spClientSite->ShowObject();
569 return S_OK;
570}
571
572void ChromeActiveDocument::OnNavigationStateChanged(int tab_handle, int flags,
573 const IPC::NavigationInfo& nav_info) {
574 // TODO(joshia): handle INVALIDATE_TAB,INVALIDATE_LOAD etc.
575 DLOG(INFO) << __FUNCTION__ << std::endl << " Flags: " << flags
576 << "Url: " << nav_info.url <<
577 ", Title: " << nav_info.title <<
578 ", Type: " << nav_info.navigation_type << ", Relative Offset: " <<
[email protected]62bb18dc12009-11-25 01:34:08579 nav_info.relative_offset << ", Index: " << nav_info.navigation_index;
[email protected]f7817822009-09-24 05:11:58580
581 UpdateNavigationState(nav_info);
582}
583
584void ChromeActiveDocument::OnUpdateTargetUrl(int tab_handle,
585 const std::wstring& new_target_url) {
[email protected]bc2ff5192010-06-01 22:05:45586 if (in_place_frame_)
[email protected]f7817822009-09-24 05:11:58587 in_place_frame_->SetStatusText(new_target_url.c_str());
[email protected]f7817822009-09-24 05:11:58588}
589
590bool IsFindAccelerator(const MSG& msg) {
591 // TODO(robertshield): This may not stand up to localization. Fix if this
592 // is the case.
593 return msg.message == WM_KEYDOWN && msg.wParam == 'F' &&
594 win_util::IsCtrlPressed() &&
595 !(win_util::IsAltPressed() || win_util::IsShiftPressed());
596}
597
598void ChromeActiveDocument::OnAcceleratorPressed(int tab_handle,
599 const MSG& accel_message) {
[email protected]a15d4a42010-02-17 08:21:35600 if (::TranslateAccelerator(m_hWnd, accelerator_table_,
601 const_cast<MSG*>(&accel_message)))
602 return;
603
[email protected]f7817822009-09-24 05:11:58604 bool handled_accel = false;
605 if (in_place_frame_ != NULL) {
606 handled_accel = (S_OK == in_place_frame_->TranslateAcceleratorW(
607 const_cast<MSG*>(&accel_message), 0));
608 }
609
610 if (!handled_accel) {
611 if (IsFindAccelerator(accel_message)) {
612 // Handle the showing of the find dialog explicitly.
613 OnFindInPage();
[email protected]2b8fd322009-10-02 00:00:59614 } else {
[email protected]97965e12010-04-09 00:51:10615 BaseActiveX::OnAcceleratorPressed(tab_handle, accel_message);
[email protected]f7817822009-09-24 05:11:58616 }
617 } else {
618 DLOG(INFO) << "IE handled accel key " << accel_message.wParam;
619 }
620}
621
622void ChromeActiveDocument::OnTabbedOut(int tab_handle, bool reverse) {
623 DLOG(INFO) << __FUNCTION__;
624 if (in_place_frame_) {
625 MSG msg = { NULL, WM_KEYDOWN, VK_TAB };
626 in_place_frame_->TranslateAcceleratorW(&msg, 0);
627 }
628}
629
630void ChromeActiveDocument::OnDidNavigate(int tab_handle,
631 const IPC::NavigationInfo& nav_info) {
632 DLOG(INFO) << __FUNCTION__ << std::endl << "Url: " << nav_info.url <<
633 ", Title: " << nav_info.title <<
634 ", Type: " << nav_info.navigation_type << ", Relative Offset: " <<
635 nav_info.relative_offset << ", Index: " << nav_info.navigation_index;
636
[email protected]897b26272010-06-11 02:23:44637 CrashMetricsReporter::GetInstance()->IncrementMetric(
638 CrashMetricsReporter::CHROME_FRAME_NAVIGATION_COUNT);
639
[email protected]f7817822009-09-24 05:11:58640 // This could be NULL if the active document instance is being destroyed.
641 if (!m_spInPlaceSite) {
642 DLOG(INFO) << __FUNCTION__ << "m_spInPlaceSite is NULL. Returning";
643 return;
644 }
645
646 UpdateNavigationState(nav_info);
647}
648
[email protected]e16dd1672010-06-07 21:40:29649void ChromeActiveDocument::OnCloseTab(int tab_handle) {
650 ScopedComPtr<IWebBrowser2> web_browser2;
651 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
652 if (web_browser2)
653 web_browser2->Quit();
654}
655
[email protected]f7817822009-09-24 05:11:58656void ChromeActiveDocument::UpdateNavigationState(
657 const IPC::NavigationInfo& new_navigation_info) {
[email protected]a1800e82009-11-19 00:53:23658 HRESULT hr = S_OK;
[email protected]f7817822009-09-24 05:11:58659 bool is_title_changed = (navigation_info_.title != new_navigation_info.title);
[email protected]f7817822009-09-24 05:11:58660 bool is_ssl_state_changed =
661 (navigation_info_.security_style != new_navigation_info.security_style) ||
[email protected]b4e75c12010-05-18 18:28:48662 (navigation_info_.displayed_insecure_content !=
663 new_navigation_info.displayed_insecure_content) ||
664 (navigation_info_.ran_insecure_content !=
665 new_navigation_info.ran_insecure_content);
[email protected]f7817822009-09-24 05:11:58666
[email protected]f7817822009-09-24 05:11:58667 if (is_ssl_state_changed) {
668 int lock_status = SECURELOCK_SET_UNSECURE;
[email protected]a1800e82009-11-19 00:53:23669 switch (new_navigation_info.security_style) {
[email protected]f7817822009-09-24 05:11:58670 case SECURITY_STYLE_AUTHENTICATION_BROKEN:
671 lock_status = SECURELOCK_SET_SECUREUNKNOWNBIT;
672 break;
673 case SECURITY_STYLE_AUTHENTICATED:
[email protected]b4e75c12010-05-18 18:28:48674 lock_status = new_navigation_info.displayed_insecure_content ?
[email protected]f7817822009-09-24 05:11:58675 SECURELOCK_SET_MIXED : SECURELOCK_SET_SECUREUNKNOWNBIT;
676 break;
677 default:
678 break;
679 }
680
681 ScopedVariant secure_lock_status(lock_status);
682 IEExec(&CGID_ShellDocView, INTERNAL_CMDID_SET_SSL_LOCK,
683 OLECMDEXECOPT_DODEFAULT, secure_lock_status.AsInput(), NULL);
684 }
685
[email protected]a1800e82009-11-19 00:53:23686 // Ideally all navigations should come to Chrome Frame so that we can call
687 // BeforeNavigate2 on installed BHOs and give them a chance to cancel the
688 // navigation. However, in practice what happens is as below:
689 // The very first navigation that happens in CF happens via a Load or a
690 // LoadHistory call. In this case, IE already has the correct information for
691 // its travel log as well address bar. For other internal navigations (navs
692 // that only happen within Chrome such as anchor navigations) we need to
693 // update IE's internal state after the fact. In the case of internal
694 // navigations, we notify the BHOs but ignore the should_cancel flag.
[email protected]6f526082010-01-28 19:36:58695
696 // Another case where we need to issue BeforeNavigate2 calls is as below:-
697 // We get notified after the fact, when navigations are initiated within
698 // Chrome via window.open calls. These navigations are handled by creating
699 // an external tab container within chrome and then connecting to it from IE.
700 // We still want to update the address bar/history, etc, to ensure that
701 // the special URL used by Chrome to indicate this is updated correctly.
702 bool is_internal_navigation = ((new_navigation_info.navigation_index > 0) &&
[email protected]a1800e82009-11-19 00:53:23703 (new_navigation_info.navigation_index !=
[email protected]6f526082010-01-28 19:36:58704 navigation_info_.navigation_index)) ||
705 StartsWith(static_cast<BSTR>(url_), kChromeAttachExternalTabPrefix,
706 false);
[email protected]f7817822009-09-24 05:11:58707
[email protected]bc2ff5192010-06-01 22:05:45708 if (new_navigation_info.url.is_valid())
[email protected]38939de2010-05-13 02:32:06709 url_.Allocate(UTF8ToWide(new_navigation_info.url.spec()).c_str());
[email protected]38939de2010-05-13 02:32:06710
[email protected]a1800e82009-11-19 00:53:23711 if (is_internal_navigation) {
712 ScopedComPtr<IDocObjectService> doc_object_svc;
[email protected]f7817822009-09-24 05:11:58713 ScopedComPtr<IWebBrowserEventsService> web_browser_events_svc;
[email protected]ae33d672010-03-04 21:58:06714
[email protected]f7817822009-09-24 05:11:58715 DoQueryService(__uuidof(web_browser_events_svc), m_spClientSite,
716 web_browser_events_svc.Receive());
[email protected]ae33d672010-03-04 21:58:06717
718 if (!web_browser_events_svc.get()) {
719 DoQueryService(SID_SShellBrowser, m_spClientSite,
720 doc_object_svc.Receive());
721 }
722
[email protected]62bb18dc12009-11-25 01:34:08723 // web_browser_events_svc can be NULL on IE6.
[email protected]f7817822009-09-24 05:11:58724 if (web_browser_events_svc) {
[email protected]f7817822009-09-24 05:11:58725 VARIANT_BOOL should_cancel = VARIANT_FALSE;
726 web_browser_events_svc->FireBeforeNavigate2Event(&should_cancel);
[email protected]a1800e82009-11-19 00:53:23727 }
728
729 // We need to tell IE that we support navigation so that IE will query us
730 // for IPersistHistory and call GetPositionCookie to save our navigation
731 // index.
732 ScopedVariant html_window(static_cast<IUnknown*>(
733 static_cast<IHTMLWindow2*>(this)));
734 IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0,
735 html_window.AsInput(), NULL);
736
737 // We pass the HLNF_INTERNALJUMP flag to INTERNAL_CMDID_FINALIZE_TRAVEL_LOG
738 // since we want to make IE treat all internal navigations within this page
739 // (including anchor navigations and subframe navigations) as anchor
740 // navigations. This will ensure that IE calls GetPositionCookie
741 // to save the current position cookie in the travel log and then call
742 // SetPositionCookie when the user hits Back/Forward to come back here.
743 ScopedVariant internal_navigation(HLNF_INTERNALJUMP);
744 IEExec(&CGID_Explorer, INTERNAL_CMDID_FINALIZE_TRAVEL_LOG, 0,
745 internal_navigation.AsInput(), NULL);
746
747 // We no longer need to lie to IE. If we lie persistently to IE, then
748 // IE reuses us for new navigations.
749 IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0, NULL, NULL);
750
751 if (doc_object_svc) {
752 // Now call the FireNavigateCompleteEvent which makes IE update the text
753 // in the address-bar.
754 doc_object_svc->FireNavigateComplete2(this, 0);
755 doc_object_svc->FireDocumentComplete(this, 0);
756 } else if (web_browser_events_svc) {
[email protected]f7817822009-09-24 05:11:58757 web_browser_events_svc->FireNavigateComplete2Event();
[email protected]a1800e82009-11-19 00:53:23758 web_browser_events_svc->FireDocumentCompleteEvent();
[email protected]f7817822009-09-24 05:11:58759 }
760 }
[email protected]37346bc2009-09-25 22:54:33761
[email protected]a1800e82009-11-19 00:53:23762 if (is_title_changed) {
763 ScopedVariant title(new_navigation_info.title.c_str());
764 IEExec(NULL, OLECMDID_SETTITLE, OLECMDEXECOPT_DONTPROMPTUSER,
765 title.AsInput(), NULL);
766 }
767
768 // It is important that we only update the navigation_info_ after we have
769 // finalized the travel log. This is because IE will ask for information
770 // such as navigation index when the travel log is finalized and we need
771 // supply the old index and not the new one.
772 navigation_info_ = new_navigation_info;
[email protected]37346bc2009-09-25 22:54:33773 // Update the IE zone here. Ideally we would like to do it when the active
774 // document is activated. However that does not work at times as the frame we
775 // get there is not the actual frame which handles the command.
776 IEExec(&CGID_Explorer, SBCMDID_MIXEDZONE, 0, NULL, NULL);
[email protected]f7817822009-09-24 05:11:58777}
778
779void ChromeActiveDocument::OnFindInPage() {
780 TabProxy* tab = GetTabProxy();
781 if (tab) {
[email protected]bc2ff5192010-06-01 22:05:45782 if (!find_dialog_.IsWindow())
[email protected]f7817822009-09-24 05:11:58783 find_dialog_.Create(m_hWnd);
[email protected]f7817822009-09-24 05:11:58784
785 find_dialog_.ShowWindow(SW_SHOW);
786 }
787}
788
789void ChromeActiveDocument::OnViewSource() {
790 DCHECK(navigation_info_.url.is_valid());
[email protected]76e7da22010-06-18 22:44:49791 HostNavigate(GURL(chrome::kViewSourceScheme + std::string(":") +
792 navigation_info_.url.spec()), GURL(), NEW_WINDOW);
[email protected]f7817822009-09-24 05:11:58793}
794
[email protected]37346bc2009-09-25 22:54:33795void ChromeActiveDocument::OnDetermineSecurityZone(const GUID* cmd_group_guid,
796 DWORD command_id,
797 DWORD cmd_exec_opt,
798 VARIANT* in_args,
799 VARIANT* out_args) {
[email protected]7c712c92010-03-24 17:29:22800 // Always return URLZONE_INTERNET since that is the Chrome's behaviour.
801 // Correct step is to use MapUrlToZone().
[email protected]37346bc2009-09-25 22:54:33802 if (out_args != NULL) {
803 out_args->vt = VT_UI4;
804 out_args->ulVal = URLZONE_INTERNET;
805 }
806}
807
[email protected]80b5a8d2010-03-19 16:50:43808void ChromeActiveDocument::OnDisplayPrivacyInfo() {
809 privacy_info_ = url_fetcher_.privacy_info();
810 Reset();
811 DoPrivacyDlg(m_hWnd, url_, this, TRUE);
812}
813
[email protected]b36a9f92009-10-19 17:34:57814void ChromeActiveDocument::OnOpenURL(int tab_handle,
815 const GURL& url_to_open,
816 const GURL& referrer,
[email protected]f7817822009-09-24 05:11:58817 int open_disposition) {
818 // If the disposition indicates that we should be opening the URL in the
819 // current tab, then we can reuse the ChromeFrameAutomationClient instance
820 // maintained by the current ChromeActiveDocument instance. We cache this
821 // instance so that it can be used by the new ChromeActiveDocument instance
822 // which may be instantiated for handling the new URL.
823 if (open_disposition == CURRENT_TAB) {
824 // Grab a reference to ensure that the document remains valid.
825 AddRef();
826 g_active_doc_cache.Set(this);
827 }
828
[email protected]97965e12010-04-09 00:51:10829 BaseActiveX::OnOpenURL(tab_handle, url_to_open, referrer, open_disposition);
[email protected]f7817822009-09-24 05:11:58830}
831
[email protected]b1c55638612010-03-08 16:26:11832void ChromeActiveDocument::OnAttachExternalTab(int tab_handle,
833 const IPC::AttachExternalTabParams& params) {
[email protected]77700832010-04-27 00:06:03834 if (!automation_client_.get()) {
835 DLOG(WARNING) << "Invalid automation client instance";
836 return;
837 }
[email protected]b1c55638612010-03-08 16:26:11838 DWORD flags = 0;
839 if (params.user_gesture)
840 flags = NWMF_USERREQUESTED;
[email protected]355309e2010-03-15 17:01:34841 else if (popup_allowed_)
842 flags = NWMF_USERALLOWED;
[email protected]b1c55638612010-03-08 16:26:11843
844 HRESULT hr = S_OK;
845 if (popup_manager_) {
[email protected]355309e2010-03-15 17:01:34846 LPCWSTR popup_wnd_url = UTF8ToWide(params.url.spec()).c_str();
847 hr = popup_manager_->EvaluateNewWindow(popup_wnd_url, NULL, url_,
848 NULL, FALSE, flags, 0);
[email protected]b1c55638612010-03-08 16:26:11849 }
850 // Allow popup
851 if (hr == S_OK) {
[email protected]97965e12010-04-09 00:51:10852 BaseActiveX::OnAttachExternalTab(tab_handle, params);
[email protected]b1c55638612010-03-08 16:26:11853 return;
854 }
855
856 automation_client_->BlockExternalTab(params.cookie);
857}
858
[email protected]f7817822009-09-24 05:11:58859bool ChromeActiveDocument::PreProcessContextMenu(HMENU menu) {
860 ScopedComPtr<IBrowserService> browser_service;
861 ScopedComPtr<ITravelLog> travel_log;
[email protected]a1800e82009-11-19 00:53:23862 GetBrowserServiceAndTravelLog(browser_service.Receive(),
863 travel_log.Receive());
864 if (!browser_service || !travel_log)
[email protected]f7817822009-09-24 05:11:58865 return true;
866
[email protected]bc2ff5192010-06-01 22:05:45867 EnableMenuItem(menu, IDS_CONTENT_CONTEXT_BACK, MF_BYCOMMAND |
868 (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_BACK, NULL)) ?
869 MF_ENABLED : MF_DISABLED));
870 EnableMenuItem(menu, IDS_CONTENT_CONTEXT_FORWARD, MF_BYCOMMAND |
871 (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_FORE, NULL)) ?
872 MF_ENABLED : MF_DISABLED));
[email protected]f7817822009-09-24 05:11:58873
874 // Call base class (adds 'About' item)
[email protected]97965e12010-04-09 00:51:10875 return BaseActiveX::PreProcessContextMenu(menu);
[email protected]f7817822009-09-24 05:11:58876}
877
[email protected]35f13ab2009-12-16 23:59:17878bool ChromeActiveDocument::HandleContextMenuCommand(UINT cmd,
879 const IPC::ContextMenuParams& params) {
[email protected]f7817822009-09-24 05:11:58880 ScopedComPtr<IWebBrowser2> web_browser2;
881 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
882
[email protected]074283a2010-05-28 23:47:59883 if (cmd == IDC_BACK)
[email protected]b731a142010-05-14 00:03:03884 web_browser2->GoBack();
[email protected]074283a2010-05-28 23:47:59885 else if (cmd == IDC_FORWARD)
[email protected]b731a142010-05-14 00:03:03886 web_browser2->GoForward();
[email protected]074283a2010-05-28 23:47:59887 else if (cmd == IDC_RELOAD)
[email protected]b731a142010-05-14 00:03:03888 web_browser2->Refresh();
[email protected]074283a2010-05-28 23:47:59889 else
[email protected]b731a142010-05-14 00:03:03890 return BaseActiveX::HandleContextMenuCommand(cmd, params);
[email protected]f7817822009-09-24 05:11:58891
892 return true;
893}
894
895HRESULT ChromeActiveDocument::IEExec(const GUID* cmd_group_guid,
896 DWORD command_id, DWORD cmd_exec_opt,
897 VARIANT* in_args, VARIANT* out_args) {
898 HRESULT hr = E_FAIL;
[email protected]37346bc2009-09-25 22:54:33899
[email protected]f7817822009-09-24 05:11:58900 ScopedComPtr<IOleCommandTarget> frame_cmd_target;
[email protected]37346bc2009-09-25 22:54:33901
902 ScopedComPtr<IOleInPlaceSite> in_place_site(m_spInPlaceSite);
[email protected]bc2ff5192010-06-01 22:05:45903 if (!in_place_site.get() && m_spClientSite != NULL)
[email protected]37346bc2009-09-25 22:54:33904 in_place_site.QueryFrom(m_spClientSite);
[email protected]37346bc2009-09-25 22:54:33905
906 if (in_place_site)
907 hr = frame_cmd_target.QueryFrom(in_place_site);
[email protected]f7817822009-09-24 05:11:58908
[email protected]bc2ff5192010-06-01 22:05:45909 if (frame_cmd_target) {
[email protected]f7817822009-09-24 05:11:58910 hr = frame_cmd_target->Exec(cmd_group_guid, command_id, cmd_exec_opt,
911 in_args, out_args);
[email protected]bc2ff5192010-06-01 22:05:45912 }
[email protected]f7817822009-09-24 05:11:58913
914 return hr;
915}
[email protected]37346bc2009-09-25 22:54:33916
[email protected]7c712c92010-03-24 17:29:22917unsigned long ChromeActiveDocument::MapUrlToZone(const wchar_t* url) {
918 unsigned long zone = URLZONE_INVALID;
[email protected]37346bc2009-09-25 22:54:33919 if (security_manager_.get() == NULL) {
920 HRESULT hr = CoCreateInstance(
921 CLSID_InternetSecurityManager,
922 NULL,
923 CLSCTX_ALL,
924 IID_IInternetSecurityManager,
925 reinterpret_cast<void**>(security_manager_.Receive()));
926
927 if (FAILED(hr)) {
928 NOTREACHED() << __FUNCTION__
929 << " Failed to create InternetSecurityManager. Error: 0x%x"
930 << hr;
[email protected]7c712c92010-03-24 17:29:22931 return zone;
[email protected]37346bc2009-09-25 22:54:33932 }
933 }
934
[email protected]7c712c92010-03-24 17:29:22935 security_manager_->MapUrlToZone(url, &zone, 0);
936 return zone;
[email protected]37346bc2009-09-25 22:54:33937}
938
939bool ChromeActiveDocument::ParseUrl(const std::wstring& url,
940 bool* is_new_navigation,
941 bool* is_chrome_protocol,
942 std::wstring* parsed_url) {
943 if (!is_new_navigation || !is_chrome_protocol|| !parsed_url) {
944 NOTREACHED() << __FUNCTION__ << " Invalid arguments";
945 return false;
946 }
947
948 std::wstring initial_url = url;
949
950 *is_chrome_protocol = StartsWith(initial_url, kChromeProtocolPrefix,
951 false);
952
953 *is_new_navigation = true;
954
955 if (*is_chrome_protocol) {
956 initial_url.erase(0, lstrlen(kChromeProtocolPrefix));
957 *is_new_navigation =
958 !StartsWith(initial_url, kChromeAttachExternalTabPrefix, false);
959 }
960
[email protected]10bf1a72009-10-01 16:00:21961 if (!IsValidUrlScheme(initial_url, is_privileged_)) {
[email protected]37346bc2009-09-25 22:54:33962 DLOG(WARNING) << __FUNCTION__ << " Disallowing navigation to url: "
963 << url;
964 return false;
965 }
966
[email protected]7c712c92010-03-24 17:29:22967 if (URLZONE_UNTRUSTED == MapUrlToZone(initial_url.c_str())) {
[email protected]37346bc2009-09-25 22:54:33968 DLOG(WARNING) << __FUNCTION__
969 << " Disallowing navigation to restricted url: "
970 << initial_url;
971 return false;
972 }
973
[email protected]552fb6012010-02-03 17:24:29974 if (*is_chrome_protocol) {
975 // Allow chrome protocol (gcf:) if -
976 // - explicitly enabled using registry
977 // - for gcf:attach_external_tab
978 // - for gcf:about and gcf:view-source
979 GURL crack_url(initial_url);
980 bool allow_gcf_protocol = !*is_new_navigation ||
981 GetConfigBool(false, kEnableGCFProtocol) ||
982 crack_url.SchemeIs(chrome::kAboutScheme) ||
983 crack_url.SchemeIs(chrome::kViewSourceScheme);
984 if (!allow_gcf_protocol)
985 return false;
986 }
987
[email protected]37346bc2009-09-25 22:54:33988 *parsed_url = initial_url;
989 return true;
990}
991
992bool ChromeActiveDocument::LaunchUrl(const std::wstring& url,
993 bool is_new_navigation) {
[email protected]77700832010-04-27 00:06:03994 DCHECK(automation_client_.get() != NULL);
995
[email protected]b509ec82010-04-13 16:53:06996 url_.Allocate(url.c_str());
[email protected]6f526082010-01-28 19:36:58997
[email protected]e150e822010-06-03 23:10:55998 std::string referrer;
999 std::string utf8_url;
1000
[email protected]37346bc2009-09-25 22:54:331001 if (!is_new_navigation) {
[email protected]e3a91e72010-06-17 01:19:041002 int disposition = 0;
[email protected]b1c55638612010-03-08 16:26:111003 uint64 external_tab_cookie = 0;
[email protected]e3a91e72010-06-17 01:19:041004
1005 if (!ParseAttachExternalTabUrl(url, &external_tab_cookie, &dimensions_,
1006 &disposition)) {
1007 NOTREACHED() << "Failed to parse attach tab url:" << url;
1008 return false;
[email protected]b1c55638612010-03-08 16:26:111009 }
[email protected]37346bc2009-09-25 22:54:331010
1011 if (external_tab_cookie == 0) {
1012 NOTREACHED() << "invalid url for attach tab: " << url;
1013 return false;
1014 }
1015
[email protected]e3a91e72010-06-17 01:19:041016 is_new_navigation_ = false;
[email protected]37346bc2009-09-25 22:54:331017 automation_client_->AttachExternalTab(external_tab_cookie);
1018 } else {
[email protected]e3a91e72010-06-17 01:19:041019 is_new_navigation_ = true;
[email protected]37346bc2009-09-25 22:54:331020 // Initiate navigation before launching chrome so that the url will be
1021 // cached and sent with launch settings.
[email protected]62bb18dc12009-11-25 01:34:081022 if (url_.Length()) {
[email protected]62bb18dc12009-11-25 01:34:081023 WideToUTF8(url_, url_.Length(), &utf8_url);
[email protected]b36a9f92009-10-19 17:34:571024
[email protected]051236f2010-03-12 22:06:141025 NavigationManager* mgr = NavigationManager::GetThreadInstance();
1026 if (mgr)
1027 referrer = mgr->referrer();
[email protected]a1800e82009-11-19 00:53:231028
[email protected]62bb18dc12009-11-25 01:34:081029 if (!automation_client_->InitiateNavigation(utf8_url,
1030 referrer,
1031 is_privileged_)) {
1032 DLOG(ERROR) << "Invalid URL: " << url;
1033 Error(L"Invalid URL");
1034 url_.Reset();
1035 return false;
[email protected]37346bc2009-09-25 22:54:331036 }
[email protected]62bb18dc12009-11-25 01:34:081037
1038 DLOG(INFO) << "Url is " << url_;
[email protected]37346bc2009-09-25 22:54:331039 }
1040 }
1041
[email protected]3eb07da2010-02-01 19:48:361042 if (is_automation_client_reused_)
1043 return true;
[email protected]37346bc2009-09-25 22:54:331044
[email protected]3eb07da2010-02-01 19:48:361045 automation_client_->SetUrlFetcher(&url_fetcher_);
1046
[email protected]e150e822010-06-03 23:10:551047 return InitializeAutomation(GetHostProcessName(false), L"", IsIEInPrivate(),
1048 false, GURL(utf8_url), GURL(referrer));
[email protected]37346bc2009-09-25 22:54:331049}
[email protected]1bb5f892009-10-06 01:44:571050
[email protected]355309e2010-03-15 17:01:341051
1052HRESULT ChromeActiveDocument::OnRefreshPage(const GUID* cmd_group_guid,
1053 DWORD command_id, DWORD cmd_exec_opt, VARIANT* in_args, VARIANT* out_args) {
1054 popup_allowed_ = false;
1055 if (in_args->vt == VT_I4 &&
1056 in_args->lVal & OLECMDIDF_REFRESH_PAGEACTION_POPUPWINDOW) {
1057 popup_allowed_ = true;
1058
1059 // Ask the yellow security band to change the text and icon and to remain
1060 // visible.
1061 IEExec(&CGID_DocHostCommandHandler, OLECMDID_PAGEACTIONBLOCKED,
1062 0x80000000 | OLECMDIDF_WINDOWSTATE_USERVISIBLE_VALID, NULL, NULL);
1063 }
1064
1065 TabProxy* tab_proxy = GetTabProxy();
1066 if (tab_proxy)
1067 tab_proxy->ReloadAsync();
1068
1069 return S_OK;
1070}
1071
1072
[email protected]1bb5f892009-10-06 01:44:571073HRESULT ChromeActiveDocument::SetPageFontSize(const GUID* cmd_group_guid,
1074 DWORD command_id,
1075 DWORD cmd_exec_opt,
1076 VARIANT* in_args,
1077 VARIANT* out_args) {
1078 if (!automation_client_.get()) {
[email protected]77700832010-04-27 00:06:031079 NOTREACHED() << "Invalid automation client";
[email protected]1bb5f892009-10-06 01:44:571080 return E_FAIL;
1081 }
1082
1083 switch (command_id) {
1084 case IDM_BASELINEFONT1:
1085 automation_client_->SetPageFontSize(SMALLEST_FONT);
1086 break;
1087
1088 case IDM_BASELINEFONT2:
1089 automation_client_->SetPageFontSize(SMALL_FONT);
1090 break;
1091
1092 case IDM_BASELINEFONT3:
1093 automation_client_->SetPageFontSize(MEDIUM_FONT);
1094 break;
1095
1096 case IDM_BASELINEFONT4:
1097 automation_client_->SetPageFontSize(LARGE_FONT);
1098 break;
1099
1100 case IDM_BASELINEFONT5:
1101 automation_client_->SetPageFontSize(LARGEST_FONT);
1102 break;
1103
1104 default:
1105 NOTREACHED() << "Invalid font size command: "
1106 << command_id;
1107 return E_FAIL;
1108 }
1109
1110 // Forward the command back to IEFrame with group set to
1111 // CGID_ExplorerBarDoc. This is probably needed to update the menu state to
1112 // indicate that the font size was set. This currently fails with error
1113 // 0x80040104.
1114 // TODO(iyengar)
1115 // Do some investigation into why this Exec call fails.
1116 IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
1117 return S_OK;
1118}
1119
[email protected]2f2afba2010-04-01 01:53:191120HRESULT ChromeActiveDocument::OnEncodingChange(const GUID* cmd_group_guid,
1121 DWORD command_id,
1122 DWORD cmd_exec_opt,
1123 VARIANT* in_args,
1124 VARIANT* out_args) {
1125 const struct EncodingMapData {
1126 DWORD ie_encoding_id;
1127 const char* chrome_encoding_name;
1128 } kEncodingTestDatas[] = {
1129#define DEFINE_ENCODING_MAP(encoding_name, id, chrome_name) \
1130 { encoding_name, chrome_name },
1131 INTERNAL_IE_ENCODINGMENU_IDS(DEFINE_ENCODING_MAP)
1132#undef DEFINE_ENCODING_MAP
1133 };
1134
1135 if (!automation_client_.get()) {
1136 NOTREACHED() << "Invalid automtion client";
1137 return E_FAIL;
1138 }
1139
[email protected]e9b35282010-06-02 16:43:191140 // Using ARRAYSIZE_UNSAFE in here is because we define the struct
1141 // EncodingMapData inside function.
[email protected]2f2afba2010-04-01 01:53:191142 const char* chrome_encoding_name = NULL;
1143 for (int i = 0; i < ARRAYSIZE_UNSAFE(kEncodingTestDatas); ++i) {
1144 const struct EncodingMapData* encoding_data = &kEncodingTestDatas[i];
1145 if (command_id == encoding_data->ie_encoding_id) {
1146 chrome_encoding_name = encoding_data->chrome_encoding_name;
1147 break;
1148 }
1149 }
1150 // Return E_FAIL when encountering invalid encoding id.
1151 if (!chrome_encoding_name)
1152 return E_FAIL;
1153
1154 TabProxy* tab = GetTabProxy();
1155 if (!tab) {
1156 NOTREACHED() << "Can not get TabProxy";
1157 return E_FAIL;
1158 }
1159
1160 if (chrome_encoding_name)
1161 tab->OverrideEncoding(chrome_encoding_name);
1162
1163 // Like we did on SetPageFontSize, we may forward the command back to IEFrame
1164 // to update the menu state to indicate that which encoding was set.
1165 // TODO(iyengar)
1166 // Do some investigation into why this Exec call fails.
1167 IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
1168 return S_OK;
1169}
1170
[email protected]f9cc4c452009-10-13 14:56:381171void ChromeActiveDocument::OnGoToHistoryEntryOffset(int tab_handle,
1172 int offset) {
[email protected]a1800e82009-11-19 00:53:231173 DLOG(INFO) << __FUNCTION__ << " - offset:" << offset;
1174
[email protected]f9cc4c452009-10-13 14:56:381175 ScopedComPtr<IBrowserService> browser_service;
[email protected]a1800e82009-11-19 00:53:231176 ScopedComPtr<ITravelLog> travel_log;
1177 GetBrowserServiceAndTravelLog(browser_service.Receive(),
1178 travel_log.Receive());
1179
1180 if (browser_service && travel_log)
1181 travel_log->Travel(browser_service, offset);
1182}
1183
1184HRESULT ChromeActiveDocument::GetBrowserServiceAndTravelLog(
1185 IBrowserService** browser_service, ITravelLog** travel_log) {
1186 DCHECK(browser_service || travel_log);
1187 ScopedComPtr<IBrowserService> browser_service_local;
1188 HRESULT hr = DoQueryService(SID_SShellBrowser, m_spClientSite,
1189 browser_service_local.Receive());
1190 if (!browser_service_local) {
1191 NOTREACHED() << "DoQueryService for IBrowserService failed: " << hr;
1192 return hr;
[email protected]f9cc4c452009-10-13 14:56:381193 }
[email protected]a1800e82009-11-19 00:53:231194
1195 if (travel_log) {
1196 hr = browser_service_local->GetTravelLog(travel_log);
[email protected]bc2ff5192010-06-01 22:05:451197 DLOG_IF(INFO, !travel_log) << "browser_service->GetTravelLog failed: " <<
1198 hr;
[email protected]a1800e82009-11-19 00:53:231199 }
1200
1201 if (browser_service)
1202 *browser_service = browser_service_local.Detach();
1203
1204 return hr;
[email protected]f9cc4c452009-10-13 14:56:381205}
[email protected]a15d4a42010-02-17 08:21:351206
1207LRESULT ChromeActiveDocument::OnForward(WORD notify_code, WORD id,
1208 HWND control_window,
1209 BOOL& bHandled) {
1210 ScopedComPtr<IWebBrowser2> web_browser2;
1211 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
1212 DCHECK(web_browser2);
1213
[email protected]bc2ff5192010-06-01 22:05:451214 if (web_browser2)
[email protected]a15d4a42010-02-17 08:21:351215 web_browser2->GoForward();
[email protected]a15d4a42010-02-17 08:21:351216 return 0;
1217}
1218
1219LRESULT ChromeActiveDocument::OnBack(WORD notify_code, WORD id,
1220 HWND control_window,
1221 BOOL& bHandled) {
1222 ScopedComPtr<IWebBrowser2> web_browser2;
1223 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
1224 DCHECK(web_browser2);
1225
[email protected]bc2ff5192010-06-01 22:05:451226 if (web_browser2)
[email protected]a15d4a42010-02-17 08:21:351227 web_browser2->GoBack();
[email protected]a15d4a42010-02-17 08:21:351228 return 0;
1229}
1230
[email protected]80b5a8d2010-03-19 16:50:431231LRESULT ChromeActiveDocument::OnFirePrivacyChange(UINT message, WPARAM wparam,
1232 LPARAM lparam,
1233 BOOL& handled) {
1234 if (!m_spClientSite)
1235 return 0;
1236
1237 ScopedComPtr<IWebBrowser2> web_browser2;
1238 DoQueryService(SID_SWebBrowserApp, m_spClientSite,
1239 web_browser2.Receive());
1240 if (!web_browser2) {
1241 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
1242 return 0;
1243 }
1244
1245 ScopedComPtr<IShellBrowser> shell_browser;
1246 DoQueryService(SID_STopLevelBrowser, web_browser2,
1247 shell_browser.Receive());
1248 DCHECK(shell_browser.get() != NULL);
1249 ScopedComPtr<ITridentService2> trident_services;
1250 trident_services.QueryFrom(shell_browser);
[email protected]bc2ff5192010-06-01 22:05:451251 if (trident_services)
[email protected]80b5a8d2010-03-19 16:50:431252 trident_services->FirePrivacyImpactedStateChange(wparam);
[email protected]bc2ff5192010-06-01 22:05:451253 else
[email protected]80b5a8d2010-03-19 16:50:431254 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
[email protected]80b5a8d2010-03-19 16:50:431255 return 0;
1256}