blob: 143a62bbd209e87f24bc043c71c9ac6b0331c288 [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]f7817822009-09-24 05:11:5841#include "chrome_frame/utils.h"
42
43const wchar_t kChromeAttachExternalTabPrefix[] = L"attach_external_tab";
44
45static const wchar_t kUseChromeNetworking[] = L"UseChromeNetworking";
[email protected]552fb6012010-02-03 17:24:2946static const wchar_t kHandleTopLevelRequests[] = L"HandleTopLevelRequests";
[email protected]f7817822009-09-24 05:11:5847
[email protected]a1800e82009-11-19 00:53:2348DEFINE_GUID(CGID_DocHostCmdPriv, 0x000214D4L, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0,
49 0x46);
50
51
[email protected]f7817822009-09-24 05:11:5852base::ThreadLocalPointer<ChromeActiveDocument> g_active_doc_cache;
53
54bool g_first_launch_by_process_ = true;
55
[email protected]2f2afba2010-04-01 01:53:1956const DWORD kIEEncodingIdArray[] = {
57#define DEFINE_ENCODING_ID_ARRAY(encoding_name, id, chrome_name) encoding_name,
58 INTERNAL_IE_ENCODINGMENU_IDS(DEFINE_ENCODING_ID_ARRAY)
59#undef DEFINE_ENCODING_ID_ARRAY
60 0 // The Last data must be 0 to indicate the end of the encoding id array.
61};
62
[email protected]f7817822009-09-24 05:11:5863ChromeActiveDocument::ChromeActiveDocument()
64 : first_navigation_(true),
[email protected]a15d4a42010-02-17 08:21:3565 is_automation_client_reused_(false),
[email protected]355309e2010-03-15 17:01:3466 popup_allowed_(false),
[email protected]a15d4a42010-02-17 08:21:3567 accelerator_table_(NULL) {
[email protected]04e3f352010-05-10 13:48:2468 TRACE_EVENT_BEGIN("chromeframe.createactivedocument", this, "");
69
[email protected]3eb07da2010-02-01 19:48:3670 url_fetcher_.set_frame_busting(false);
[email protected]a1800e82009-11-19 00:53:2371 memset(&navigation_info_, 0, sizeof(navigation_info_));
[email protected]f7817822009-09-24 05:11:5872}
73
74HRESULT ChromeActiveDocument::FinalConstruct() {
75 // If we have a cached ChromeActiveDocument instance in TLS, then grab
76 // ownership of the cached document's automation client. This is an
77 // optimization to get Chrome active documents to load faster.
78 ChromeActiveDocument* cached_document = g_active_doc_cache.Get();
[email protected]58174a72010-05-05 21:30:3079 if (cached_document && cached_document->IsValid()) {
[email protected]f7817822009-09-24 05:11:5880 DCHECK(automation_client_.get() == NULL);
[email protected]3148c0ae2010-03-11 18:06:0081 automation_client_.swap(cached_document->automation_client_);
[email protected]f7817822009-09-24 05:11:5882 DLOG(INFO) << "Reusing automation client instance from "
83 << cached_document;
84 DCHECK(automation_client_.get() != NULL);
[email protected]3eb07da2010-02-01 19:48:3685 automation_client_->Reinitialize(this, &url_fetcher_);
[email protected]f7817822009-09-24 05:11:5886 is_automation_client_reused_ = true;
87 } else {
88 // The FinalConstruct implementation in the ChromeFrameActivexBase class
89 // i.e. Base creates an instance of the ChromeFrameAutomationClient class
90 // and initializes it, which would spawn a new Chrome process, etc.
91 // We don't want to be doing this if we have a cached document, whose
92 // automation client instance can be reused.
[email protected]97965e12010-04-09 00:51:1093 HRESULT hr = BaseActiveX::FinalConstruct();
[email protected]f7817822009-09-24 05:11:5894 if (FAILED(hr))
95 return hr;
96 }
97
98 bool chrome_network = GetConfigBool(false, kUseChromeNetworking);
99 bool top_level_requests = GetConfigBool(true, kHandleTopLevelRequests);
100 automation_client_->set_use_chrome_network(chrome_network);
101 automation_client_->set_handle_top_level_requests(top_level_requests);
102
103 find_dialog_.Init(automation_client_.get());
104
105 enabled_commands_map_[OLECMDID_PRINT] = true;
106 enabled_commands_map_[OLECMDID_FIND] = true;
107 enabled_commands_map_[OLECMDID_CUT] = true;
108 enabled_commands_map_[OLECMDID_COPY] = true;
109 enabled_commands_map_[OLECMDID_PASTE] = true;
110 enabled_commands_map_[OLECMDID_SELECTALL] = true;
[email protected]673fd2c02010-02-04 23:10:00111 enabled_commands_map_[OLECMDID_SAVEAS] = true;
[email protected]a15d4a42010-02-17 08:21:35112
[email protected]3c6f8e12010-03-24 21:58:21113 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase);
[email protected]a15d4a42010-02-17 08:21:35114 accelerator_table_ =
[email protected]3c6f8e12010-03-24 21:58:21115 LoadAccelerators(this_module,
[email protected]a15d4a42010-02-17 08:21:35116 MAKEINTRESOURCE(IDR_CHROME_FRAME_IE_FULL_TAB));
117 DCHECK(accelerator_table_ != NULL);
[email protected]f7817822009-09-24 05:11:58118 return S_OK;
119}
120
121ChromeActiveDocument::~ChromeActiveDocument() {
122 DLOG(INFO) << __FUNCTION__;
[email protected]bc2ff5192010-06-01 22:05:45123 if (find_dialog_.IsWindow())
[email protected]f7817822009-09-24 05:11:58124 find_dialog_.DestroyWindow();
[email protected]3eb07da2010-02-01 19:48:36125 // ChromeFramePlugin
[email protected]97965e12010-04-09 00:51:10126 BaseActiveX::Uninitialize();
[email protected]04e3f352010-05-10 13:48:24127
128 TRACE_EVENT_END("chromeframe.createactivedocument", this, "");
[email protected]f7817822009-09-24 05:11:58129}
130
131// Override DoVerb
132STDMETHODIMP ChromeActiveDocument::DoVerb(LONG verb,
133 LPMSG msg,
134 IOleClientSite* active_site,
135 LONG index,
136 HWND parent_window,
137 LPCRECT pos) {
138 // IE will try and in-place activate us in some cases. This happens when
139 // the user opens a new IE window with a URL that has us as the DocObject.
140 // Here we refuse to be activated in-place and we will force IE to UIActivate
141 // us.
[email protected]bc2ff5192010-06-01 22:05:45142 if (OLEIVERB_INPLACEACTIVATE == verb)
[email protected]f7817822009-09-24 05:11:58143 return E_NOTIMPL;
[email protected]f7817822009-09-24 05:11:58144 // Check if we should activate as a docobject or not
145 // (client supports IOleDocumentSite)
146 if (doc_site_) {
147 switch (verb) {
[email protected]1bb5f892009-10-06 01:44:57148 case OLEIVERB_SHOW: {
149 ScopedComPtr<IDocHostUIHandler> doc_host_handler;
150 doc_host_handler.QueryFrom(doc_site_);
[email protected]bc2ff5192010-06-01 22:05:45151 if (doc_host_handler.get())
[email protected]1bb5f892009-10-06 01:44:57152 doc_host_handler->ShowUI(DOCHOSTUITYPE_BROWSE, this, this, NULL, NULL);
[email protected]1bb5f892009-10-06 01:44:57153 }
[email protected]f7817822009-09-24 05:11:58154 case OLEIVERB_OPEN:
155 case OLEIVERB_UIACTIVATE:
[email protected]bc2ff5192010-06-01 22:05:45156 if (!m_bUIActive)
[email protected]f7817822009-09-24 05:11:58157 return doc_site_->ActivateMe(NULL);
[email protected]f7817822009-09-24 05:11:58158 break;
159 }
160 }
161 return IOleObjectImpl<ChromeActiveDocument>::DoVerb(verb,
162 msg,
163 active_site,
164 index,
165 parent_window,
166 pos);
167}
168
[email protected]f7817822009-09-24 05:11:58169// Override IOleInPlaceActiveObjectImpl::OnDocWindowActivate
170STDMETHODIMP ChromeActiveDocument::OnDocWindowActivate(BOOL activate) {
171 DLOG(INFO) << __FUNCTION__;
172 return S_OK;
173}
174
175STDMETHODIMP ChromeActiveDocument::TranslateAccelerator(MSG* msg) {
176 DLOG(INFO) << __FUNCTION__;
177 if (msg == NULL)
178 return E_POINTER;
179
180 if (msg->message == WM_KEYDOWN && msg->wParam == VK_TAB) {
181 HWND focus = ::GetFocus();
182 if (focus != m_hWnd && !::IsChild(m_hWnd, focus)) {
183 // The call to SetFocus triggers a WM_SETFOCUS that makes the base class
184 // set focus to the correct element in Chrome.
185 ::SetFocus(m_hWnd);
186 return S_OK;
187 }
188 }
189
190 return S_FALSE;
191}
192// Override IPersistStorageImpl::IsDirty
193STDMETHODIMP ChromeActiveDocument::IsDirty() {
194 DLOG(INFO) << __FUNCTION__;
195 return S_FALSE;
196}
197
[email protected]b95f550f2009-11-19 05:35:22198void ChromeActiveDocument::OnAutomationServerReady() {
[email protected]97965e12010-04-09 00:51:10199 BaseActiveX::OnAutomationServerReady();
[email protected]1fd45692010-04-19 21:01:18200 BaseActiveX::GiveFocusToChrome(true);
[email protected]b95f550f2009-11-19 05:35:22201}
202
[email protected]f7817822009-09-24 05:11:58203STDMETHODIMP ChromeActiveDocument::Load(BOOL fully_avalable,
204 IMoniker* moniker_name,
205 LPBC bind_context,
206 DWORD mode) {
[email protected]bc2ff5192010-06-01 22:05:45207 if (NULL == moniker_name)
[email protected]f7817822009-09-24 05:11:58208 return E_INVALIDARG;
[email protected]a1800e82009-11-19 00:53:23209
210 ScopedComPtr<IOleClientSite> client_site;
211 if (bind_context) {
212 ScopedComPtr<IUnknown> site;
213 bind_context->GetObjectParam(SZ_HTML_CLIENTSITE_OBJECTPARAM,
214 site.Receive());
215 if (site)
216 client_site.QueryFrom(site);
217 }
218
219 if (client_site) {
220 SetClientSite(client_site);
[email protected]b1c55638612010-03-08 16:26:11221 DoQueryService(IID_INewWindowManager, client_site,
222 popup_manager_.Receive());
[email protected]051236f2010-03-12 22:06:14223
224 // See if mshtml parsed the html header for us. If so, we need to
225 // clear the browser service flag that we use to indicate that this
226 // browser instance is navigating to a CF document.
227 ScopedComPtr<IBrowserService> browser_service;
228 DoQueryService(SID_SShellBrowser, client_site, browser_service.Receive());
229 if (browser_service) {
230 bool flagged = CheckForCFNavigation(browser_service, true);
231 DLOG_IF(INFO, flagged) << "Cleared flagged browser service";
232 }
[email protected]a1800e82009-11-19 00:53:23233 }
234
[email protected]051236f2010-03-12 22:06:14235 NavigationManager* mgr = NavigationManager::GetThreadInstance();
236 DCHECK(mgr);
[email protected]7e3544b2010-01-22 00:02:34237
[email protected]29c32f902010-04-20 23:27:29238 std::wstring url;
239
[email protected]77d7aee2010-05-14 20:31:55240 ScopedComPtr<BindContextInfo> info;
241 BindContextInfo::FromBindContext(bind_context, info.Receive());
[email protected]29c32f902010-04-20 23:27:29242 DCHECK(info);
243 if (info && !info->url().empty()) {
244 url = info->url();
245 } else {
246 // If the original URL contains an anchor, then the URL queried
247 // from the moniker does not contain the anchor. To workaround
248 // this we retrieve the URL from our BHO.
249 url = GetActualUrlFromMoniker(moniker_name, bind_context,
250 mgr ? mgr->url(): std::wstring());
251 }
[email protected]051236f2010-03-12 22:06:14252
[email protected]37346bc2009-09-25 22:54:33253 // The is_new_navigation variable indicates if this a navigation initiated
254 // by typing in a URL for e.g. in the IE address bar, or from Chrome by
255 // a window.open call from javascript, in which case the current IE tab
256 // will attach to an existing ExternalTabContainer instance.
[email protected]f7817822009-09-24 05:11:58257 bool is_new_navigation = true;
[email protected]37346bc2009-09-25 22:54:33258 bool is_chrome_protocol = false;
[email protected]f7817822009-09-24 05:11:58259
[email protected]37346bc2009-09-25 22:54:33260 if (!ParseUrl(url, &is_new_navigation, &is_chrome_protocol, &url)) {
261 DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
[email protected]f7817822009-09-24 05:11:58262 return E_INVALIDARG;
263 }
264
[email protected]37346bc2009-09-25 22:54:33265 if (!LaunchUrl(url, is_new_navigation)) {
266 NOTREACHED() << __FUNCTION__ << " Failed to launch url:" << url;
267 return E_INVALIDARG;
[email protected]f7817822009-09-24 05:11:58268 }
269
[email protected]bc2ff5192010-06-01 22:05:45270 if (!is_chrome_protocol)
[email protected]0ce46402010-04-08 16:53:57271 url_fetcher_.SetInfoForUrl(url, moniker_name, bind_context);
[email protected]f7817822009-09-24 05:11:58272
[email protected]333590002010-03-05 18:49:21273 THREAD_SAFE_UMA_HISTOGRAM_CUSTOM_COUNTS("ChromeFrame.FullTabLaunchType",
274 is_chrome_protocol, 0, 1, 2);
[email protected]f7817822009-09-24 05:11:58275 return S_OK;
276}
277
278STDMETHODIMP ChromeActiveDocument::Save(IMoniker* moniker_name,
279 LPBC bind_context,
280 BOOL remember) {
281 return E_NOTIMPL;
282}
283
284STDMETHODIMP ChromeActiveDocument::SaveCompleted(IMoniker* moniker_name,
285 LPBC bind_context) {
286 return E_NOTIMPL;
287}
288
289STDMETHODIMP ChromeActiveDocument::GetCurMoniker(IMoniker** moniker_name) {
290 return E_NOTIMPL;
291}
292
293STDMETHODIMP ChromeActiveDocument::GetClassID(CLSID* class_id) {
[email protected]bc2ff5192010-06-01 22:05:45294 if (NULL == class_id)
[email protected]f7817822009-09-24 05:11:58295 return E_POINTER;
[email protected]f7817822009-09-24 05:11:58296 *class_id = GetObjectCLSID();
297 return S_OK;
298}
299
300STDMETHODIMP ChromeActiveDocument::QueryStatus(const GUID* cmd_group_guid,
301 ULONG number_of_commands,
302 OLECMD commands[],
303 OLECMDTEXT* command_text) {
304 DLOG(INFO) << __FUNCTION__;
[email protected]b509ec82010-04-13 16:53:06305 const GUID* supported_groups[] = {
306 &GUID_NULL,
307 &CGID_MSHTML,
308 &CGID_Explorer,
309 };
310
311 bool supported = (cmd_group_guid == NULL);
[email protected]bc2ff5192010-06-01 22:05:45312 for (int i = 0; !supported && i < arraysize(supported_groups); ++i)
[email protected]b509ec82010-04-13 16:53:06313 supported = (IsEqualGUID(*cmd_group_guid, *supported_groups[i]) != FALSE);
[email protected]b509ec82010-04-13 16:53:06314
315 if (!supported) {
316 DLOG(INFO) << "unsupported command group: "
317 << GuidToString(*cmd_group_guid);
318 return OLECMDERR_E_NOTSUPPORTED;
319 }
320
[email protected]f7817822009-09-24 05:11:58321 for (ULONG command_index = 0; command_index < number_of_commands;
322 command_index++) {
323 DLOG(INFO) << "Command id = " << commands[command_index].cmdID;
324 if (enabled_commands_map_.find(commands[command_index].cmdID) !=
[email protected]bc2ff5192010-06-01 22:05:45325 enabled_commands_map_.end())
[email protected]f7817822009-09-24 05:11:58326 commands[command_index].cmdf = OLECMDF_ENABLED;
[email protected]f7817822009-09-24 05:11:58327 }
328 return S_OK;
329}
330
331STDMETHODIMP ChromeActiveDocument::Exec(const GUID* cmd_group_guid,
332 DWORD command_id,
333 DWORD cmd_exec_opt,
334 VARIANT* in_args,
335 VARIANT* out_args) {
336 DLOG(INFO) << __FUNCTION__ << " Cmd id =" << command_id;
337 // Bail out if we have been uninitialized.
338 if (automation_client_.get() && automation_client_->tab()) {
339 return ProcessExecCommand(cmd_group_guid, command_id, cmd_exec_opt,
340 in_args, out_args);
341 }
[email protected]e3200932009-10-09 21:33:03342 return OLECMDERR_E_NOTSUPPORTED;
[email protected]f7817822009-09-24 05:11:58343}
344
[email protected]a1800e82009-11-19 00:53:23345STDMETHODIMP ChromeActiveDocument::LoadHistory(IStream* stream,
346 IBindCtx* bind_context) {
347 // Read notes in ChromeActiveDocument::SaveHistory
348 DCHECK(stream);
349 LARGE_INTEGER offset = {0};
350 ULARGE_INTEGER cur_pos = {0};
351 STATSTG statstg = {0};
352
353 stream->Seek(offset, STREAM_SEEK_CUR, &cur_pos);
354 stream->Stat(&statstg, STATFLAG_NONAME);
355
356 DWORD url_size = statstg.cbSize.LowPart - cur_pos.LowPart;
357 ScopedBstr url_bstr;
358 DWORD bytes_read = 0;
359 stream->Read(url_bstr.AllocateBytes(url_size), url_size, &bytes_read);
360 std::wstring url(url_bstr);
361
362 bool is_new_navigation = true;
363 bool is_chrome_protocol = false;
364
365 if (!ParseUrl(url, &is_new_navigation, &is_chrome_protocol, &url)) {
366 DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
367 return E_INVALIDARG;
368 }
369
370 if (!LaunchUrl(url, is_new_navigation)) {
371 NOTREACHED() << __FUNCTION__ << " Failed to launch url:" << url;
372 return E_INVALIDARG;
373 }
374 return S_OK;
375}
376
377STDMETHODIMP ChromeActiveDocument::SaveHistory(IStream* stream) {
378 // TODO(sanjeevr): We need to fetch the entire list of navigation entries
379 // from Chrome and persist it in the stream. And in LoadHistory we need to
380 // pass this list back to Chrome which will recreate the list. This will allow
381 // Back-Forward navigation to anchors to work correctly when we navigate to a
382 // page outside of ChromeFrame and then come back.
383 if (!stream) {
384 NOTREACHED();
385 return E_INVALIDARG;
386 }
387
388 LARGE_INTEGER offset = {0};
389 ULARGE_INTEGER new_pos = {0};
390 DWORD written = 0;
391 std::wstring url = UTF8ToWide(navigation_info_.url.spec());
392 return stream->Write(url.c_str(), (url.length() + 1) * sizeof(wchar_t),
393 &written);
394}
395
396STDMETHODIMP ChromeActiveDocument::SetPositionCookie(DWORD position_cookie) {
[email protected]77700832010-04-27 00:06:03397 if (automation_client_.get()) {
398 int index = static_cast<int>(position_cookie);
399 navigation_info_.navigation_index = index;
400 automation_client_->NavigateToIndex(index);
401 } else {
402 DLOG(WARNING) << "Invalid automation client instance";
403 }
[email protected]a1800e82009-11-19 00:53:23404 return S_OK;
405}
406
407STDMETHODIMP ChromeActiveDocument::GetPositionCookie(DWORD* position_cookie) {
408 if (!position_cookie)
409 return E_INVALIDARG;
410
411 *position_cookie = navigation_info_.navigation_index;
412 return S_OK;
413}
414
[email protected]f7817822009-09-24 05:11:58415STDMETHODIMP ChromeActiveDocument::GetUrlForEvents(BSTR* url) {
[email protected]bc2ff5192010-06-01 22:05:45416 if (NULL == url)
[email protected]f7817822009-09-24 05:11:58417 return E_POINTER;
[email protected]f7817822009-09-24 05:11:58418 *url = ::SysAllocString(url_);
419 return S_OK;
420}
421
[email protected]a1800e82009-11-19 00:53:23422STDMETHODIMP ChromeActiveDocument::GetAddressBarUrl(BSTR* url) {
423 return GetUrlForEvents(url);
424}
425
[email protected]80b5a8d2010-03-19 16:50:43426STDMETHODIMP ChromeActiveDocument::Reset() {
427 next_privacy_record_ = privacy_info_.privacy_records.begin();
428 return S_OK;
429}
430
431STDMETHODIMP ChromeActiveDocument::GetSize(unsigned long* size) {
432 if (!size)
433 return E_POINTER;
434
435 *size = privacy_info_.privacy_records.size();
436 return S_OK;
437}
438
439STDMETHODIMP ChromeActiveDocument::GetPrivacyImpacted(BOOL* privacy_impacted) {
440 if (!privacy_impacted)
441 return E_POINTER;
442
443 *privacy_impacted = privacy_info_.privacy_impacted;
444 return S_OK;
445}
446
447STDMETHODIMP ChromeActiveDocument::Next(BSTR* url, BSTR* policy,
448 long* reserved, unsigned long* flags) {
449 if (!url || !policy || !flags)
450 return E_POINTER;
451
452 if (next_privacy_record_ == privacy_info_.privacy_records.end())
453 return HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS);
454
455 *url = SysAllocString(next_privacy_record_->first.c_str());
456 *policy = SysAllocString(next_privacy_record_->second.policy_ref.c_str());
457 *flags = next_privacy_record_->second.flags;
458
459 next_privacy_record_++;
460 return S_OK;
461}
462
[email protected]f7817822009-09-24 05:11:58463HRESULT ChromeActiveDocument::IOleObject_SetClientSite(
464 IOleClientSite* client_site) {
465 if (client_site == NULL) {
466 ChromeActiveDocument* cached_document = g_active_doc_cache.Get();
467 if (cached_document) {
468 DCHECK(this == cached_document);
469 g_active_doc_cache.Set(NULL);
470 cached_document->Release();
471 }
[email protected]dda7d9c2009-11-11 23:01:47472
473 ScopedComPtr<IDocHostUIHandler> doc_host_handler;
[email protected]bc2ff5192010-06-01 22:05:45474 if (doc_site_)
[email protected]77059f72010-02-28 06:16:00475 doc_host_handler.QueryFrom(doc_site_);
[email protected]dda7d9c2009-11-11 23:01:47476
[email protected]bc2ff5192010-06-01 22:05:45477 if (doc_host_handler.get())
[email protected]dda7d9c2009-11-11 23:01:47478 doc_host_handler->HideUI();
[email protected]dda7d9c2009-11-11 23:01:47479
480 doc_site_.Release();
481 in_place_frame_.Release();
[email protected]f7817822009-09-24 05:11:58482 }
[email protected]a1800e82009-11-19 00:53:23483
[email protected]bc2ff5192010-06-01 22:05:45484 if (client_site != m_spClientSite)
[email protected]97965e12010-04-09 00:51:10485 return BaseActiveX::IOleObject_SetClientSite(client_site);
[email protected]a1800e82009-11-19 00:53:23486
487 return S_OK;
[email protected]f7817822009-09-24 05:11:58488}
489
[email protected]f7817822009-09-24 05:11:58490HRESULT ChromeActiveDocument::ActiveXDocActivate(LONG verb) {
491 HRESULT hr = S_OK;
492 m_bNegotiatedWnd = TRUE;
493 if (!m_bInPlaceActive) {
494 hr = m_spInPlaceSite->CanInPlaceActivate();
[email protected]bc2ff5192010-06-01 22:05:45495 if (FAILED(hr))
[email protected]f7817822009-09-24 05:11:58496 return hr;
[email protected]f7817822009-09-24 05:11:58497 m_spInPlaceSite->OnInPlaceActivate();
498 }
499 m_bInPlaceActive = TRUE;
500 // get location in the parent window,
501 // as well as some information about the parent
502 ScopedComPtr<IOleInPlaceUIWindow> in_place_ui_window;
503 frame_info_.cb = sizeof(OLEINPLACEFRAMEINFO);
504 HWND parent_window = NULL;
505 if (m_spInPlaceSite->GetWindow(&parent_window) == S_OK) {
506 in_place_frame_.Release();
507 RECT position_rect = {0};
508 RECT clip_rect = {0};
509 m_spInPlaceSite->GetWindowContext(in_place_frame_.Receive(),
510 in_place_ui_window.Receive(),
511 &position_rect,
512 &clip_rect,
513 &frame_info_);
514 if (!m_bWndLess) {
515 if (IsWindow()) {
516 ::ShowWindow(m_hWnd, SW_SHOW);
517 SetFocus();
518 } else {
[email protected]c1d19582010-03-03 01:57:13519 m_hWnd = Create(parent_window, position_rect, 0, 0, WS_EX_CLIENTEDGE);
[email protected]f7817822009-09-24 05:11:58520 }
521 }
522 SetObjectRects(&position_rect, &clip_rect);
523 }
524
525 ScopedComPtr<IOleInPlaceActiveObject> in_place_active_object(this);
526
527 // Gone active by now, take care of UIACTIVATE
528 if (DoesVerbUIActivate(verb)) {
529 if (!m_bUIActive) {
530 m_bUIActive = TRUE;
531 hr = m_spInPlaceSite->OnUIActivate();
[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 // set ourselves up in the host
535 if (in_place_active_object) {
[email protected]bc2ff5192010-06-01 22:05:45536 if (in_place_frame_)
[email protected]f7817822009-09-24 05:11:58537 in_place_frame_->SetActiveObject(in_place_active_object, NULL);
[email protected]bc2ff5192010-06-01 22:05:45538 if (in_place_ui_window)
[email protected]f7817822009-09-24 05:11:58539 in_place_ui_window->SetActiveObject(in_place_active_object, NULL);
[email protected]f7817822009-09-24 05:11:58540 }
541 }
542 }
543 m_spClientSite->ShowObject();
544 return S_OK;
545}
546
547void ChromeActiveDocument::OnNavigationStateChanged(int tab_handle, int flags,
548 const IPC::NavigationInfo& nav_info) {
549 // TODO(joshia): handle INVALIDATE_TAB,INVALIDATE_LOAD etc.
550 DLOG(INFO) << __FUNCTION__ << std::endl << " Flags: " << flags
551 << "Url: " << nav_info.url <<
552 ", Title: " << nav_info.title <<
553 ", Type: " << nav_info.navigation_type << ", Relative Offset: " <<
[email protected]62bb18dc12009-11-25 01:34:08554 nav_info.relative_offset << ", Index: " << nav_info.navigation_index;
[email protected]f7817822009-09-24 05:11:58555
556 UpdateNavigationState(nav_info);
557}
558
559void ChromeActiveDocument::OnUpdateTargetUrl(int tab_handle,
560 const std::wstring& new_target_url) {
[email protected]bc2ff5192010-06-01 22:05:45561 if (in_place_frame_)
[email protected]f7817822009-09-24 05:11:58562 in_place_frame_->SetStatusText(new_target_url.c_str());
[email protected]f7817822009-09-24 05:11:58563}
564
565bool IsFindAccelerator(const MSG& msg) {
566 // TODO(robertshield): This may not stand up to localization. Fix if this
567 // is the case.
568 return msg.message == WM_KEYDOWN && msg.wParam == 'F' &&
569 win_util::IsCtrlPressed() &&
570 !(win_util::IsAltPressed() || win_util::IsShiftPressed());
571}
572
573void ChromeActiveDocument::OnAcceleratorPressed(int tab_handle,
574 const MSG& accel_message) {
[email protected]a15d4a42010-02-17 08:21:35575 if (::TranslateAccelerator(m_hWnd, accelerator_table_,
576 const_cast<MSG*>(&accel_message)))
577 return;
578
[email protected]f7817822009-09-24 05:11:58579 bool handled_accel = false;
580 if (in_place_frame_ != NULL) {
581 handled_accel = (S_OK == in_place_frame_->TranslateAcceleratorW(
582 const_cast<MSG*>(&accel_message), 0));
583 }
584
585 if (!handled_accel) {
586 if (IsFindAccelerator(accel_message)) {
587 // Handle the showing of the find dialog explicitly.
588 OnFindInPage();
[email protected]2b8fd322009-10-02 00:00:59589 } else {
[email protected]97965e12010-04-09 00:51:10590 BaseActiveX::OnAcceleratorPressed(tab_handle, accel_message);
[email protected]f7817822009-09-24 05:11:58591 }
592 } else {
593 DLOG(INFO) << "IE handled accel key " << accel_message.wParam;
594 }
595}
596
597void ChromeActiveDocument::OnTabbedOut(int tab_handle, bool reverse) {
598 DLOG(INFO) << __FUNCTION__;
599 if (in_place_frame_) {
600 MSG msg = { NULL, WM_KEYDOWN, VK_TAB };
601 in_place_frame_->TranslateAcceleratorW(&msg, 0);
602 }
603}
604
605void ChromeActiveDocument::OnDidNavigate(int tab_handle,
606 const IPC::NavigationInfo& nav_info) {
607 DLOG(INFO) << __FUNCTION__ << std::endl << "Url: " << nav_info.url <<
608 ", Title: " << nav_info.title <<
609 ", Type: " << nav_info.navigation_type << ", Relative Offset: " <<
610 nav_info.relative_offset << ", Index: " << nav_info.navigation_index;
611
612 // This could be NULL if the active document instance is being destroyed.
613 if (!m_spInPlaceSite) {
614 DLOG(INFO) << __FUNCTION__ << "m_spInPlaceSite is NULL. Returning";
615 return;
616 }
617
618 UpdateNavigationState(nav_info);
619}
620
621void ChromeActiveDocument::UpdateNavigationState(
622 const IPC::NavigationInfo& new_navigation_info) {
[email protected]a1800e82009-11-19 00:53:23623 HRESULT hr = S_OK;
[email protected]f7817822009-09-24 05:11:58624 bool is_title_changed = (navigation_info_.title != new_navigation_info.title);
[email protected]f7817822009-09-24 05:11:58625 bool is_ssl_state_changed =
626 (navigation_info_.security_style != new_navigation_info.security_style) ||
[email protected]b4e75c12010-05-18 18:28:48627 (navigation_info_.displayed_insecure_content !=
628 new_navigation_info.displayed_insecure_content) ||
629 (navigation_info_.ran_insecure_content !=
630 new_navigation_info.ran_insecure_content);
[email protected]f7817822009-09-24 05:11:58631
[email protected]f7817822009-09-24 05:11:58632 if (is_ssl_state_changed) {
633 int lock_status = SECURELOCK_SET_UNSECURE;
[email protected]a1800e82009-11-19 00:53:23634 switch (new_navigation_info.security_style) {
[email protected]f7817822009-09-24 05:11:58635 case SECURITY_STYLE_AUTHENTICATION_BROKEN:
636 lock_status = SECURELOCK_SET_SECUREUNKNOWNBIT;
637 break;
638 case SECURITY_STYLE_AUTHENTICATED:
[email protected]b4e75c12010-05-18 18:28:48639 lock_status = new_navigation_info.displayed_insecure_content ?
[email protected]f7817822009-09-24 05:11:58640 SECURELOCK_SET_MIXED : SECURELOCK_SET_SECUREUNKNOWNBIT;
641 break;
642 default:
643 break;
644 }
645
646 ScopedVariant secure_lock_status(lock_status);
647 IEExec(&CGID_ShellDocView, INTERNAL_CMDID_SET_SSL_LOCK,
648 OLECMDEXECOPT_DODEFAULT, secure_lock_status.AsInput(), NULL);
649 }
650
[email protected]a1800e82009-11-19 00:53:23651 // Ideally all navigations should come to Chrome Frame so that we can call
652 // BeforeNavigate2 on installed BHOs and give them a chance to cancel the
653 // navigation. However, in practice what happens is as below:
654 // The very first navigation that happens in CF happens via a Load or a
655 // LoadHistory call. In this case, IE already has the correct information for
656 // its travel log as well address bar. For other internal navigations (navs
657 // that only happen within Chrome such as anchor navigations) we need to
658 // update IE's internal state after the fact. In the case of internal
659 // navigations, we notify the BHOs but ignore the should_cancel flag.
[email protected]6f526082010-01-28 19:36:58660
661 // Another case where we need to issue BeforeNavigate2 calls is as below:-
662 // We get notified after the fact, when navigations are initiated within
663 // Chrome via window.open calls. These navigations are handled by creating
664 // an external tab container within chrome and then connecting to it from IE.
665 // We still want to update the address bar/history, etc, to ensure that
666 // the special URL used by Chrome to indicate this is updated correctly.
667 bool is_internal_navigation = ((new_navigation_info.navigation_index > 0) &&
[email protected]a1800e82009-11-19 00:53:23668 (new_navigation_info.navigation_index !=
[email protected]6f526082010-01-28 19:36:58669 navigation_info_.navigation_index)) ||
670 StartsWith(static_cast<BSTR>(url_), kChromeAttachExternalTabPrefix,
671 false);
[email protected]f7817822009-09-24 05:11:58672
[email protected]bc2ff5192010-06-01 22:05:45673 if (new_navigation_info.url.is_valid())
[email protected]38939de2010-05-13 02:32:06674 url_.Allocate(UTF8ToWide(new_navigation_info.url.spec()).c_str());
[email protected]38939de2010-05-13 02:32:06675
[email protected]a1800e82009-11-19 00:53:23676 if (is_internal_navigation) {
677 ScopedComPtr<IDocObjectService> doc_object_svc;
[email protected]f7817822009-09-24 05:11:58678 ScopedComPtr<IWebBrowserEventsService> web_browser_events_svc;
[email protected]ae33d672010-03-04 21:58:06679
[email protected]f7817822009-09-24 05:11:58680 DoQueryService(__uuidof(web_browser_events_svc), m_spClientSite,
681 web_browser_events_svc.Receive());
[email protected]ae33d672010-03-04 21:58:06682
683 if (!web_browser_events_svc.get()) {
684 DoQueryService(SID_SShellBrowser, m_spClientSite,
685 doc_object_svc.Receive());
686 }
687
[email protected]62bb18dc12009-11-25 01:34:08688 // web_browser_events_svc can be NULL on IE6.
[email protected]f7817822009-09-24 05:11:58689 if (web_browser_events_svc) {
[email protected]f7817822009-09-24 05:11:58690 VARIANT_BOOL should_cancel = VARIANT_FALSE;
691 web_browser_events_svc->FireBeforeNavigate2Event(&should_cancel);
[email protected]a1800e82009-11-19 00:53:23692 }
693
694 // We need to tell IE that we support navigation so that IE will query us
695 // for IPersistHistory and call GetPositionCookie to save our navigation
696 // index.
697 ScopedVariant html_window(static_cast<IUnknown*>(
698 static_cast<IHTMLWindow2*>(this)));
699 IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0,
700 html_window.AsInput(), NULL);
701
702 // We pass the HLNF_INTERNALJUMP flag to INTERNAL_CMDID_FINALIZE_TRAVEL_LOG
703 // since we want to make IE treat all internal navigations within this page
704 // (including anchor navigations and subframe navigations) as anchor
705 // navigations. This will ensure that IE calls GetPositionCookie
706 // to save the current position cookie in the travel log and then call
707 // SetPositionCookie when the user hits Back/Forward to come back here.
708 ScopedVariant internal_navigation(HLNF_INTERNALJUMP);
709 IEExec(&CGID_Explorer, INTERNAL_CMDID_FINALIZE_TRAVEL_LOG, 0,
710 internal_navigation.AsInput(), NULL);
711
712 // We no longer need to lie to IE. If we lie persistently to IE, then
713 // IE reuses us for new navigations.
714 IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0, NULL, NULL);
715
716 if (doc_object_svc) {
717 // Now call the FireNavigateCompleteEvent which makes IE update the text
718 // in the address-bar.
719 doc_object_svc->FireNavigateComplete2(this, 0);
720 doc_object_svc->FireDocumentComplete(this, 0);
721 } else if (web_browser_events_svc) {
[email protected]f7817822009-09-24 05:11:58722 web_browser_events_svc->FireNavigateComplete2Event();
[email protected]a1800e82009-11-19 00:53:23723 web_browser_events_svc->FireDocumentCompleteEvent();
[email protected]f7817822009-09-24 05:11:58724 }
725 }
[email protected]37346bc2009-09-25 22:54:33726
[email protected]a1800e82009-11-19 00:53:23727 if (is_title_changed) {
728 ScopedVariant title(new_navigation_info.title.c_str());
729 IEExec(NULL, OLECMDID_SETTITLE, OLECMDEXECOPT_DONTPROMPTUSER,
730 title.AsInput(), NULL);
731 }
732
733 // It is important that we only update the navigation_info_ after we have
734 // finalized the travel log. This is because IE will ask for information
735 // such as navigation index when the travel log is finalized and we need
736 // supply the old index and not the new one.
737 navigation_info_ = new_navigation_info;
[email protected]37346bc2009-09-25 22:54:33738 // Update the IE zone here. Ideally we would like to do it when the active
739 // document is activated. However that does not work at times as the frame we
740 // get there is not the actual frame which handles the command.
741 IEExec(&CGID_Explorer, SBCMDID_MIXEDZONE, 0, NULL, NULL);
[email protected]f7817822009-09-24 05:11:58742}
743
744void ChromeActiveDocument::OnFindInPage() {
745 TabProxy* tab = GetTabProxy();
746 if (tab) {
[email protected]bc2ff5192010-06-01 22:05:45747 if (!find_dialog_.IsWindow())
[email protected]f7817822009-09-24 05:11:58748 find_dialog_.Create(m_hWnd);
[email protected]f7817822009-09-24 05:11:58749
750 find_dialog_.ShowWindow(SW_SHOW);
751 }
752}
753
754void ChromeActiveDocument::OnViewSource() {
755 DCHECK(navigation_info_.url.is_valid());
756 std::string url_to_open = "view-source:";
757 url_to_open += navigation_info_.url.spec();
[email protected]b1c55638612010-03-08 16:26:11758 HostNavigate(GURL(url_to_open), GURL(), NEW_WINDOW);
[email protected]f7817822009-09-24 05:11:58759}
760
[email protected]37346bc2009-09-25 22:54:33761void ChromeActiveDocument::OnDetermineSecurityZone(const GUID* cmd_group_guid,
762 DWORD command_id,
763 DWORD cmd_exec_opt,
764 VARIANT* in_args,
765 VARIANT* out_args) {
[email protected]7c712c92010-03-24 17:29:22766 // Always return URLZONE_INTERNET since that is the Chrome's behaviour.
767 // Correct step is to use MapUrlToZone().
[email protected]37346bc2009-09-25 22:54:33768 if (out_args != NULL) {
769 out_args->vt = VT_UI4;
770 out_args->ulVal = URLZONE_INTERNET;
771 }
772}
773
[email protected]80b5a8d2010-03-19 16:50:43774void ChromeActiveDocument::OnDisplayPrivacyInfo() {
775 privacy_info_ = url_fetcher_.privacy_info();
776 Reset();
777 DoPrivacyDlg(m_hWnd, url_, this, TRUE);
778}
779
[email protected]b36a9f92009-10-19 17:34:57780void ChromeActiveDocument::OnOpenURL(int tab_handle,
781 const GURL& url_to_open,
782 const GURL& referrer,
[email protected]f7817822009-09-24 05:11:58783 int open_disposition) {
784 // If the disposition indicates that we should be opening the URL in the
785 // current tab, then we can reuse the ChromeFrameAutomationClient instance
786 // maintained by the current ChromeActiveDocument instance. We cache this
787 // instance so that it can be used by the new ChromeActiveDocument instance
788 // which may be instantiated for handling the new URL.
789 if (open_disposition == CURRENT_TAB) {
790 // Grab a reference to ensure that the document remains valid.
791 AddRef();
792 g_active_doc_cache.Set(this);
793 }
794
[email protected]97965e12010-04-09 00:51:10795 BaseActiveX::OnOpenURL(tab_handle, url_to_open, referrer, open_disposition);
[email protected]f7817822009-09-24 05:11:58796}
797
[email protected]b1c55638612010-03-08 16:26:11798void ChromeActiveDocument::OnAttachExternalTab(int tab_handle,
799 const IPC::AttachExternalTabParams& params) {
[email protected]77700832010-04-27 00:06:03800 if (!automation_client_.get()) {
801 DLOG(WARNING) << "Invalid automation client instance";
802 return;
803 }
[email protected]b1c55638612010-03-08 16:26:11804 DWORD flags = 0;
805 if (params.user_gesture)
806 flags = NWMF_USERREQUESTED;
[email protected]355309e2010-03-15 17:01:34807 else if (popup_allowed_)
808 flags = NWMF_USERALLOWED;
[email protected]b1c55638612010-03-08 16:26:11809
810 HRESULT hr = S_OK;
811 if (popup_manager_) {
[email protected]355309e2010-03-15 17:01:34812 LPCWSTR popup_wnd_url = UTF8ToWide(params.url.spec()).c_str();
813 hr = popup_manager_->EvaluateNewWindow(popup_wnd_url, NULL, url_,
814 NULL, FALSE, flags, 0);
[email protected]b1c55638612010-03-08 16:26:11815 }
816 // Allow popup
817 if (hr == S_OK) {
[email protected]97965e12010-04-09 00:51:10818 BaseActiveX::OnAttachExternalTab(tab_handle, params);
[email protected]b1c55638612010-03-08 16:26:11819 return;
820 }
821
822 automation_client_->BlockExternalTab(params.cookie);
823}
824
[email protected]f7817822009-09-24 05:11:58825bool ChromeActiveDocument::PreProcessContextMenu(HMENU menu) {
826 ScopedComPtr<IBrowserService> browser_service;
827 ScopedComPtr<ITravelLog> travel_log;
[email protected]a1800e82009-11-19 00:53:23828 GetBrowserServiceAndTravelLog(browser_service.Receive(),
829 travel_log.Receive());
830 if (!browser_service || !travel_log)
[email protected]f7817822009-09-24 05:11:58831 return true;
832
[email protected]bc2ff5192010-06-01 22:05:45833 EnableMenuItem(menu, IDS_CONTENT_CONTEXT_BACK, MF_BYCOMMAND |
834 (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_BACK, NULL)) ?
835 MF_ENABLED : MF_DISABLED));
836 EnableMenuItem(menu, IDS_CONTENT_CONTEXT_FORWARD, MF_BYCOMMAND |
837 (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_FORE, NULL)) ?
838 MF_ENABLED : MF_DISABLED));
[email protected]f7817822009-09-24 05:11:58839
840 // Call base class (adds 'About' item)
[email protected]97965e12010-04-09 00:51:10841 return BaseActiveX::PreProcessContextMenu(menu);
[email protected]f7817822009-09-24 05:11:58842}
843
[email protected]35f13ab2009-12-16 23:59:17844bool ChromeActiveDocument::HandleContextMenuCommand(UINT cmd,
845 const IPC::ContextMenuParams& params) {
[email protected]f7817822009-09-24 05:11:58846 ScopedComPtr<IWebBrowser2> web_browser2;
847 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
848
[email protected]074283a2010-05-28 23:47:59849 if (cmd == IDC_BACK)
[email protected]b731a142010-05-14 00:03:03850 web_browser2->GoBack();
[email protected]074283a2010-05-28 23:47:59851 else if (cmd == IDC_FORWARD)
[email protected]b731a142010-05-14 00:03:03852 web_browser2->GoForward();
[email protected]074283a2010-05-28 23:47:59853 else if (cmd == IDC_RELOAD)
[email protected]b731a142010-05-14 00:03:03854 web_browser2->Refresh();
[email protected]074283a2010-05-28 23:47:59855 else
[email protected]b731a142010-05-14 00:03:03856 return BaseActiveX::HandleContextMenuCommand(cmd, params);
[email protected]f7817822009-09-24 05:11:58857
858 return true;
859}
860
861HRESULT ChromeActiveDocument::IEExec(const GUID* cmd_group_guid,
862 DWORD command_id, DWORD cmd_exec_opt,
863 VARIANT* in_args, VARIANT* out_args) {
864 HRESULT hr = E_FAIL;
[email protected]37346bc2009-09-25 22:54:33865
[email protected]f7817822009-09-24 05:11:58866 ScopedComPtr<IOleCommandTarget> frame_cmd_target;
[email protected]37346bc2009-09-25 22:54:33867
868 ScopedComPtr<IOleInPlaceSite> in_place_site(m_spInPlaceSite);
[email protected]bc2ff5192010-06-01 22:05:45869 if (!in_place_site.get() && m_spClientSite != NULL)
[email protected]37346bc2009-09-25 22:54:33870 in_place_site.QueryFrom(m_spClientSite);
[email protected]37346bc2009-09-25 22:54:33871
872 if (in_place_site)
873 hr = frame_cmd_target.QueryFrom(in_place_site);
[email protected]f7817822009-09-24 05:11:58874
[email protected]bc2ff5192010-06-01 22:05:45875 if (frame_cmd_target) {
[email protected]f7817822009-09-24 05:11:58876 hr = frame_cmd_target->Exec(cmd_group_guid, command_id, cmd_exec_opt,
877 in_args, out_args);
[email protected]bc2ff5192010-06-01 22:05:45878 }
[email protected]f7817822009-09-24 05:11:58879
880 return hr;
881}
[email protected]37346bc2009-09-25 22:54:33882
[email protected]7c712c92010-03-24 17:29:22883unsigned long ChromeActiveDocument::MapUrlToZone(const wchar_t* url) {
884 unsigned long zone = URLZONE_INVALID;
[email protected]37346bc2009-09-25 22:54:33885 if (security_manager_.get() == NULL) {
886 HRESULT hr = CoCreateInstance(
887 CLSID_InternetSecurityManager,
888 NULL,
889 CLSCTX_ALL,
890 IID_IInternetSecurityManager,
891 reinterpret_cast<void**>(security_manager_.Receive()));
892
893 if (FAILED(hr)) {
894 NOTREACHED() << __FUNCTION__
895 << " Failed to create InternetSecurityManager. Error: 0x%x"
896 << hr;
[email protected]7c712c92010-03-24 17:29:22897 return zone;
[email protected]37346bc2009-09-25 22:54:33898 }
899 }
900
[email protected]7c712c92010-03-24 17:29:22901 security_manager_->MapUrlToZone(url, &zone, 0);
902 return zone;
[email protected]37346bc2009-09-25 22:54:33903}
904
905bool ChromeActiveDocument::ParseUrl(const std::wstring& url,
906 bool* is_new_navigation,
907 bool* is_chrome_protocol,
908 std::wstring* parsed_url) {
909 if (!is_new_navigation || !is_chrome_protocol|| !parsed_url) {
910 NOTREACHED() << __FUNCTION__ << " Invalid arguments";
911 return false;
912 }
913
914 std::wstring initial_url = url;
915
916 *is_chrome_protocol = StartsWith(initial_url, kChromeProtocolPrefix,
917 false);
918
919 *is_new_navigation = true;
920
921 if (*is_chrome_protocol) {
922 initial_url.erase(0, lstrlen(kChromeProtocolPrefix));
923 *is_new_navigation =
924 !StartsWith(initial_url, kChromeAttachExternalTabPrefix, false);
925 }
926
[email protected]10bf1a72009-10-01 16:00:21927 if (!IsValidUrlScheme(initial_url, is_privileged_)) {
[email protected]37346bc2009-09-25 22:54:33928 DLOG(WARNING) << __FUNCTION__ << " Disallowing navigation to url: "
929 << url;
930 return false;
931 }
932
[email protected]7c712c92010-03-24 17:29:22933 if (URLZONE_UNTRUSTED == MapUrlToZone(initial_url.c_str())) {
[email protected]37346bc2009-09-25 22:54:33934 DLOG(WARNING) << __FUNCTION__
935 << " Disallowing navigation to restricted url: "
936 << initial_url;
937 return false;
938 }
939
[email protected]552fb6012010-02-03 17:24:29940 if (*is_chrome_protocol) {
941 // Allow chrome protocol (gcf:) if -
942 // - explicitly enabled using registry
943 // - for gcf:attach_external_tab
944 // - for gcf:about and gcf:view-source
945 GURL crack_url(initial_url);
946 bool allow_gcf_protocol = !*is_new_navigation ||
947 GetConfigBool(false, kEnableGCFProtocol) ||
948 crack_url.SchemeIs(chrome::kAboutScheme) ||
949 crack_url.SchemeIs(chrome::kViewSourceScheme);
950 if (!allow_gcf_protocol)
951 return false;
952 }
953
[email protected]37346bc2009-09-25 22:54:33954 *parsed_url = initial_url;
955 return true;
956}
957
958bool ChromeActiveDocument::LaunchUrl(const std::wstring& url,
959 bool is_new_navigation) {
[email protected]77700832010-04-27 00:06:03960 DCHECK(automation_client_.get() != NULL);
961
[email protected]b509ec82010-04-13 16:53:06962 url_.Allocate(url.c_str());
[email protected]6f526082010-01-28 19:36:58963
[email protected]37346bc2009-09-25 22:54:33964 if (!is_new_navigation) {
965 WStringTokenizer tokenizer(url, L"&");
966 // Skip over kChromeAttachExternalTabPrefix
967 tokenizer.GetNext();
968
[email protected]b1c55638612010-03-08 16:26:11969 uint64 external_tab_cookie = 0;
970 if (tokenizer.GetNext()) {
971 wchar_t* end_ptr = 0;
972 external_tab_cookie = _wcstoui64(tokenizer.token().c_str(), &end_ptr, 10);
973 }
[email protected]37346bc2009-09-25 22:54:33974
975 if (external_tab_cookie == 0) {
976 NOTREACHED() << "invalid url for attach tab: " << url;
977 return false;
978 }
979
980 automation_client_->AttachExternalTab(external_tab_cookie);
981 } else {
982 // Initiate navigation before launching chrome so that the url will be
983 // cached and sent with launch settings.
[email protected]62bb18dc12009-11-25 01:34:08984 if (url_.Length()) {
985 std::string utf8_url;
986 WideToUTF8(url_, url_.Length(), &utf8_url);
[email protected]b36a9f92009-10-19 17:34:57987
[email protected]62bb18dc12009-11-25 01:34:08988 std::string referrer;
[email protected]051236f2010-03-12 22:06:14989 NavigationManager* mgr = NavigationManager::GetThreadInstance();
990 if (mgr)
991 referrer = mgr->referrer();
[email protected]a1800e82009-11-19 00:53:23992
[email protected]62bb18dc12009-11-25 01:34:08993 if (!automation_client_->InitiateNavigation(utf8_url,
994 referrer,
995 is_privileged_)) {
996 DLOG(ERROR) << "Invalid URL: " << url;
997 Error(L"Invalid URL");
998 url_.Reset();
999 return false;
[email protected]37346bc2009-09-25 22:54:331000 }
[email protected]62bb18dc12009-11-25 01:34:081001
1002 DLOG(INFO) << "Url is " << url_;
[email protected]37346bc2009-09-25 22:54:331003 }
1004 }
1005
[email protected]3eb07da2010-02-01 19:48:361006 if (is_automation_client_reused_)
1007 return true;
[email protected]37346bc2009-09-25 22:54:331008
[email protected]3eb07da2010-02-01 19:48:361009 automation_client_->SetUrlFetcher(&url_fetcher_);
1010
[email protected]bc2ff5192010-06-01 22:05:451011 return InitializeAutomation(GetHostProcessName(false), std::wstring(),
1012 IsIEInPrivate(), false);
[email protected]37346bc2009-09-25 22:54:331013}
[email protected]1bb5f892009-10-06 01:44:571014
[email protected]355309e2010-03-15 17:01:341015
1016HRESULT ChromeActiveDocument::OnRefreshPage(const GUID* cmd_group_guid,
1017 DWORD command_id, DWORD cmd_exec_opt, VARIANT* in_args, VARIANT* out_args) {
1018 popup_allowed_ = false;
1019 if (in_args->vt == VT_I4 &&
1020 in_args->lVal & OLECMDIDF_REFRESH_PAGEACTION_POPUPWINDOW) {
1021 popup_allowed_ = true;
1022
1023 // Ask the yellow security band to change the text and icon and to remain
1024 // visible.
1025 IEExec(&CGID_DocHostCommandHandler, OLECMDID_PAGEACTIONBLOCKED,
1026 0x80000000 | OLECMDIDF_WINDOWSTATE_USERVISIBLE_VALID, NULL, NULL);
1027 }
1028
1029 TabProxy* tab_proxy = GetTabProxy();
1030 if (tab_proxy)
1031 tab_proxy->ReloadAsync();
1032
1033 return S_OK;
1034}
1035
1036
[email protected]1bb5f892009-10-06 01:44:571037HRESULT ChromeActiveDocument::SetPageFontSize(const GUID* cmd_group_guid,
1038 DWORD command_id,
1039 DWORD cmd_exec_opt,
1040 VARIANT* in_args,
1041 VARIANT* out_args) {
1042 if (!automation_client_.get()) {
[email protected]77700832010-04-27 00:06:031043 NOTREACHED() << "Invalid automation client";
[email protected]1bb5f892009-10-06 01:44:571044 return E_FAIL;
1045 }
1046
1047 switch (command_id) {
1048 case IDM_BASELINEFONT1:
1049 automation_client_->SetPageFontSize(SMALLEST_FONT);
1050 break;
1051
1052 case IDM_BASELINEFONT2:
1053 automation_client_->SetPageFontSize(SMALL_FONT);
1054 break;
1055
1056 case IDM_BASELINEFONT3:
1057 automation_client_->SetPageFontSize(MEDIUM_FONT);
1058 break;
1059
1060 case IDM_BASELINEFONT4:
1061 automation_client_->SetPageFontSize(LARGE_FONT);
1062 break;
1063
1064 case IDM_BASELINEFONT5:
1065 automation_client_->SetPageFontSize(LARGEST_FONT);
1066 break;
1067
1068 default:
1069 NOTREACHED() << "Invalid font size command: "
1070 << command_id;
1071 return E_FAIL;
1072 }
1073
1074 // Forward the command back to IEFrame with group set to
1075 // CGID_ExplorerBarDoc. This is probably needed to update the menu state to
1076 // indicate that the font size was set. This currently fails with error
1077 // 0x80040104.
1078 // TODO(iyengar)
1079 // Do some investigation into why this Exec call fails.
1080 IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
1081 return S_OK;
1082}
1083
[email protected]2f2afba2010-04-01 01:53:191084HRESULT ChromeActiveDocument::OnEncodingChange(const GUID* cmd_group_guid,
1085 DWORD command_id,
1086 DWORD cmd_exec_opt,
1087 VARIANT* in_args,
1088 VARIANT* out_args) {
1089 const struct EncodingMapData {
1090 DWORD ie_encoding_id;
1091 const char* chrome_encoding_name;
1092 } kEncodingTestDatas[] = {
1093#define DEFINE_ENCODING_MAP(encoding_name, id, chrome_name) \
1094 { encoding_name, chrome_name },
1095 INTERNAL_IE_ENCODINGMENU_IDS(DEFINE_ENCODING_MAP)
1096#undef DEFINE_ENCODING_MAP
1097 };
1098
1099 if (!automation_client_.get()) {
1100 NOTREACHED() << "Invalid automtion client";
1101 return E_FAIL;
1102 }
1103
[email protected]2f2afba2010-04-01 01:53:191104 const char* chrome_encoding_name = NULL;
1105 for (int i = 0; i < ARRAYSIZE_UNSAFE(kEncodingTestDatas); ++i) {
1106 const struct EncodingMapData* encoding_data = &kEncodingTestDatas[i];
1107 if (command_id == encoding_data->ie_encoding_id) {
1108 chrome_encoding_name = encoding_data->chrome_encoding_name;
1109 break;
1110 }
1111 }
1112 // Return E_FAIL when encountering invalid encoding id.
1113 if (!chrome_encoding_name)
1114 return E_FAIL;
1115
1116 TabProxy* tab = GetTabProxy();
1117 if (!tab) {
1118 NOTREACHED() << "Can not get TabProxy";
1119 return E_FAIL;
1120 }
1121
1122 if (chrome_encoding_name)
1123 tab->OverrideEncoding(chrome_encoding_name);
1124
1125 // Like we did on SetPageFontSize, we may forward the command back to IEFrame
1126 // to update the menu state to indicate that which encoding was set.
1127 // TODO(iyengar)
1128 // Do some investigation into why this Exec call fails.
1129 IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
1130 return S_OK;
1131}
1132
[email protected]f9cc4c452009-10-13 14:56:381133void ChromeActiveDocument::OnGoToHistoryEntryOffset(int tab_handle,
1134 int offset) {
[email protected]a1800e82009-11-19 00:53:231135 DLOG(INFO) << __FUNCTION__ << " - offset:" << offset;
1136
[email protected]f9cc4c452009-10-13 14:56:381137 ScopedComPtr<IBrowserService> browser_service;
[email protected]a1800e82009-11-19 00:53:231138 ScopedComPtr<ITravelLog> travel_log;
1139 GetBrowserServiceAndTravelLog(browser_service.Receive(),
1140 travel_log.Receive());
1141
1142 if (browser_service && travel_log)
1143 travel_log->Travel(browser_service, offset);
1144}
1145
1146HRESULT ChromeActiveDocument::GetBrowserServiceAndTravelLog(
1147 IBrowserService** browser_service, ITravelLog** travel_log) {
1148 DCHECK(browser_service || travel_log);
1149 ScopedComPtr<IBrowserService> browser_service_local;
1150 HRESULT hr = DoQueryService(SID_SShellBrowser, m_spClientSite,
1151 browser_service_local.Receive());
1152 if (!browser_service_local) {
1153 NOTREACHED() << "DoQueryService for IBrowserService failed: " << hr;
1154 return hr;
[email protected]f9cc4c452009-10-13 14:56:381155 }
[email protected]a1800e82009-11-19 00:53:231156
1157 if (travel_log) {
1158 hr = browser_service_local->GetTravelLog(travel_log);
[email protected]bc2ff5192010-06-01 22:05:451159 DLOG_IF(INFO, !travel_log) << "browser_service->GetTravelLog failed: " <<
1160 hr;
[email protected]a1800e82009-11-19 00:53:231161 }
1162
1163 if (browser_service)
1164 *browser_service = browser_service_local.Detach();
1165
1166 return hr;
[email protected]f9cc4c452009-10-13 14:56:381167}
[email protected]a15d4a42010-02-17 08:21:351168
1169LRESULT ChromeActiveDocument::OnForward(WORD notify_code, WORD id,
1170 HWND control_window,
1171 BOOL& bHandled) {
1172 ScopedComPtr<IWebBrowser2> web_browser2;
1173 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
1174 DCHECK(web_browser2);
1175
[email protected]bc2ff5192010-06-01 22:05:451176 if (web_browser2)
[email protected]a15d4a42010-02-17 08:21:351177 web_browser2->GoForward();
[email protected]a15d4a42010-02-17 08:21:351178 return 0;
1179}
1180
1181LRESULT ChromeActiveDocument::OnBack(WORD notify_code, WORD id,
1182 HWND control_window,
1183 BOOL& bHandled) {
1184 ScopedComPtr<IWebBrowser2> web_browser2;
1185 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
1186 DCHECK(web_browser2);
1187
[email protected]bc2ff5192010-06-01 22:05:451188 if (web_browser2)
[email protected]a15d4a42010-02-17 08:21:351189 web_browser2->GoBack();
[email protected]a15d4a42010-02-17 08:21:351190 return 0;
1191}
1192
[email protected]80b5a8d2010-03-19 16:50:431193LRESULT ChromeActiveDocument::OnFirePrivacyChange(UINT message, WPARAM wparam,
1194 LPARAM lparam,
1195 BOOL& handled) {
1196 if (!m_spClientSite)
1197 return 0;
1198
1199 ScopedComPtr<IWebBrowser2> web_browser2;
1200 DoQueryService(SID_SWebBrowserApp, m_spClientSite,
1201 web_browser2.Receive());
1202 if (!web_browser2) {
1203 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
1204 return 0;
1205 }
1206
1207 ScopedComPtr<IShellBrowser> shell_browser;
1208 DoQueryService(SID_STopLevelBrowser, web_browser2,
1209 shell_browser.Receive());
1210 DCHECK(shell_browser.get() != NULL);
1211 ScopedComPtr<ITridentService2> trident_services;
1212 trident_services.QueryFrom(shell_browser);
[email protected]bc2ff5192010-06-01 22:05:451213 if (trident_services)
[email protected]80b5a8d2010-03-19 16:50:431214 trident_services->FirePrivacyImpactedStateChange(wparam);
[email protected]bc2ff5192010-06-01 22:05:451215 else
[email protected]80b5a8d2010-03-19 16:50:431216 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
[email protected]80b5a8d2010-03-19 16:50:431217 return 0;
1218}