Home | History | Annotate | Download | only in chrome_frame
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // 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>
     10 #include <initguid.h>
     11 #include <mshtmcid.h>
     12 #include <shdeprecated.h>
     13 #include <shlguid.h>
     14 #include <shlobj.h>
     15 #include <shobjidl.h>
     16 #include <tlogstg.h>
     17 #include <urlmon.h>
     18 #include <wininet.h>
     19 
     20 #include "base/command_line.h"
     21 #include "base/debug/trace_event.h"
     22 #include "base/logging.h"
     23 #include "base/strings/string_util.h"
     24 #include "base/strings/utf_string_conversions.h"
     25 #include "base/win/scoped_variant.h"
     26 #include "chrome/app/chrome_command_ids.h"
     27 #include "chrome/app/chrome_dll_resource.h"
     28 #include "chrome/common/automation_messages.h"
     29 #include "chrome/common/chrome_constants.h"
     30 #include "chrome/test/automation/browser_proxy.h"
     31 #include "chrome/test/automation/tab_proxy.h"
     32 #include "chrome_frame/bho.h"
     33 #include "chrome_frame/bind_context_info.h"
     34 #include "chrome_frame/buggy_bho_handling.h"
     35 #include "chrome_frame/crash_reporting/crash_metrics.h"
     36 #include "chrome_frame/utils.h"
     37 #include "content/public/browser/invalidate_type.h"
     38 #include "content/public/browser/navigation_type.h"
     39 #include "content/public/browser/web_contents.h"
     40 #include "content/public/common/page_zoom.h"
     41 #include "grit/generated_resources.h"
     42 
     43 DEFINE_GUID(CGID_DocHostCmdPriv, 0x000214D4L, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0,
     44             0x46);
     45 
     46 bool g_first_launch_by_process_ = true;
     47 
     48 const DWORD kIEEncodingIdArray[] = {
     49 #define DEFINE_ENCODING_ID_ARRAY(encoding_name, id, chrome_name) encoding_name,
     50   INTERNAL_IE_ENCODINGMENU_IDS(DEFINE_ENCODING_ID_ARRAY)
     51 #undef DEFINE_ENCODING_ID_ARRAY
     52   0  // The Last data must be 0 to indicate the end of the encoding id array.
     53 };
     54 
     55 ChromeActiveDocument::ChromeActiveDocument()
     56     : navigation_info_(new NavigationInfo()),
     57       first_navigation_(true),
     58       is_automation_client_reused_(false),
     59       popup_allowed_(false),
     60       accelerator_table_(NULL) {
     61   TRACE_EVENT_BEGIN_ETW("chromeframe.createactivedocument", this, "");
     62 
     63   url_fetcher_->set_frame_busting(false);
     64   memset(navigation_info_.get(), 0, sizeof(NavigationInfo));
     65 }
     66 
     67 HRESULT ChromeActiveDocument::FinalConstruct() {
     68   // The FinalConstruct implementation in the ChromeFrameActivexBase class
     69   // i.e. Base creates an instance of the ChromeFrameAutomationClient class
     70   // and initializes it, which would spawn a new Chrome process, etc.
     71   // We don't want to be doing this if we have a cached document, whose
     72   // automation client instance can be reused.
     73   HRESULT hr = BaseActiveX::FinalConstruct();
     74   if (FAILED(hr))
     75     return hr;
     76 
     77   InitializeAutomationSettings();
     78 
     79   find_dialog_.Init(automation_client_.get());
     80 
     81   OLECMDF flags = static_cast<OLECMDF>(OLECMDF_ENABLED | OLECMDF_SUPPORTED);
     82 
     83   null_group_commands_map_[OLECMDID_PRINT] = flags;
     84   null_group_commands_map_[OLECMDID_FIND] = flags;
     85   null_group_commands_map_[OLECMDID_CUT] = flags;
     86   null_group_commands_map_[OLECMDID_COPY] = flags;
     87   null_group_commands_map_[OLECMDID_PASTE] = flags;
     88   null_group_commands_map_[OLECMDID_SELECTALL] = flags;
     89   null_group_commands_map_[OLECMDID_SAVEAS] = flags;
     90 
     91   mshtml_group_commands_map_[IDM_BASELINEFONT1] = flags;
     92   mshtml_group_commands_map_[IDM_BASELINEFONT2] = flags;
     93   mshtml_group_commands_map_[IDM_BASELINEFONT3] = flags;
     94   mshtml_group_commands_map_[IDM_BASELINEFONT4] = flags;
     95   mshtml_group_commands_map_[IDM_BASELINEFONT5] = flags;
     96   mshtml_group_commands_map_[IDM_VIEWSOURCE] = flags;
     97 
     98   HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase);
     99   accelerator_table_ =
    100     LoadAccelerators(this_module,
    101                      MAKEINTRESOURCE(IDR_CHROME_FRAME_IE_FULL_TAB));
    102   DCHECK(accelerator_table_ != NULL);
    103   return S_OK;
    104 }
    105 
    106 ChromeActiveDocument::~ChromeActiveDocument() {
    107   DVLOG(1) << __FUNCTION__;
    108   if (find_dialog_.IsWindow())
    109     find_dialog_.DestroyWindow();
    110   // ChromeFramePlugin
    111   BaseActiveX::Uninitialize();
    112 
    113   TRACE_EVENT_END_ETW("chromeframe.createactivedocument", this, "");
    114 }
    115 
    116 // Override DoVerb
    117 STDMETHODIMP ChromeActiveDocument::DoVerb(LONG verb,
    118                                           LPMSG msg,
    119                                           IOleClientSite* active_site,
    120                                           LONG index,
    121                                           HWND parent_window,
    122                                           LPCRECT pos) {
    123   // IE will try and in-place activate us in some cases. This happens when
    124   // the user opens a new IE window with a URL that has us as the DocObject.
    125   // Here we refuse to be activated in-place and we will force IE to UIActivate
    126   // us.
    127   if (OLEIVERB_INPLACEACTIVATE == verb)
    128     return OLEOBJ_E_INVALIDVERB;
    129   // Check if we should activate as a docobject or not
    130   // (client supports IOleDocumentSite)
    131   if (doc_site_) {
    132     switch (verb) {
    133     case OLEIVERB_SHOW: {
    134       base::win::ScopedComPtr<IDocHostUIHandler> doc_host_handler;
    135       doc_host_handler.QueryFrom(doc_site_);
    136       if (doc_host_handler.get())
    137         doc_host_handler->ShowUI(DOCHOSTUITYPE_BROWSE, this, this, NULL, NULL);
    138     }
    139     case OLEIVERB_OPEN:
    140     case OLEIVERB_UIACTIVATE:
    141       if (!m_bUIActive)
    142         return doc_site_->ActivateMe(NULL);
    143       break;
    144     }
    145   }
    146   return IOleObjectImpl<ChromeActiveDocument>::DoVerb(verb,
    147                                                       msg,
    148                                                       active_site,
    149                                                       index,
    150                                                       parent_window,
    151                                                       pos);
    152 }
    153 
    154 // Override IOleInPlaceActiveObjectImpl::OnDocWindowActivate
    155 STDMETHODIMP ChromeActiveDocument::OnDocWindowActivate(BOOL activate) {
    156   DVLOG(1) << __FUNCTION__;
    157   return S_OK;
    158 }
    159 
    160 STDMETHODIMP ChromeActiveDocument::TranslateAccelerator(MSG* msg) {
    161   DVLOG(1) << __FUNCTION__;
    162   if (msg == NULL)
    163     return E_POINTER;
    164 
    165   if (msg->message == WM_KEYDOWN && msg->wParam == VK_TAB) {
    166     HWND focus = ::GetFocus();
    167     if (focus != m_hWnd && !::IsChild(m_hWnd, focus)) {
    168       // The call to SetFocus triggers a WM_SETFOCUS that makes the base class
    169       // set focus to the correct element in Chrome.
    170       ::SetFocus(m_hWnd);
    171       return S_OK;
    172     }
    173   }
    174 
    175   return S_FALSE;
    176 }
    177 // Override IPersistStorageImpl::IsDirty
    178 STDMETHODIMP ChromeActiveDocument::IsDirty() {
    179   DVLOG(1) << __FUNCTION__;
    180   return S_FALSE;
    181 }
    182 
    183 void ChromeActiveDocument::OnAutomationServerReady() {
    184   BaseActiveX::OnAutomationServerReady();
    185   BaseActiveX::GiveFocusToChrome(true);
    186 }
    187 
    188 STDMETHODIMP ChromeActiveDocument::Load(BOOL fully_avalable,
    189                                         IMoniker* moniker_name,
    190                                         LPBC bind_context,
    191                                         DWORD mode) {
    192   if (NULL == moniker_name)
    193     return E_INVALIDARG;
    194 
    195   base::win::ScopedComPtr<IOleClientSite> client_site;
    196   if (bind_context) {
    197     base::win::ScopedComPtr<IUnknown> site;
    198     bind_context->GetObjectParam(SZ_HTML_CLIENTSITE_OBJECTPARAM,
    199                                  site.Receive());
    200     if (site)
    201       client_site.QueryFrom(site);
    202   }
    203 
    204   if (client_site) {
    205     SetClientSite(client_site);
    206     DoQueryService(IID_INewWindowManager, client_site,
    207                    popup_manager_.Receive());
    208 
    209     // See if mshtml parsed the html header for us.  If so, we need to
    210     // clear the browser service flag that we use to indicate that this
    211     // browser instance is navigating to a CF document.
    212     base::win::ScopedComPtr<IBrowserService> browser_service;
    213     DoQueryService(SID_SShellBrowser, client_site, browser_service.Receive());
    214     if (browser_service) {
    215       bool flagged = CheckForCFNavigation(browser_service, true);
    216       DVLOG_IF(1, flagged) << "Cleared flagged browser service";
    217     }
    218   }
    219 
    220   NavigationManager* mgr = NavigationManager::GetThreadInstance();
    221   DLOG_IF(ERROR, !mgr) << "Couldn't get instance of NavigationManager";
    222 
    223   std::wstring url;
    224 
    225   base::win::ScopedComPtr<BindContextInfo> info;
    226   BindContextInfo::FromBindContext(bind_context, info.Receive());
    227   DCHECK(info);
    228   if (info && !info->GetUrl().empty()) {
    229     url = info->GetUrl();
    230     if (mgr) {
    231       // If the original URL contains an anchor, then the URL queried
    232       // from the protocol sink wrapper does not contain the anchor. To
    233       // workaround this we retrieve the anchor from the navigation manager
    234       // and append it to the url retrieved from the protocol sink wrapper.
    235       GURL url_for_anchor(mgr->url());
    236       if (url_for_anchor.has_ref()) {
    237         url += L"#";
    238         url += UTF8ToWide(url_for_anchor.ref());
    239       }
    240     }
    241   } else {
    242     // If the original URL contains an anchor, then the URL queried
    243     // from the moniker does not contain the anchor. To workaround
    244     // this we retrieve the URL from our BHO.
    245     url = GetActualUrlFromMoniker(moniker_name, bind_context,
    246                                   mgr ? mgr->url(): std::wstring());
    247   }
    248 
    249   ChromeFrameUrl cf_url;
    250   if (!cf_url.Parse(url)) {
    251     DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
    252     return E_INVALIDARG;
    253   }
    254 
    255   std::string referrer(mgr ? mgr->referrer() : std::string());
    256   RendererType renderer_type = cf_url.is_chrome_protocol() ?
    257       RENDERER_TYPE_CHROME_GCF_PROTOCOL : RENDERER_TYPE_UNDETERMINED;
    258 
    259   // With CTransaction patch we have more robust way to grab the referrer for
    260   // each top-level-switch-to-CF request by peeking at our sniffing data
    261   // object that lives inside the bind context.  We also remember the reason
    262   // we're rendering the document in Chrome.
    263   if (g_patch_helper.state() == PatchHelper::PATCH_PROTOCOL && info) {
    264     scoped_refptr<ProtData> prot_data = info->get_prot_data();
    265     if (prot_data) {
    266       referrer = prot_data->referrer();
    267       renderer_type = prot_data->renderer_type();
    268     }
    269   }
    270 
    271   // For gcf: URLs allow only about and view-source schemes to pass through for
    272   // further inspection.
    273   bool is_safe_scheme = cf_url.gurl().SchemeIs(chrome::kAboutScheme) ||
    274       cf_url.gurl().SchemeIs(content::kViewSourceScheme);
    275   if (cf_url.is_chrome_protocol() && !is_safe_scheme &&
    276       !GetConfigBool(false, kAllowUnsafeURLs)) {
    277     DLOG(ERROR) << __FUNCTION__ << " gcf: not allowed:" << url;
    278     return E_INVALIDARG;
    279   }
    280 
    281   if (!LaunchUrl(cf_url, referrer)) {
    282     DLOG(ERROR) << __FUNCTION__ << " Failed to launch url:" << url;
    283     return E_INVALIDARG;
    284   }
    285 
    286   if (!cf_url.is_chrome_protocol() && !cf_url.attach_to_external_tab())
    287     url_fetcher_->SetInfoForUrl(url.c_str(), moniker_name, bind_context);
    288 
    289   // Log a metric indicating why GCF is rendering in Chrome.
    290   // (Note: we only track the renderer type when using the CTransaction patch.
    291   // When the code for the browser service patch and for the moniker patch is
    292   // removed, this conditional can also go away.)
    293   if (RENDERER_TYPE_UNDETERMINED != renderer_type) {
    294     UMA_LAUNCH_TYPE_COUNT(renderer_type);
    295   }
    296 
    297   return S_OK;
    298 }
    299 
    300 STDMETHODIMP ChromeActiveDocument::Save(IMoniker* moniker_name,
    301                                         LPBC bind_context,
    302                                         BOOL remember) {
    303   return E_NOTIMPL;
    304 }
    305 
    306 STDMETHODIMP ChromeActiveDocument::SaveCompleted(IMoniker* moniker_name,
    307                                                  LPBC bind_context) {
    308   return E_NOTIMPL;
    309 }
    310 
    311 STDMETHODIMP ChromeActiveDocument::GetCurMoniker(IMoniker** moniker_name) {
    312   return E_NOTIMPL;
    313 }
    314 
    315 STDMETHODIMP ChromeActiveDocument::GetClassID(CLSID* class_id) {
    316   if (NULL == class_id)
    317     return E_POINTER;
    318   *class_id = GetObjectCLSID();
    319   return S_OK;
    320 }
    321 
    322 STDMETHODIMP ChromeActiveDocument::QueryStatus(const GUID* cmd_group_guid,
    323                                                ULONG number_of_commands,
    324                                                OLECMD commands[],
    325                                                OLECMDTEXT* command_text) {
    326   DVLOG(1) << __FUNCTION__;
    327 
    328   CommandStatusMap* command_map = NULL;
    329 
    330   if (cmd_group_guid) {
    331     if (IsEqualGUID(*cmd_group_guid, GUID_NULL)) {
    332       command_map = &null_group_commands_map_;
    333     } else if (IsEqualGUID(*cmd_group_guid, CGID_MSHTML)) {
    334       command_map = &mshtml_group_commands_map_;
    335     } else if (IsEqualGUID(*cmd_group_guid, CGID_Explorer)) {
    336       command_map = &explorer_group_commands_map_;
    337     } else if (IsEqualGUID(*cmd_group_guid, CGID_ShellDocView)) {
    338       command_map = &shdoc_view_group_commands_map_;
    339     }
    340   } else {
    341     command_map = &null_group_commands_map_;
    342   }
    343 
    344   if (!command_map) {
    345     DVLOG(1) << "unsupported command group: " << GuidToString(*cmd_group_guid);
    346     return OLECMDERR_E_NOTSUPPORTED;
    347   }
    348 
    349   for (ULONG command_index = 0; command_index < number_of_commands;
    350        command_index++) {
    351     DVLOG(1) << "Command id = " << commands[command_index].cmdID;
    352     CommandStatusMap::iterator index =
    353         command_map->find(commands[command_index].cmdID);
    354     if (index != command_map->end())
    355       commands[command_index].cmdf = index->second;
    356   }
    357   return S_OK;
    358 }
    359 
    360 STDMETHODIMP ChromeActiveDocument::Exec(const GUID* cmd_group_guid,
    361                                         DWORD command_id,
    362                                         DWORD cmd_exec_opt,
    363                                         VARIANT* in_args,
    364                                         VARIANT* out_args) {
    365   DVLOG(1) << __FUNCTION__ << " Cmd id =" << command_id;
    366   // Bail out if we have been uninitialized.
    367   if (automation_client_.get() && automation_client_->tab()) {
    368     return ProcessExecCommand(cmd_group_guid, command_id, cmd_exec_opt,
    369                               in_args, out_args);
    370   } else if (command_id == OLECMDID_REFRESH && cmd_group_guid == NULL) {
    371     // If the automation server has crashed and the user is refreshing the
    372     // page, let OnRefreshPage attempt to recover.
    373     OnRefreshPage(cmd_group_guid, command_id, cmd_exec_opt, in_args, out_args);
    374   }
    375 
    376   return OLECMDERR_E_NOTSUPPORTED;
    377 }
    378 
    379 STDMETHODIMP ChromeActiveDocument::LoadHistory(IStream* stream,
    380                                                IBindCtx* bind_context) {
    381   // Read notes in ChromeActiveDocument::SaveHistory
    382   DCHECK(stream);
    383   LARGE_INTEGER offset = {0};
    384   ULARGE_INTEGER cur_pos = {0};
    385   STATSTG statstg = {0};
    386 
    387   stream->Seek(offset, STREAM_SEEK_CUR, &cur_pos);
    388   stream->Stat(&statstg, STATFLAG_NONAME);
    389 
    390   DWORD url_size = statstg.cbSize.LowPart - cur_pos.LowPart;
    391   base::win::ScopedBstr url_bstr;
    392   DWORD bytes_read = 0;
    393   stream->Read(url_bstr.AllocateBytes(url_size), url_size, &bytes_read);
    394   std::wstring url(url_bstr);
    395 
    396   ChromeFrameUrl cf_url;
    397   if (!cf_url.Parse(url)) {
    398     DLOG(WARNING) << __FUNCTION__ << " Failed to parse url:" << url;
    399     return E_INVALIDARG;
    400   }
    401 
    402   if (!LaunchUrl(cf_url, std::string())) {
    403     NOTREACHED() << __FUNCTION__ << " Failed to launch url:" << url;
    404     return E_INVALIDARG;
    405   }
    406   return S_OK;
    407 }
    408 
    409 STDMETHODIMP ChromeActiveDocument::SaveHistory(IStream* stream) {
    410   // TODO(sanjeevr): We need to fetch the entire list of navigation entries
    411   // from Chrome and persist it in the stream. And in LoadHistory we need to
    412   // pass this list back to Chrome which will recreate the list. This will allow
    413   // Back-Forward navigation to anchors to work correctly when we navigate to a
    414   // page outside of ChromeFrame and then come back.
    415   if (!stream) {
    416     NOTREACHED();
    417     return E_INVALIDARG;
    418   }
    419 
    420   LARGE_INTEGER offset = {0};
    421   ULARGE_INTEGER new_pos = {0};
    422   DWORD written = 0;
    423   std::wstring url = UTF8ToWide(navigation_info_->url.spec());
    424   return stream->Write(url.c_str(), (url.length() + 1) * sizeof(wchar_t),
    425                        &written);
    426 }
    427 
    428 STDMETHODIMP ChromeActiveDocument::SetPositionCookie(DWORD position_cookie) {
    429   if (automation_client_.get()) {
    430     int index = static_cast<int>(position_cookie);
    431     navigation_info_->navigation_index = index;
    432     automation_client_->NavigateToIndex(index);
    433   } else {
    434     DLOG(WARNING) << "Invalid automation client instance";
    435   }
    436   return S_OK;
    437 }
    438 
    439 STDMETHODIMP ChromeActiveDocument::GetPositionCookie(DWORD* position_cookie) {
    440   if (!position_cookie)
    441     return E_INVALIDARG;
    442 
    443   *position_cookie = navigation_info_->navigation_index;
    444   return S_OK;
    445 }
    446 
    447 STDMETHODIMP ChromeActiveDocument::GetUrlForEvents(BSTR* url) {
    448   if (NULL == url)
    449     return E_POINTER;
    450   *url = ::SysAllocString(url_);
    451   return S_OK;
    452 }
    453 
    454 STDMETHODIMP ChromeActiveDocument::GetAddressBarUrl(BSTR* url) {
    455   return GetUrlForEvents(url);
    456 }
    457 
    458 STDMETHODIMP ChromeActiveDocument::Reset() {
    459   next_privacy_record_ = privacy_info_.privacy_records.begin();
    460   return S_OK;
    461 }
    462 
    463 STDMETHODIMP ChromeActiveDocument::GetSize(DWORD* size) {
    464   if (!size)
    465     return E_POINTER;
    466 
    467   *size = privacy_info_.privacy_records.size();
    468   return S_OK;
    469 }
    470 
    471 STDMETHODIMP ChromeActiveDocument::GetPrivacyImpacted(BOOL* privacy_impacted) {
    472   if (!privacy_impacted)
    473     return E_POINTER;
    474 
    475   *privacy_impacted = privacy_info_.privacy_impacted;
    476   return S_OK;
    477 }
    478 
    479 STDMETHODIMP ChromeActiveDocument::Next(BSTR* url, BSTR* policy,
    480                                         LONG* reserved, DWORD* flags) {
    481   if (!url || !policy || !flags)
    482     return E_POINTER;
    483 
    484   if (next_privacy_record_ == privacy_info_.privacy_records.end())
    485     return HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS);
    486 
    487   *url = SysAllocString(next_privacy_record_->first.c_str());
    488   *policy = SysAllocString(next_privacy_record_->second.policy_ref.c_str());
    489   *flags = next_privacy_record_->second.flags;
    490 
    491   next_privacy_record_++;
    492   return S_OK;
    493 }
    494 
    495 bool ChromeActiveDocument::IsSchemeAllowed(const GURL& url) {
    496   bool allowed = BaseActiveX::IsSchemeAllowed(url);
    497   if (allowed)
    498     return true;
    499 
    500   if (url.SchemeIs(chrome::kAboutScheme)) {
    501     if (LowerCaseEqualsASCII(url.spec(), chrome::kAboutPluginsURL))
    502       return true;
    503   }
    504   return false;
    505 }
    506 
    507 HRESULT ChromeActiveDocument::GetInPlaceFrame(
    508     IOleInPlaceFrame** in_place_frame) {
    509   DCHECK(in_place_frame);
    510   if (in_place_frame_) {
    511     *in_place_frame = in_place_frame_.get();
    512     (*in_place_frame)->AddRef();
    513     return S_OK;
    514   } else {
    515     return S_FALSE;
    516   }
    517 }
    518 
    519 HRESULT ChromeActiveDocument::IOleObject_SetClientSite(
    520     IOleClientSite* client_site) {
    521   if (client_site == NULL) {
    522     base::win::ScopedComPtr<IDocHostUIHandler> doc_host_handler;
    523     if (doc_site_)
    524       doc_host_handler.QueryFrom(doc_site_);
    525 
    526     if (doc_host_handler.get())
    527       doc_host_handler->HideUI();
    528 
    529     doc_site_.Release();
    530   }
    531 
    532   if (client_site != m_spClientSite)
    533     return BaseActiveX::IOleObject_SetClientSite(client_site);
    534 
    535   return S_OK;
    536 }
    537 
    538 HRESULT ChromeActiveDocument::ActiveXDocActivate(LONG verb) {
    539   HRESULT hr = S_OK;
    540   m_bNegotiatedWnd = TRUE;
    541   if (!m_bInPlaceActive) {
    542     hr = m_spInPlaceSite->CanInPlaceActivate();
    543     if (FAILED(hr))
    544       return hr;
    545     m_spInPlaceSite->OnInPlaceActivate();
    546   }
    547   m_bInPlaceActive = TRUE;
    548   // get location in the parent window,
    549   // as well as some information about the parent
    550   base::win::ScopedComPtr<IOleInPlaceUIWindow> in_place_ui_window;
    551   frame_info_.cb = sizeof(OLEINPLACEFRAMEINFO);
    552   HWND parent_window = NULL;
    553   if (m_spInPlaceSite->GetWindow(&parent_window) == S_OK) {
    554     in_place_frame_.Release();
    555     RECT position_rect = {0};
    556     RECT clip_rect = {0};
    557     m_spInPlaceSite->GetWindowContext(in_place_frame_.Receive(),
    558                                       in_place_ui_window.Receive(),
    559                                       &position_rect,
    560                                       &clip_rect,
    561                                       &frame_info_);
    562     if (!m_bWndLess) {
    563       if (IsWindow()) {
    564         ::ShowWindow(m_hWnd, SW_SHOW);
    565         SetFocus();
    566       } else {
    567         m_hWnd = Create(parent_window, position_rect);
    568         if (!IsWindow()) {
    569           // This might happen if the automation server couldn't be
    570           // instantiated.  If so, a NOTREACHED() will have already been hit.
    571           DLOG(ERROR) << "Failed to create Ax window";
    572           return AtlHresultFromLastError();
    573         }
    574       }
    575     }
    576     SetObjectRects(&position_rect, &clip_rect);
    577   }
    578 
    579   base::win::ScopedComPtr<IOleInPlaceActiveObject> in_place_active_object(this);
    580 
    581   // Gone active by now, take care of UIACTIVATE
    582   if (DoesVerbUIActivate(verb)) {
    583     if (!m_bUIActive) {
    584       m_bUIActive = TRUE;
    585       hr = m_spInPlaceSite->OnUIActivate();
    586       if (FAILED(hr))
    587         return hr;
    588       // set ourselves up in the host
    589       if (in_place_active_object) {
    590         if (in_place_frame_)
    591           in_place_frame_->SetActiveObject(in_place_active_object, NULL);
    592         if (in_place_ui_window)
    593           in_place_ui_window->SetActiveObject(in_place_active_object, NULL);
    594       }
    595     }
    596   }
    597   m_spClientSite->ShowObject();
    598   return S_OK;
    599 }
    600 
    601 void ChromeActiveDocument::OnNavigationStateChanged(
    602     int flags, const NavigationInfo& nav_info) {
    603   // TODO(joshia): handle INVALIDATE_TYPE_TAB,INVALIDATE_TYPE_LOAD etc.
    604   DVLOG(1) << __FUNCTION__
    605            << "\n Flags: " << flags
    606            << ", Url: " << nav_info.url
    607            << ", Title: " << nav_info.title
    608            << ", Type: " << nav_info.navigation_type
    609            << ", Relative Offset: " << nav_info.relative_offset
    610            << ", Index: " << nav_info.navigation_index;
    611 
    612   UpdateNavigationState(nav_info, flags);
    613 }
    614 
    615 void ChromeActiveDocument::OnUpdateTargetUrl(
    616     const std::wstring& new_target_url) {
    617   if (in_place_frame_)
    618     in_place_frame_->SetStatusText(new_target_url.c_str());
    619 }
    620 
    621 bool IsFindAccelerator(const MSG& msg) {
    622   // TODO(robertshield): This may not stand up to localization. Fix if this
    623   // is the case.
    624   return msg.message == WM_KEYDOWN && msg.wParam == 'F' &&
    625          base::win::IsCtrlPressed() &&
    626          !(base::win::IsAltPressed() || base::win::IsShiftPressed());
    627 }
    628 
    629 void ChromeActiveDocument::OnAcceleratorPressed(const MSG& accel_message) {
    630   if (::TranslateAccelerator(m_hWnd, accelerator_table_,
    631                              const_cast<MSG*>(&accel_message)))
    632     return;
    633 
    634   bool handled_accel = false;
    635   if (in_place_frame_ != NULL) {
    636     handled_accel = (S_OK == in_place_frame_->TranslateAcceleratorW(
    637         const_cast<MSG*>(&accel_message), 0));
    638   }
    639 
    640   if (!handled_accel) {
    641     if (IsFindAccelerator(accel_message)) {
    642       // Handle the showing of the find dialog explicitly.
    643       OnFindInPage();
    644     } else {
    645       BaseActiveX::OnAcceleratorPressed(accel_message);
    646     }
    647   } else {
    648     DVLOG(1) << "IE handled accel key " << accel_message.wParam;
    649   }
    650 }
    651 
    652 void ChromeActiveDocument::OnTabbedOut(bool reverse) {
    653   DVLOG(1) << __FUNCTION__;
    654   if (in_place_frame_) {
    655     MSG msg = { NULL, WM_KEYDOWN, VK_TAB };
    656     in_place_frame_->TranslateAcceleratorW(&msg, 0);
    657   }
    658 }
    659 
    660 void ChromeActiveDocument::OnDidNavigate(const NavigationInfo& nav_info) {
    661   DVLOG(1) << __FUNCTION__ << std::endl
    662            << "Url: " << nav_info.url
    663            << ", Title: " << nav_info.title
    664            << ", Type: " << nav_info.navigation_type
    665            << ", Relative Offset: " << nav_info.relative_offset
    666            << ", Index: " << nav_info.navigation_index;
    667 
    668   CrashMetricsReporter::GetInstance()->IncrementMetric(
    669       CrashMetricsReporter::CHROME_FRAME_NAVIGATION_COUNT);
    670 
    671   UpdateNavigationState(nav_info, 0);
    672 }
    673 
    674 void ChromeActiveDocument::OnCloseTab() {
    675   // Base class will fire DIChromeFrameEvents::onclose.
    676   BaseActiveX::OnCloseTab();
    677 
    678   // Close the container window.
    679   base::win::ScopedComPtr<IWebBrowser2> web_browser2;
    680   DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
    681   if (web_browser2)
    682     web_browser2->Quit();
    683 }
    684 
    685 void ChromeActiveDocument::UpdateNavigationState(
    686     const NavigationInfo& new_navigation_info, int flags) {
    687   // This could be NULL if the active document instance is being destroyed.
    688   if (!m_spInPlaceSite) {
    689     DVLOG(1) << __FUNCTION__ << "m_spInPlaceSite is NULL. Returning";
    690     return;
    691   }
    692 
    693   HRESULT hr = S_OK;
    694   bool is_title_changed =
    695       (navigation_info_->title != new_navigation_info.title);
    696   bool is_ssl_state_changed =
    697       (navigation_info_->security_style !=
    698           new_navigation_info.security_style) ||
    699       (navigation_info_->displayed_insecure_content !=
    700           new_navigation_info.displayed_insecure_content) ||
    701       (navigation_info_->ran_insecure_content !=
    702           new_navigation_info.ran_insecure_content);
    703 
    704   if (is_ssl_state_changed) {
    705     int lock_status = SECURELOCK_SET_UNSECURE;
    706     switch (new_navigation_info.security_style) {
    707       case content::SECURITY_STYLE_AUTHENTICATED:
    708         lock_status = new_navigation_info.displayed_insecure_content ?
    709             SECURELOCK_SET_MIXED : SECURELOCK_SET_SECUREUNKNOWNBIT;
    710         break;
    711       default:
    712         break;
    713     }
    714 
    715     base::win::ScopedVariant secure_lock_status(lock_status);
    716     IEExec(&CGID_ShellDocView, INTERNAL_CMDID_SET_SSL_LOCK,
    717            OLECMDEXECOPT_DODEFAULT, secure_lock_status.AsInput(), NULL);
    718   }
    719 
    720   // A number of poorly written bho's crash in their event sink callbacks if
    721   // chrome frame is the currently loaded document. This is because they expect
    722   // chrome frame to implement interfaces like IHTMLDocument, etc. We patch the
    723   // event sink's of these bho's and don't invoke the event sink if chrome
    724   // frame is the currently loaded document.
    725   if (GetConfigBool(true, kEnableBuggyBhoIntercept)) {
    726     base::win::ScopedComPtr<IWebBrowser2> wb2;
    727     DoQueryService(SID_SWebBrowserApp, m_spClientSite, wb2.Receive());
    728     if (wb2 && buggy_bho::BuggyBhoTls::GetInstance()) {
    729       buggy_bho::BuggyBhoTls::GetInstance()->PatchBuggyBHOs(wb2);
    730     }
    731   }
    732 
    733   // Ideally all navigations should come to Chrome Frame so that we can call
    734   // BeforeNavigate2 on installed BHOs and give them a chance to cancel the
    735   // navigation. However, in practice what happens is as below:
    736   // The very first navigation that happens in CF happens via a Load or a
    737   // LoadHistory call. In this case, IE already has the correct information for
    738   // its travel log as well address bar. For other internal navigations (navs
    739   // that only happen within Chrome such as anchor navigations) we need to
    740   // update IE's internal state after the fact. In the case of internal
    741   // navigations, we notify the BHOs but ignore the should_cancel flag.
    742 
    743   // Another case where we need to issue BeforeNavigate2 calls is as below:-
    744   // We get notified after the fact, when navigations are initiated within
    745   // Chrome via window.open calls. These navigations are handled by creating
    746   // an external tab container within chrome and then connecting to it from IE.
    747   // We still want to update the address bar/history, etc, to ensure that
    748   // the special URL used by Chrome to indicate this is updated correctly.
    749   ChromeFrameUrl cf_url;
    750   bool is_attach_external_tab_url = cf_url.Parse(std::wstring(url_)) &&
    751       cf_url.attach_to_external_tab();
    752 
    753   bool is_internal_navigation =
    754       IsNewNavigation(new_navigation_info, flags) || is_attach_external_tab_url;
    755 
    756   if (new_navigation_info.url.is_valid())
    757     url_.Allocate(UTF8ToWide(new_navigation_info.url.spec()).c_str());
    758 
    759   if (is_internal_navigation) {
    760     // IE6 does not support tabs. If Chrome sent us a window open request
    761     // indicating that the navigation needs to occur in a foreground tab or
    762     // a popup window, then we need to ensure that the new window in IE6 is
    763     // brought to the foreground.
    764     if (GetIEVersion() == IE_6 &&
    765         is_attach_external_tab_url &&
    766         (cf_url.disposition() == NEW_FOREGROUND_TAB ||
    767          cf_url.disposition() == NEW_POPUP)) {
    768       base::win::ScopedComPtr<IWebBrowser2> wb2;
    769       DoQueryService(SID_SWebBrowserApp, m_spClientSite, wb2.Receive());
    770       if (wb2)
    771         BaseActiveX::BringWebBrowserWindowToTop(wb2);
    772     }
    773     base::win::ScopedComPtr<IDocObjectService> doc_object_svc;
    774     base::win::ScopedComPtr<IWebBrowserEventsService> web_browser_events_svc;
    775 
    776     DoQueryService(__uuidof(web_browser_events_svc), m_spClientSite,
    777                    web_browser_events_svc.Receive());
    778 
    779     if (!web_browser_events_svc.get()) {
    780       DoQueryService(SID_SShellBrowser, m_spClientSite,
    781                      doc_object_svc.Receive());
    782     }
    783 
    784     // web_browser_events_svc can be NULL on IE6.
    785     if (web_browser_events_svc) {
    786       VARIANT_BOOL should_cancel = VARIANT_FALSE;
    787       web_browser_events_svc->FireBeforeNavigate2Event(&should_cancel);
    788     } else if (doc_object_svc) {
    789       BOOL should_cancel = FALSE;
    790       doc_object_svc->FireBeforeNavigate2(NULL, url_, 0, NULL, NULL, 0,
    791                                           NULL, FALSE, &should_cancel);
    792     }
    793 
    794     // We need to tell IE that we support navigation so that IE will query us
    795     // for IPersistHistory and call GetPositionCookie to save our navigation
    796     // index.
    797     base::win::ScopedVariant html_window(static_cast<IUnknown*>(
    798         static_cast<IHTMLWindow2*>(this)));
    799     IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0,
    800            html_window.AsInput(), NULL);
    801 
    802     // We pass the HLNF_INTERNALJUMP flag to INTERNAL_CMDID_FINALIZE_TRAVEL_LOG
    803     // since we want to make IE treat all internal navigations within this page
    804     // (including anchor navigations and subframe navigations) as anchor
    805     // navigations. This will ensure that IE calls GetPositionCookie
    806     // to save the current position cookie in the travel log and then call
    807     // SetPositionCookie when the user hits Back/Forward to come back here.
    808     base::win::ScopedVariant internal_navigation(HLNF_INTERNALJUMP);
    809     IEExec(&CGID_Explorer, INTERNAL_CMDID_FINALIZE_TRAVEL_LOG, 0,
    810            internal_navigation.AsInput(), NULL);
    811 
    812     // We no longer need to lie to IE. If we lie persistently to IE, then
    813     // IE reuses us for new navigations.
    814     IEExec(&CGID_DocHostCmdPriv, DOCHOST_DOCCANNAVIGATE, 0, NULL, NULL);
    815 
    816     if (doc_object_svc) {
    817       // Now call the FireNavigateCompleteEvent which makes IE update the text
    818       // in the address-bar.
    819       doc_object_svc->FireNavigateComplete2(this, 0);
    820       doc_object_svc->FireDocumentComplete(this, 0);
    821     } else if (web_browser_events_svc) {
    822       web_browser_events_svc->FireNavigateComplete2Event();
    823       web_browser_events_svc->FireDocumentCompleteEvent();
    824     }
    825   }
    826 
    827   if (is_title_changed) {
    828     base::win::ScopedVariant title(new_navigation_info.title.c_str());
    829     IEExec(NULL, OLECMDID_SETTITLE, OLECMDEXECOPT_DONTPROMPTUSER,
    830            title.AsInput(), NULL);
    831   }
    832 
    833   // There are cases in which we receive NavigationStateChanged events for
    834   // provisional loads. These events will contain a new URL but will not be
    835   // considered new navigations since their navigation_index is 0. For these
    836   // events, do not update the navigation_info_ state, as this will muck up the
    837   // travel log when the subsequent committed navigation event takes place.
    838   //
    839   // Given this filtering, also special-case first navigations since those
    840   // are needed to initially populate navigation_info_ to keep it in sync with
    841   // what was placed in the IE travel log when CF was first opened.
    842   //
    843   // Lastly, allow through navigation events that would neither affect the
    844   // travel log nor cause the page location to change, as these will
    845   // contain informational state (referrer, title, etc.) that we should
    846   // preserve.
    847   if (is_internal_navigation || IsFirstNavigation(new_navigation_info) ||
    848       new_navigation_info.url == navigation_info_->url) {
    849     // It is important that we only update the navigation_info_ after we have
    850     // finalized the travel log. This is because IE will ask for information
    851     // such as navigation index when the travel log is finalized and we need
    852     // supply the old index and not the new one.
    853     *navigation_info_ = new_navigation_info;
    854   }
    855 
    856   // Update the IE zone here. Ideally we would like to do it when the active
    857   // document is activated. However that does not work at times as the frame we
    858   // get there is not the actual frame which handles the command.
    859   IEExec(&CGID_Explorer, SBCMDID_MIXEDZONE, 0, NULL, NULL);
    860 }
    861 
    862 void ChromeActiveDocument::OnFindInPage() {
    863   TabProxy* tab = GetTabProxy();
    864   if (tab) {
    865     if (!find_dialog_.IsWindow())
    866       find_dialog_.Create(m_hWnd);
    867 
    868     find_dialog_.ShowWindow(SW_SHOW);
    869   }
    870 }
    871 
    872 void ChromeActiveDocument::OnViewSource() {
    873   DCHECK(navigation_info_->url.is_valid());
    874   HostNavigate(GURL(content::kViewSourceScheme + std::string(":") +
    875       navigation_info_->url.spec()), GURL(), NEW_WINDOW);
    876 }
    877 
    878 void ChromeActiveDocument::OnDetermineSecurityZone(const GUID* cmd_group_guid,
    879                                                    DWORD command_id,
    880                                                    DWORD cmd_exec_opt,
    881                                                    VARIANT* in_args,
    882                                                    VARIANT* out_args) {
    883   // Always return URLZONE_INTERNET since that is the Chrome's behaviour.
    884   // Correct step is to use MapUrlToZone().
    885   if (out_args != NULL) {
    886     out_args->vt = VT_UI4;
    887     out_args->ulVal = URLZONE_INTERNET;
    888   }
    889 }
    890 
    891 void ChromeActiveDocument::OnDisplayPrivacyInfo() {
    892   privacy_info_ = url_fetcher_->privacy_info();
    893   Reset();
    894   DoPrivacyDlg(m_hWnd, url_, this, TRUE);
    895 }
    896 
    897 void ChromeActiveDocument::OnGetZoomRange(const GUID* cmd_group_guid,
    898                                           DWORD command_id,
    899                                           DWORD cmd_exec_opt,
    900                                           VARIANT* in_args,
    901                                           VARIANT* out_args) {
    902   if (out_args != NULL) {
    903     out_args->vt = VT_I4;
    904     out_args->lVal = 0;
    905   }
    906 }
    907 
    908 void ChromeActiveDocument::OnSetZoomRange(const GUID* cmd_group_guid,
    909                                           DWORD command_id,
    910                                           DWORD cmd_exec_opt,
    911                                           VARIANT* in_args,
    912                                           VARIANT* out_args) {
    913   const int kZoomIn = 125;
    914   const int kZoomOut = 75;
    915 
    916   if (in_args && V_VT(in_args) == VT_I4 && IsValid()) {
    917     if (in_args->lVal == kZoomIn) {
    918       automation_client_->SetZoomLevel(content::PAGE_ZOOM_IN);
    919     } else if (in_args->lVal == kZoomOut) {
    920       automation_client_->SetZoomLevel(content::PAGE_ZOOM_OUT);
    921     } else {
    922       DLOG(WARNING) << "Unsupported zoom level:" << in_args->lVal;
    923     }
    924   }
    925 }
    926 
    927 void ChromeActiveDocument::OnUnload(const GUID* cmd_group_guid,
    928                                     DWORD command_id,
    929                                     DWORD cmd_exec_opt,
    930                                     VARIANT* in_args,
    931                                     VARIANT* out_args) {
    932   if (IsValid() && out_args) {
    933     // If the navigation is attempted to the url loaded in CF, with the only
    934     // difference being the addition of an anchor, IE will attempt to unload
    935     // the currently loaded document which basically would end up running
    936     // unload handlers on the currently loaded document thus rendering it non
    937     // functional. We handle this as below:-
    938     // The BHO receives the new url in the BeforeNavigate event.
    939     // We compare the non anchor portions of the url in the BHO with the loaded
    940     // url and if they match, we initiate a Chrome navigation to the url with
    941     // the anchor which works nicely.
    942     // We don't want to continue processing the unload in this case.
    943     // Note:-
    944     // IE handles these navigations by querying the loaded document for
    945     // IHTMLDocument which then handles the new navigation. That won't work for
    946     // us as we don't implement IHTMLDocument.
    947     NavigationManager* mgr = NavigationManager::GetThreadInstance();
    948     DLOG_IF(ERROR, !mgr) << "Couldn't get instance of NavigationManager";
    949     if (mgr) {
    950       ChromeFrameUrl url;
    951       url.Parse(mgr->url());
    952       if (url.gurl().has_ref()) {
    953         url_canon::Replacements<char> replacements;
    954         replacements.ClearRef();
    955 
    956         if (url.gurl().ReplaceComponents(replacements) ==
    957             GURL(static_cast<BSTR>(url_))) {
    958           // We want to reuse the existing automation client and channel for
    959           // initiating the new navigation. Setting the
    960           // is_automation_client_reused_ flag to true before calling the
    961           // LaunchUrl function achieves that.
    962           is_automation_client_reused_ = true;
    963           LaunchUrl(url, mgr->referrer());
    964           out_args->vt = VT_BOOL;
    965           out_args->boolVal = VARIANT_FALSE;
    966           return;
    967         }
    968       }
    969     }
    970     bool should_unload = true;
    971     automation_client_->OnUnload(&should_unload);
    972     out_args->vt = VT_BOOL;
    973     out_args->boolVal = should_unload ? VARIANT_TRUE : VARIANT_FALSE;
    974   }
    975 }
    976 
    977 void ChromeActiveDocument::OnAttachExternalTab(
    978     const AttachExternalTabParams& params) {
    979   if (!automation_client_.get()) {
    980     DLOG(WARNING) << "Invalid automation client instance";
    981     return;
    982   }
    983   DWORD flags = 0;
    984   if (params.user_gesture)
    985     flags = NWMF_USERREQUESTED;
    986   else if (popup_allowed_)
    987     flags = NWMF_USERALLOWED;
    988 
    989   HRESULT hr = S_OK;
    990   if (popup_manager_) {
    991     const std::wstring& url_wide = UTF8ToWide(params.url.spec());
    992     hr = popup_manager_->EvaluateNewWindow(url_wide.c_str(), NULL, url_,
    993         NULL, FALSE, flags, 0);
    994   }
    995   // Allow popup
    996   if (hr == S_OK) {
    997     BaseActiveX::OnAttachExternalTab(params);
    998     return;
    999   }
   1000 
   1001   automation_client_->BlockExternalTab(params.cookie);
   1002 }
   1003 
   1004 bool ChromeActiveDocument::PreProcessContextMenu(HMENU menu) {
   1005   base::win::ScopedComPtr<IBrowserService> browser_service;
   1006   base::win::ScopedComPtr<ITravelLog> travel_log;
   1007   GetBrowserServiceAndTravelLog(browser_service.Receive(),
   1008                                 travel_log.Receive());
   1009   if (!browser_service || !travel_log)
   1010     return true;
   1011 
   1012   EnableMenuItem(menu, IDC_BACK, MF_BYCOMMAND |
   1013       (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_BACK,
   1014                                             NULL)) ?
   1015       MF_ENABLED : MF_DISABLED));
   1016   EnableMenuItem(menu, IDC_FORWARD, MF_BYCOMMAND |
   1017       (SUCCEEDED(travel_log->GetTravelEntry(browser_service, TLOG_FORE,
   1018                                             NULL)) ?
   1019       MF_ENABLED : MF_DISABLED));
   1020   // Call base class (adds 'About' item)
   1021   return BaseActiveX::PreProcessContextMenu(menu);
   1022 }
   1023 
   1024 bool ChromeActiveDocument::HandleContextMenuCommand(
   1025     UINT cmd, const MiniContextMenuParams& params) {
   1026   base::win::ScopedComPtr<IWebBrowser2> web_browser2;
   1027   DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
   1028 
   1029   if (cmd == IDC_BACK)
   1030     web_browser2->GoBack();
   1031   else if (cmd == IDC_FORWARD)
   1032     web_browser2->GoForward();
   1033   else if (cmd == IDC_RELOAD)
   1034     web_browser2->Refresh();
   1035   else
   1036     return BaseActiveX::HandleContextMenuCommand(cmd, params);
   1037 
   1038   return true;
   1039 }
   1040 
   1041 HRESULT ChromeActiveDocument::IEExec(const GUID* cmd_group_guid,
   1042                                      DWORD command_id, DWORD cmd_exec_opt,
   1043                                      VARIANT* in_args, VARIANT* out_args) {
   1044   HRESULT hr = E_FAIL;
   1045 
   1046   base::win::ScopedComPtr<IOleCommandTarget> frame_cmd_target;
   1047 
   1048   base::win::ScopedComPtr<IOleInPlaceSite> in_place_site(m_spInPlaceSite);
   1049   if (!in_place_site.get() && m_spClientSite != NULL)
   1050     in_place_site.QueryFrom(m_spClientSite);
   1051 
   1052   if (in_place_site)
   1053     hr = frame_cmd_target.QueryFrom(in_place_site);
   1054 
   1055   if (frame_cmd_target) {
   1056     hr = frame_cmd_target->Exec(cmd_group_guid, command_id, cmd_exec_opt,
   1057                                 in_args, out_args);
   1058   }
   1059 
   1060   return hr;
   1061 }
   1062 
   1063 bool ChromeActiveDocument::LaunchUrl(const ChromeFrameUrl& cf_url,
   1064                                      const std::string& referrer) {
   1065   DCHECK(!cf_url.gurl().is_empty());
   1066 
   1067   if (!automation_client_.get()) {
   1068     // http://code.google.com/p/chromium/issues/detail?id=52894
   1069     // Still not sure how this happens.
   1070     DLOG(ERROR) << "No automation client!";
   1071     if (!Initialize()) {
   1072       NOTREACHED() << "...and failed to start a new one >:(";
   1073       return false;
   1074     }
   1075   }
   1076 
   1077   document_url_ = cf_url.gurl().spec();
   1078 
   1079   url_.Allocate(UTF8ToWide(cf_url.gurl().spec()).c_str());
   1080   if (cf_url.attach_to_external_tab()) {
   1081     automation_client_->AttachExternalTab(cf_url.cookie());
   1082     OnMoveWindow(cf_url.dimensions());
   1083   } else if (!automation_client_->InitiateNavigation(cf_url.gurl().spec(),
   1084                                                      referrer,
   1085                                                      this)) {
   1086     DLOG(ERROR) << "Invalid URL: " << url_;
   1087     Error(L"Invalid URL");
   1088     url_.Reset();
   1089     return false;
   1090   }
   1091 
   1092   if (is_automation_client_reused_)
   1093     return true;
   1094 
   1095   automation_client_->SetUrlFetcher(url_fetcher_.get());
   1096   if (launch_params_) {
   1097     return automation_client_->Initialize(this, launch_params_);
   1098   } else {
   1099     std::wstring profile = UTF8ToWide(cf_url.profile_name());
   1100     // If no profile was given, then make use of the host process's name.
   1101     if (profile.empty())
   1102       profile = GetHostProcessName(false);
   1103     return InitializeAutomation(profile, IsIEInPrivate(),
   1104                                 false, cf_url.gurl(), GURL(referrer),
   1105                                 false);
   1106   }
   1107 }
   1108 
   1109 HRESULT ChromeActiveDocument::OnRefreshPage(const GUID* cmd_group_guid,
   1110     DWORD command_id, DWORD cmd_exec_opt, VARIANT* in_args, VARIANT* out_args) {
   1111   DVLOG(1) << __FUNCTION__;
   1112   popup_allowed_ = false;
   1113   if (in_args->vt == VT_I4 &&
   1114       in_args->lVal & OLECMDIDF_REFRESH_PAGEACTION_POPUPWINDOW) {
   1115     popup_allowed_ = true;
   1116 
   1117     // Ask the yellow security band to change the text and icon and to remain
   1118     // visible.
   1119     IEExec(&CGID_DocHostCommandHandler, OLECMDID_PAGEACTIONBLOCKED,
   1120         0x80000000 | OLECMDIDF_WINDOWSTATE_USERVISIBLE_VALID, NULL, NULL);
   1121   }
   1122 
   1123   NavigationManager* mgr = NavigationManager::GetThreadInstance();
   1124   DLOG_IF(ERROR, !mgr) << "Couldn't get instance of NavigationManager";
   1125 
   1126   // If ChromeFrame was activated on this page as a result of a document
   1127   // received in response to a top level post, then we ask the user for
   1128   // permission to repost and issue a navigation with the saved post data
   1129   // which reinitates the whole sequence, i.e. the server receives the top
   1130   // level post and chrome frame will be reactivated in response.
   1131   if (mgr && mgr->post_data().type() != VT_EMPTY) {
   1132     if (MessageBox(
   1133             SimpleResourceLoader::Get(IDS_HTTP_POST_WARNING).c_str(),
   1134             SimpleResourceLoader::Get(IDS_HTTP_POST_WARNING_TITLE).c_str(),
   1135             MB_YESNO | MB_ICONEXCLAMATION) == IDYES) {
   1136       base::win::ScopedComPtr<IWebBrowser2> web_browser2;
   1137       DoQueryService(SID_SWebBrowserApp, m_spClientSite,
   1138                      web_browser2.Receive());
   1139       DCHECK(web_browser2);
   1140       VARIANT empty = base::win::ScopedVariant::kEmptyVariant;
   1141       VARIANT flags = { VT_I4 };
   1142       V_I4(&flags) = navNoHistory;
   1143 
   1144       return web_browser2->Navigate2(base::win::ScopedVariant(url_).AsInput(),
   1145                                      &flags,
   1146                                      &empty,
   1147                                      const_cast<VARIANT*>(&mgr->post_data()),
   1148                                      const_cast<VARIANT*>(&mgr->headers()));
   1149     } else {
   1150       return S_OK;
   1151     }
   1152   }
   1153 
   1154   TabProxy* tab_proxy = GetTabProxy();
   1155   if (tab_proxy) {
   1156     tab_proxy->ReloadAsync();
   1157   } else {
   1158     DLOG(ERROR) << "No automation proxy";
   1159     DCHECK(automation_client_.get() != NULL) << "how did it get freed?";
   1160     // The current url request manager (url_fetcher_) has been switched to
   1161     // a stopping state so we need to reset it and get a new one for the new
   1162     // automation server.
   1163     ResetUrlRequestManager();
   1164     url_fetcher_->set_frame_busting(false);
   1165     // And now launch the current URL again.  This starts a new server process.
   1166     DCHECK(navigation_info_->url.is_valid());
   1167     ChromeFrameUrl cf_url;
   1168     cf_url.Parse(UTF8ToWide(navigation_info_->url.spec()));
   1169     LaunchUrl(cf_url, navigation_info_->referrer.spec());
   1170   }
   1171 
   1172   return S_OK;
   1173 }
   1174 
   1175 HRESULT ChromeActiveDocument::SetPageFontSize(const GUID* cmd_group_guid,
   1176                                               DWORD command_id,
   1177                                               DWORD cmd_exec_opt,
   1178                                               VARIANT* in_args,
   1179                                               VARIANT* out_args) {
   1180   if (!automation_client_.get()) {
   1181     NOTREACHED() << "Invalid automation client";
   1182     return E_FAIL;
   1183   }
   1184 
   1185   switch (command_id) {
   1186     case IDM_BASELINEFONT1:
   1187       automation_client_->SetPageFontSize(SMALLEST_FONT);
   1188       break;
   1189 
   1190     case IDM_BASELINEFONT2:
   1191       automation_client_->SetPageFontSize(SMALL_FONT);
   1192       break;
   1193 
   1194     case IDM_BASELINEFONT3:
   1195       automation_client_->SetPageFontSize(MEDIUM_FONT);
   1196       break;
   1197 
   1198     case IDM_BASELINEFONT4:
   1199       automation_client_->SetPageFontSize(LARGE_FONT);
   1200       break;
   1201 
   1202     case IDM_BASELINEFONT5:
   1203       automation_client_->SetPageFontSize(LARGEST_FONT);
   1204       break;
   1205 
   1206     default:
   1207       NOTREACHED() << "Invalid font size command: "
   1208                   << command_id;
   1209       return E_FAIL;
   1210   }
   1211 
   1212   // Forward the command back to IEFrame with group set to
   1213   // CGID_ExplorerBarDoc. This is probably needed to update the menu state to
   1214   // indicate that the font size was set. This currently fails with error
   1215   // 0x80040104.
   1216   // TODO(iyengar)
   1217   // Do some investigation into why this Exec call fails.
   1218   IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
   1219   return S_OK;
   1220 }
   1221 
   1222 HRESULT ChromeActiveDocument::OnEncodingChange(const GUID* cmd_group_guid,
   1223                                                DWORD command_id,
   1224                                                DWORD cmd_exec_opt,
   1225                                                VARIANT* in_args,
   1226                                                VARIANT* out_args) {
   1227   const struct EncodingMapData {
   1228     DWORD ie_encoding_id;
   1229     const char* chrome_encoding_name;
   1230   } kEncodingTestDatas[] = {
   1231 #define DEFINE_ENCODING_MAP(encoding_name, id, chrome_name) \
   1232     { encoding_name, chrome_name },
   1233   INTERNAL_IE_ENCODINGMENU_IDS(DEFINE_ENCODING_MAP)
   1234 #undef DEFINE_ENCODING_MAP
   1235   };
   1236 
   1237   if (!automation_client_.get()) {
   1238     NOTREACHED() << "Invalid automtion client";
   1239     return E_FAIL;
   1240   }
   1241 
   1242   // Using ARRAYSIZE_UNSAFE in here is because we define the struct
   1243   // EncodingMapData inside function.
   1244   const char* chrome_encoding_name = NULL;
   1245   for (int i = 0; i < ARRAYSIZE_UNSAFE(kEncodingTestDatas); ++i) {
   1246     const struct EncodingMapData* encoding_data = &kEncodingTestDatas[i];
   1247     if (command_id == encoding_data->ie_encoding_id) {
   1248       chrome_encoding_name = encoding_data->chrome_encoding_name;
   1249       break;
   1250     }
   1251   }
   1252   // Return E_FAIL when encountering invalid encoding id.
   1253   if (!chrome_encoding_name)
   1254     return E_FAIL;
   1255 
   1256   TabProxy* tab = GetTabProxy();
   1257   if (!tab) {
   1258     NOTREACHED() << "Can not get TabProxy";
   1259     return E_FAIL;
   1260   }
   1261 
   1262   if (chrome_encoding_name)
   1263     tab->OverrideEncoding(chrome_encoding_name);
   1264 
   1265   // Like we did on SetPageFontSize, we may forward the command back to IEFrame
   1266   // to update the menu state to indicate that which encoding was set.
   1267   // TODO(iyengar)
   1268   // Do some investigation into why this Exec call fails.
   1269   IEExec(&CGID_ExplorerBarDoc, command_id, cmd_exec_opt, NULL, NULL);
   1270   return S_OK;
   1271 }
   1272 
   1273 void ChromeActiveDocument::OnGoToHistoryEntryOffset(int offset) {
   1274   DVLOG(1) <<  __FUNCTION__ << " - offset:" << offset;
   1275 
   1276   base::win::ScopedComPtr<IBrowserService> browser_service;
   1277   base::win::ScopedComPtr<ITravelLog> travel_log;
   1278   GetBrowserServiceAndTravelLog(browser_service.Receive(),
   1279                                 travel_log.Receive());
   1280 
   1281   if (browser_service && travel_log)
   1282     travel_log->Travel(browser_service, offset);
   1283 }
   1284 
   1285 void ChromeActiveDocument::OnMoveWindow(const gfx::Rect& dimensions) {
   1286   base::win::ScopedComPtr<IWebBrowser2> web_browser2;
   1287   DoQueryService(SID_SWebBrowserApp, m_spClientSite,
   1288                  web_browser2.Receive());
   1289   if (!web_browser2)
   1290     return;
   1291   DVLOG(1) << "this:" << this << "\ndimensions: width:" << dimensions.width()
   1292            << " height:" << dimensions.height();
   1293   if (!dimensions.IsEmpty()) {
   1294     web_browser2->put_MenuBar(VARIANT_FALSE);
   1295     web_browser2->put_ToolBar(VARIANT_FALSE);
   1296 
   1297     int width = dimensions.width();
   1298     int height = dimensions.height();
   1299     // Compute the size of the browser window given the desired size of the
   1300     // content area. As per MSDN, the WebBrowser object returns an error from
   1301     // this method. As a result the code below is best effort.
   1302     web_browser2->ClientToWindow(&width, &height);
   1303 
   1304     web_browser2->put_Width(width);
   1305     web_browser2->put_Height(height);
   1306     web_browser2->put_Left(dimensions.x());
   1307     web_browser2->put_Top(dimensions.y());
   1308   }
   1309 }
   1310 
   1311 HRESULT ChromeActiveDocument::GetBrowserServiceAndTravelLog(
   1312     IBrowserService** browser_service, ITravelLog** travel_log) {
   1313   DCHECK(browser_service || travel_log);
   1314   base::win::ScopedComPtr<IBrowserService> browser_service_local;
   1315   HRESULT hr = DoQueryService(SID_SShellBrowser, m_spClientSite,
   1316                               browser_service_local.Receive());
   1317   if (!browser_service_local) {
   1318     NOTREACHED() << "DoQueryService for IBrowserService failed: " << hr;
   1319     return hr;
   1320   }
   1321 
   1322   if (travel_log) {
   1323     hr = browser_service_local->GetTravelLog(travel_log);
   1324     DVLOG_IF(1, !travel_log) << "browser_service->GetTravelLog failed: " << hr;
   1325   }
   1326 
   1327   if (browser_service)
   1328     *browser_service = browser_service_local.Detach();
   1329 
   1330   return hr;
   1331 }
   1332 
   1333 LRESULT ChromeActiveDocument::OnForward(WORD notify_code, WORD id,
   1334                                         HWND control_window,
   1335                                         BOOL& bHandled) {
   1336   base::win::ScopedComPtr<IWebBrowser2> web_browser2;
   1337   DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
   1338   DCHECK(web_browser2);
   1339 
   1340   if (web_browser2)
   1341     web_browser2->GoForward();
   1342   return 0;
   1343 }
   1344 
   1345 LRESULT ChromeActiveDocument::OnBack(WORD notify_code, WORD id,
   1346                                      HWND control_window,
   1347                                      BOOL& bHandled) {
   1348   base::win::ScopedComPtr<IWebBrowser2> web_browser2;
   1349   DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
   1350   DCHECK(web_browser2);
   1351 
   1352   if (web_browser2)
   1353     web_browser2->GoBack();
   1354   return 0;
   1355 }
   1356 
   1357 LRESULT ChromeActiveDocument::OnFirePrivacyChange(UINT message, WPARAM wparam,
   1358                                                   LPARAM lparam,
   1359                                                   BOOL& handled) {
   1360   if (!m_spClientSite)
   1361     return 0;
   1362 
   1363   base::win::ScopedComPtr<IWebBrowser2> web_browser2;
   1364   DoQueryService(SID_SWebBrowserApp, m_spClientSite,
   1365                  web_browser2.Receive());
   1366   if (!web_browser2) {
   1367     NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
   1368     return 0;
   1369   }
   1370 
   1371   base::win::ScopedComPtr<IShellBrowser> shell_browser;
   1372   DoQueryService(SID_STopLevelBrowser, web_browser2,
   1373                  shell_browser.Receive());
   1374   DCHECK(shell_browser.get() != NULL);
   1375   base::win::ScopedComPtr<ITridentService2> trident_services;
   1376   trident_services.QueryFrom(shell_browser);
   1377   if (trident_services)
   1378     trident_services->FirePrivacyImpactedStateChange(wparam);
   1379   else
   1380     NOTREACHED() << "Failed to retrieve IWebBrowser2 interface.";
   1381   return 0;
   1382 }
   1383 
   1384 LRESULT ChromeActiveDocument::OnShowWindow(UINT message, WPARAM wparam,
   1385                                            LPARAM lparam,
   1386                                            BOOL& handled) {  // NO_LINT
   1387   if (wparam)
   1388     SetFocus();
   1389   return 0;
   1390 }
   1391 
   1392 LRESULT ChromeActiveDocument::OnSetFocus(UINT message, WPARAM wparam,
   1393                                          LPARAM lparam,
   1394                                          BOOL& handled) {  // NO_LINT
   1395   if (!ignore_setfocus_)
   1396     GiveFocusToChrome(false);
   1397   return 0;
   1398 }
   1399 
   1400 bool ChromeActiveDocument::IsNewNavigation(
   1401     const NavigationInfo& new_navigation_info, int flags) const {
   1402   // A new navigation is typically an internal navigation which is initiated by
   1403   // the renderer(WebKit). Condition 1 below has to be true along with the
   1404   // any of the other conditions below.
   1405   // 1. The navigation notification flags passed in as the flags parameter
   1406   //    is not INVALIDATE_TYPE_LOAD which indicates that the loading state of
   1407   //    the tab changed.
   1408   // 2. The navigation index is greater than 0 which means that a top level
   1409   //    navigation was initiated on the current external tab.
   1410   // 3. The navigation type has changed.
   1411   // 4. The url or the referrer are different.
   1412   if (flags == content::INVALIDATE_TYPE_LOAD)
   1413     return false;
   1414 
   1415   if (new_navigation_info.navigation_index <= 0)
   1416     return false;
   1417 
   1418   if (new_navigation_info.navigation_index ==
   1419       navigation_info_->navigation_index)
   1420     return false;
   1421 
   1422   if (new_navigation_info.navigation_type != navigation_info_->navigation_type)
   1423     return true;
   1424 
   1425   if (new_navigation_info.url != navigation_info_->url)
   1426     return true;
   1427 
   1428   if (new_navigation_info.referrer != navigation_info_->referrer)
   1429     return true;
   1430 
   1431   return false;
   1432 }
   1433 
   1434 bool ChromeActiveDocument::IsFirstNavigation(
   1435     const NavigationInfo& new_navigation_info) const {
   1436   return (navigation_info_->url.is_empty() &&
   1437           new_navigation_info.navigation_type ==
   1438               content::NAVIGATION_TYPE_NEW_PAGE);
   1439 }
   1440