Example #1
0
//----------------------------------------------------------------------------
//
HRESULT CAnchoAddonService::navigateBrowser(LPUNKNOWN aWebBrowserWin, const std::wstring &url, INT32 aNavigateOptions)
{
  CComQIPtr<IWebBrowser2> webBrowser = aWebBrowserWin;
  if (!webBrowser) {
    return E_NOINTERFACE;
  }

  CComVariant vtUrl(url.c_str());
  CComVariant vtFlags(aNavigateOptions);
  CComVariant vtEmpty;
  return webBrowser->Navigate2(&vtUrl, &vtFlags, &vtEmpty, &vtEmpty, &vtEmpty);
}
Example #2
0
//----------------------------------------------------------------------------
//  OnBrowserBeforeNavigate2
STDMETHODIMP_(void) CAnchoRuntime::OnBrowserBeforeNavigate2(LPDISPATCH pDisp, VARIANT *pURL, VARIANT *Flags,
  VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers, BOOL *Cancel)
{
  static bool bFirstRun = true;

  // Add the frame to the frames map so we can retrieve the IWebBrowser2 object using the URL.
  ATLASSERT(pURL->vt == VT_BSTR && pURL->bstrVal != NULL);
  CComQIPtr<IWebBrowser2> pWebBrowser(pDisp);
  ATLASSERT(pWebBrowser != NULL);

  // Workaround to ensure that first request goes through PAPP
  if (bFirstRun) {
    bFirstRun = false;
    *Cancel = TRUE;
    pWebBrowser->Stop();
    pWebBrowser->Navigate2(pURL, Flags, TargetFrameName, PostData, Headers);
    return;
  }

  // Check if this is a new tab we are creating programmatically.
  // If so redirect it to the correct URL.
  std::wstring url(pURL->bstrVal, SysStringLen(pURL->bstrVal));

  boost::wregex expression(L"(.*)://\\$\\$([0-9]+)\\$\\$(.*)");
  boost::wsmatch what;
  //TODO - find a better way
  if (boost::regex_match(url, what, expression)) {
    int requestId = boost::lexical_cast<int>(what[2].str());
    url = boost::str(boost::wformat(L"%1%://%2%") % what[1] % what[3]);

    _variant_t vtUrl(url.c_str());
    *Cancel = TRUE;
    pWebBrowser->Stop();
    pWebBrowser->Navigate2(&vtUrl.GetVARIANT(), Flags, TargetFrameName, PostData, Headers);
    mTabManager->createTabNotification(m_TabID, requestId);
    return;
  }


  VARIANT_BOOL isTop;
  if (SUCCEEDED(pWebBrowser->get_TopLevelContainer(&isTop))) {
    if (isTop) {
      // Loading the main frame so reset the frame list.
      m_Frames.clear();
      m_NextFrameId = 0;
    }
  }
  CComBSTR bstrUrl;
  removeUrlFragment(pURL->bstrVal, &bstrUrl);
  m_Frames[(BSTR) bstrUrl] = FrameRecord(pWebBrowser, isTop != VARIANT_FALSE, m_NextFrameId++);

  pWebBrowser->PutProperty(CComBSTR(L"_anchoNavigateURL"), CComVariant(*pURL));

  SHANDLE_PTR hwndBrowser = NULL;
  pWebBrowser->get_HWND(&hwndBrowser);

  if (isTop) {
    //Fill information about current document
    CComPtr<IDispatch> tmp;
    pWebBrowser->get_Document(&tmp);
    CComQIPtr<IHTMLDocument2> doc = tmp;
    if (doc) {
      CComQIPtr<IOleWindow> docWin = doc;
      HWND docWinHWND = NULL;
      if (docWin) {
        docWin->GetWindow(&docWinHWND);
      }
      if (docWinHWND) {
        gWindowDocumentMap.put(WindowDocumentRecord(docWinHWND, m_TabID, m_pWebBrowser, pWebBrowser, doc));
      }
    }
    HWND tabWindow = getTabWindow();
    if (tabWindow) {
      gWindowDocumentMap.put(WindowDocumentRecord(tabWindow, m_TabID, m_pWebBrowser, pWebBrowser, doc));
    }
  }

}