agent/browser/ie/pagetest/PagetestReporting.cpp
/*-----------------------------------------------------------------------------
	If we were asked to, walk the page and save any links on it
-----------------------------------------------------------------------------*/
If we were asked to, walk the page and save any links on it
If we were asked to walk the page and save any links on it
void CPagetestReporting::SaveUrls(void)
{
	if( !linksFile.IsEmpty() )
	{
		CAtlList<CStringA> urls;
		POSITION pos = browsers.GetHeadPosition();
		while(pos)
		{
			CBrowserTracker tracker = browsers.GetNext(pos);
			if( tracker.threadId == GetCurrentThreadId() && tracker.browser )
			{
				CComPtr<IDispatch> spDoc;
				if( SUCCEEDED(tracker.browser->get_Document(&spDoc)) && spDoc )
				{
					CComQIPtr<IHTMLDocument2> doc = spDoc;
          if( doc )
          {
					  GetLinks( doc, urls);
					  CComQIPtr<IOleContainer> ole(doc);
					  if(ole)
					  {
						  CComPtr<IEnumUnknown> objects;
						  if( SUCCEEDED(ole->EnumObjects(OLECONTF_EMBEDDINGS, &objects)) && objects )
						  {
							  IUnknown* pUnk;
							  ULONG uFetched;
							  while( S_OK == objects->Next(1, &pUnk, &uFetched) )
							  {
								  CComQIPtr<IWebBrowser2> browser(pUnk);
								  pUnk->Release();
								  if (browser)
								  {
									  CComPtr<IDispatch> disp;
									  if( SUCCEEDED(browser->get_Document(&disp)) && disp )
									  {
										  CComQIPtr<IHTMLDocument2> frameDoc(disp);
										  if (frameDoc)
											  GetLinks( frameDoc, urls);
									  }
								  }
							  }
						  }
					  }			
					  CComPtr<IHTMLFramesCollection2> frames;
					  if( SUCCEEDED(doc->get_frames(&frames)) && frames )
					  {
						  long count = 0;
						  if( SUCCEEDED(frames->get_length(&count)) )
						  {
							  for( long i = 0; i < count; i++ )
							  {
								  _variant_t index = i;
								  _variant_t varFrame;
								  if( SUCCEEDED(frames->item(&index, &varFrame)) )
								  {
									  CComQIPtr<IHTMLWindow2> window(varFrame);
									  if( window )
									  {
										  CComQIPtr<IHTMLDocument2> frameDoc;
										  if( SUCCEEDED(window->get_document(&frameDoc)) && frameDoc )
											  GetLinks( frameDoc, urls);
									  }
								  }
							  }
						  }
					  }
          }
				}
			}
		}
		if( !urls.IsEmpty() )
		{
			HANDLE hFile = CreateFile(linksFile, GENERIC_WRITE, 0, &nullDacl, CREATE_ALWAYS, 0, 0);
			if( hFile != INVALID_HANDLE_VALUE )
			{
				POSITION pos = urls.GetHeadPosition();
				while( pos )
				{
					CStringA linkUrl = urls.GetNext(pos);
					linkUrl += "\r\n";
					DWORD bytes;
					WriteFile(hFile, (LPCSTR)linkUrl, linkUrl.GetLength(), &bytes, 0);
				}
				CloseHandle(hFile);
			}
		}
	}
}
void CPagetestReporting :: SaveUrls ( void ) { if ( ! linksFile . IsEmpty ( ) ) { CAtlList < CStringA > urls ; POSITION pos = browsers . GetHeadPosition ( ) ; while ( pos ) { CBrowserTracker tracker = browsers . GetNext ( pos ) ; if ( tracker . threadId == GetCurrentThreadId ( ) && tracker . browser ) { CComPtr < IDispatch > spDoc ; if ( SUCCEEDED ( tracker . browser -> get_Document ( & spDoc ) ) && spDoc ) { CComQIPtr < IHTMLDocument2 > doc = spDoc ; if ( doc ) { GetLinks ( doc , urls ) ; CComQIPtr < IOleContainer > ole ( doc ) ; if ( ole ) { CComPtr < IEnumUnknown > objects ; if ( SUCCEEDED ( ole -> EnumObjects ( OLECONTF_EMBEDDINGS , & objects ) ) && objects ) { IUnknown * pUnk ; ULONG uFetched ; while ( S_OK == objects -> Next ( 1 , & pUnk , & uFetched ) ) { CComQIPtr < IWebBrowser2 > browser ( pUnk ) ; pUnk -> Release ( ) ; if ( browser ) { CComPtr < IDispatch > disp ; if ( SUCCEEDED ( browser -> get_Document ( & disp ) ) && disp ) { CComQIPtr < IHTMLDocument2 > frameDoc ( disp ) ; if ( frameDoc ) GetLinks ( frameDoc , urls ) ; } } } } } CComPtr < IHTMLFramesCollection2 > frames ; if ( SUCCEEDED ( doc -> get_frames ( & frames ) ) && frames ) { long count = 0 ; if ( SUCCEEDED ( frames -> get_length ( & count ) ) ) { for ( long i = 0 ; i < count ; i ++ ) { _variant_t index = i ; _variant_t varFrame ; if ( SUCCEEDED ( frames -> item ( & index , & varFrame ) ) ) { CComQIPtr < IHTMLWindow2 > window ( varFrame ) ; if ( window ) { CComQIPtr < IHTMLDocument2 > frameDoc ; if ( SUCCEEDED ( window -> get_document ( & frameDoc ) ) && frameDoc ) GetLinks ( frameDoc , urls ) ; } } } } } } } } } if ( ! urls . IsEmpty ( ) ) { HANDLE hFile = CreateFile ( linksFile , GENERIC_WRITE , 0 , & nullDacl , CREATE_ALWAYS , 0 , 0 ) ; if ( hFile != INVALID_HANDLE_VALUE ) { POSITION pos = urls . GetHeadPosition ( ) ; while ( pos ) { CStringA linkUrl = urls . GetNext ( pos ) ; linkUrl += " \r \n " ; DWORD bytes ; WriteFile ( hFile , ( LPCSTR ) linkUrl , linkUrl . GetLength ( ) , & bytes , 0 ) ; } CloseHandle ( hFile ) ; } } } }
If we were asked to, walk the page and save any links on it
If we were asked to walk the page and save any links on it
// walk through all browser windows
 // get the collection of links on the main document
 // walk all of the frames on the document using OLE
 // this is a little complicated because we need to bypass cross site scripting security
 // Get an enumerator for the frames
 // Enumerate all the frames
 // QI for IWebBrowser here to see if we have an embedded browser
 // walk all of the frames on the document using native support (OLE doesn't seem to grab them all)
 // for each frame, walk all of the elements in the frame
 // save out the urls that were found
