1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chak Nanga <chak@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// File Overview....
//
// When the CBrowserFrm creates this View:
// - CreateBrowser() is called in OnCreate() to create the
// mozilla embeddable browser
//
// OnSize() method handles the window resizes and calls the approriate
// interface method to resize the embedded browser properly
//
// Command handlers to handle browser navigation - OnNavBack(),
// OnNavForward() etc
//
// DestroyBrowser() called for cleaning up during object destruction
//
// Some important coding notes....
//
// 1. Make sure we do not have the CS_HREDRAW|CS_VREDRAW in the call
// to AfxRegisterWndClass() inside of PreCreateWindow() below
// If these flags are present then you'll see screen flicker when
// you resize the frame window
//
// Next suggested file to look at : BrowserImpl.cpp
//
#include "stdafx.h"
#include "MfcEmbed.h"
#include "BrowserView.h"
#include "BrowserImpl.h"
#include "BrowserFrm.h"
#include "Dialogs.h"
#include "UrlDialog.h"
#include "ProfileMgr.h"
#include "ProfilesDlg.h"
// Print Includes
#include "PrintProgressDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Register message for FindDialog communication
static UINT WM_FINDMSG = ::RegisterWindowMessage(FINDMSGSTRING);
BEGIN_MESSAGE_MAP(CBrowserView, CWnd)
//{{AFX_MSG_MAP(CBrowserView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_CBN_SELENDOK(ID_URL_BAR, OnUrlSelectedInUrlBar)
ON_COMMAND(IDOK, OnNewUrlEnteredInUrlBar)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
ON_COMMAND(ID_VIEW_SOURCE, OnViewSource)
ON_COMMAND(ID_VIEW_INFO, OnViewInfo)
ON_COMMAND(ID_NAV_BACK, OnNavBack)
ON_COMMAND(ID_NAV_FORWARD, OnNavForward)
ON_COMMAND(ID_NAV_HOME, OnNavHome)
ON_COMMAND(ID_NAV_RELOAD, OnNavReload)
ON_COMMAND(ID_NAV_STOP, OnNavStop)
ON_COMMAND(ID_EDIT_CUT, OnCut)
ON_COMMAND(ID_EDIT_COPY, OnCopy)
ON_COMMAND(ID_EDIT_PASTE, OnPaste)
ON_COMMAND(ID_EDIT_UNDO, OnUndoUrlBarEditOp)
ON_COMMAND(ID_EDIT_SELECT_ALL, OnSelectAll)
ON_COMMAND(ID_EDIT_SELECT_NONE, OnSelectNone)
ON_COMMAND(ID_COPY_LINK_LOCATION, OnCopyLinkLocation)
ON_COMMAND(ID_OPEN_LINK_IN_NEW_WINDOW, OnOpenLinkInNewWindow)
ON_COMMAND(ID_VIEW_IMAGE, OnViewImageInNewWindow)
ON_COMMAND(ID_SAVE_LINK_AS, OnSaveLinkAs)
ON_COMMAND(ID_SAVE_IMAGE_AS, OnSaveImageAs)
ON_COMMAND(ID_EDIT_FIND, OnShowFindDlg)
ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
ON_UPDATE_COMMAND_UI(ID_FILE_PRINT, OnUpdateFilePrint)
ON_REGISTERED_MESSAGE(WM_FINDMSG, OnFindMsg)
ON_COMMAND(ID_TESTS_CHANGEURL, OnTestsChangeUrl)
ON_COMMAND(ID_TESTS_GLOBALHISTORY, OnTestsGlobalHistory)
ON_COMMAND(ID_TOOLS_REMOVEGHPAGE, OnToolsRemoveGHPage)
ON_COMMAND(ID_TESTS_CREATEFILE, OnTestsCreateFile)
ON_UPDATE_COMMAND_UI(ID_NAV_BACK, OnUpdateNavBack)
ON_UPDATE_COMMAND_UI(ID_NAV_FORWARD, OnUpdateNavForward)
ON_UPDATE_COMMAND_UI(ID_NAV_STOP, OnUpdateNavStop)
ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateCut)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateCopy)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdatePaste)
ON_COMMAND(ID_INTERFACES_NSIFILE, OnInterfacesNsifile)
ON_COMMAND(ID_TESTS_CREATEPROFILE, OnTestsCreateprofile)
ON_COMMAND(ID_INTERFACES_NSISHISTORY, OnInterfacesNsishistory)
ON_COMMAND(ID_VERIFYBUGS_70228, OnVerifybugs70228)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CBrowserView::CBrowserView()
{
mWebBrowser = nsnull;
mBaseWindow = nsnull;
mWebNav = nsnull;
mpBrowserImpl = nsnull;
mpBrowserFrame = nsnull;
mpBrowserFrameGlue = nsnull;
mbDocumentLoading = PR_FALSE;
m_pFindDlg = NULL;
m_pPrintProgressDlg = NULL;
m_bUrlBarClipOp = FALSE;
m_bCurrentlyPrinting = FALSE;
char *theUrl = "http://www.aol.com/";
}
CBrowserView::~CBrowserView()
{
}
// This is a good place to create the embeddable browser
// instance
//
int CBrowserView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CreateBrowser();
return 0;
}
void CBrowserView::OnDestroy()
{
DestroyBrowser();
}
// Create an instance of the Mozilla embeddable browser
//
HRESULT CBrowserView::CreateBrowser()
{
// Create web shell
nsresult rv;
mWebBrowser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID, &rv);
if(NS_FAILED(rv))
return rv;
// Save off the nsIWebNavigation interface pointer
// in the mWebNav member variable which we'll use
// later for web page navigation
//
rv = NS_OK;
mWebNav = do_QueryInterface(mWebBrowser, &rv);
if(NS_FAILED(rv))
return rv;
// mSessionHistory = do_GetInterface(mWebBrowser, &rv); // de: added 5/11
// Create the CBrowserImpl object - this is the object
// which implements the interfaces which are required
// by an app embedding mozilla i.e. these are the interfaces
// thru' which the *embedded* browser communicates with the
// *embedding* app
//
// The CBrowserImpl object will be passed in to the
// SetContainerWindow() call below
//
// Also note that we're passing the BrowserFrameGlue pointer
// and also the mWebBrowser interface pointer via CBrowserImpl::Init()
// of CBrowserImpl object.
// These pointers will be saved by the CBrowserImpl object.
// The CBrowserImpl object uses the BrowserFrameGlue pointer to
// call the methods on that interface to update the status/progress bars
// etc.
mpBrowserImpl = new CBrowserImpl();
if(mpBrowserImpl == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
// Pass along the mpBrowserFrameGlue pointer to the BrowserImpl object
// This is the interface thru' which the XP BrowserImpl code communicates
// with the platform specific code to update status bars etc.
mpBrowserImpl->Init(mpBrowserFrameGlue, mWebBrowser);
mpBrowserImpl->AddRef();
mWebBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome*, mpBrowserImpl));
rv = NS_OK;
nsCOMPtr<nsIDocShellTreeItem> dsti = do_QueryInterface(mWebBrowser, &rv);
if(NS_FAILED(rv))
return rv;
dsti->SetItemType(nsIDocShellTreeItem::typeContentWrapper);
// Create the real webbrowser window
// Note that we're passing the m_hWnd in the call below to InitWindow()
// (CBrowserView inherits the m_hWnd from CWnd)
// This m_hWnd will be used as the parent window by the embeddable browser
//
rv = NS_OK;
mBaseWindow = do_QueryInterface(mWebBrowser, &rv);
if(NS_FAILED(rv))
return rv;
// Get the view's ClientRect which to be passed in to the InitWindow()
// call below
RECT rcLocation;
GetClientRect(&rcLocation);
if(IsRectEmpty(&rcLocation))
{
rcLocation.bottom++;
rcLocation.top++;
}
rv = mBaseWindow->InitWindow(nsNativeWidget(m_hWnd), nsnull,
0, 0, rcLocation.right - rcLocation.left, rcLocation.bottom - rcLocation.top);
rv = mBaseWindow->Create();
// Register the BrowserImpl object to receive progress messages
// These callbacks will be used to update the status/progress bars
nsWeakPtr weakling(
do_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*, mpBrowserImpl)));
rv = mWebBrowser->AddWebBrowserListener(weakling, NS_GET_IID(nsIWebProgressListener));
if (NS_FAILED(rv))
{
AfxMessageBox("Web Progress Listener not added.");
WriteToOutputFile("Web Progress Listener not added.\r\n");
}
else
{
AfxMessageBox("Web Progress Listener added.");
WriteToOutputFile("Web Progress Listener added.\r\n");
}
// Finally, show the web browser window
mBaseWindow->SetVisibility(PR_TRUE);
return S_OK;
}
HRESULT CBrowserView::DestroyBrowser()
{
if(mBaseWindow)
{
mBaseWindow->Destroy();
mBaseWindow = nsnull;
}
if(mpBrowserImpl)
{
mpBrowserImpl->Release();
mpBrowserImpl = nsnull;
}
nsWeakPtr weakling(
do_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*, mpBrowserImpl)));
nsresult rv;
rv = mWebBrowser->RemoveWebBrowserListener(weakling, NS_GET_IID(nsIWebProgressListener));
if (NS_FAILED(rv))
{
AfxMessageBox("Web Progress Listener not removed.");
WriteToOutputFile("Web Progress Listener not removed.\r\n");
}
else
{
AfxMessageBox("Web Progress Listener removed.");
WriteToOutputFile("Web Progress Listener removed.\r\n");
}
return NS_OK;
}
BOOL CBrowserView::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CWnd::PreCreateWindow(cs))
return FALSE;
cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS,
::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);
return TRUE;
}
// Adjust the size of the embedded browser
// in response to any container size changes
//
void CBrowserView::OnSize( UINT nType, int cx, int cy)
{
mBaseWindow->SetPositionAndSize(0, 0, cx, cy, PR_TRUE);
}
// Called by this object's creator i.e. the CBrowserFrame object
// to pass it's pointer to us
//
void CBrowserView::SetBrowserFrame(CBrowserFrame* pBrowserFrame)
{
mpBrowserFrame = pBrowserFrame;
}
void CBrowserView::SetBrowserFrameGlue(PBROWSERFRAMEGLUE pBrowserFrameGlue)
{
mpBrowserFrameGlue = pBrowserFrameGlue;
}
// A new URL was entered in the URL bar
// Get the URL's text from the Urlbar's (ComboBox's) EditControl
// and navigate to that URL
//
void CBrowserView::OnNewUrlEnteredInUrlBar()
{
// Get the currently entered URL
CString strUrl;
mpBrowserFrame->m_wndUrlBar.GetEnteredURL(strUrl);
if(IsViewSourceUrl(strUrl))
OpenViewSourceWindow(strUrl.GetBuffer(0));
else
// Navigate to that URL
OpenURL(strUrl.GetBuffer(0));
// Add what was just entered into the UrlBar
mpBrowserFrame->m_wndUrlBar.AddURLToList(strUrl);
}
// A URL has been selected from the UrlBar's dropdown list
//
void CBrowserView::OnUrlSelectedInUrlBar()
{
CString strUrl;
mpBrowserFrame->m_wndUrlBar.GetSelectedURL(strUrl);
if(IsViewSourceUrl(strUrl))
OpenViewSourceWindow(strUrl.GetBuffer(0));
else
OpenURL(strUrl.GetBuffer(0));
}
BOOL CBrowserView::IsViewSourceUrl(CString& strUrl)
{
return (strUrl.Find("view-source:", 0) != -1) ? TRUE : FALSE;
}
BOOL CBrowserView::OpenViewSourceWindow(const char* pUrl)
{
// Create a new browser frame in which we'll show the document source
// Note that we're getting rid of the toolbars etc. by specifying
// the appropriate chromeFlags
PRUint32 chromeFlags = nsIWebBrowserChrome::CHROME_WINDOW_BORDERS |
nsIWebBrowserChrome::CHROME_TITLEBAR |
nsIWebBrowserChrome::CHROME_WINDOW_RESIZE;
CBrowserFrame* pFrm = CreateNewBrowserFrame(chromeFlags);
if(!pFrm)
return FALSE;
// Finally, load this URI into the newly created frame
pFrm->m_wndBrowserView.OpenURL(pUrl);
pFrm->BringWindowToTop();
return TRUE;
}
void CBrowserView::OnViewSource()
{
if(! mWebNav)
return;
// Get the URI object whose source we want to view.
nsresult rv = NS_OK;
nsCOMPtr<nsIURI> currentURI;
rv = mWebNav->GetCurrentURI(getter_AddRefs(currentURI));
if(NS_FAILED(rv) || !currentURI)
return;
// Get the uri string associated with the nsIURI object
nsXPIDLCString uriString;
rv = currentURI->GetSpec(getter_Copies(uriString));
if(NS_FAILED(rv))
return;
// Build the view-source: url
nsCAutoString viewSrcUrl;
viewSrcUrl.Append("view-source:");
viewSrcUrl.Append(uriString);
OpenViewSourceWindow(viewSrcUrl.get());
}
void CBrowserView::OnViewInfo()
{
AfxMessageBox("To Be Done...");
}
void CBrowserView::OnNavBack()
{
if(mWebNav)
mWebNav->GoBack();
}
void CBrowserView::OnUpdateNavBack(CCmdUI* pCmdUI)
{
PRBool canGoBack = PR_FALSE;
if (mWebNav)
mWebNav->GetCanGoBack(&canGoBack);
pCmdUI->Enable(canGoBack);
}
void CBrowserView::OnNavForward()
{
if(mWebNav)
mWebNav->GoForward();
}
void CBrowserView::OnUpdateNavForward(CCmdUI* pCmdUI)
{
PRBool canGoFwd = PR_FALSE;
if (mWebNav)
mWebNav->GetCanGoForward(&canGoFwd);
pCmdUI->Enable(canGoFwd);
}
void CBrowserView::OnNavHome()
{
// Get the currently configured HomePage URL
CString strHomeURL;
CMfcEmbedApp *pApp = (CMfcEmbedApp *)AfxGetApp();
if(pApp)
pApp->GetHomePage(strHomeURL);
if(strHomeURL.GetLength() > 0)
OpenURL(strHomeURL);
}
void CBrowserView::OnNavReload()
{
if(mWebNav)
mWebNav->Reload(nsIWebNavigation::LOAD_FLAGS_NONE);
}
void CBrowserView::OnNavStop()
{
if(mWebNav)
mWebNav->Stop();
}
void CBrowserView::OnUpdateNavStop(CCmdUI* pCmdUI)
{
pCmdUI->Enable(mbDocumentLoading);
}
void CBrowserView::OnCut()
{
if(m_bUrlBarClipOp)
{
// We need to operate on the URLBar selection
mpBrowserFrame->CutUrlBarSelToClipboard();
m_bUrlBarClipOp = FALSE;
}
else
{
nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
if(clipCmds)
clipCmds->CutSelection();
}
}
void CBrowserView::OnUpdateCut(CCmdUI* pCmdUI)
{
PRBool canCutSelection = PR_FALSE;
nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
if (clipCmds)
clipCmds->CanCutSelection(&canCutSelection);
if(!canCutSelection)
{
// Check to see if the Cut cmd is to cut the URL
// selection in the UrlBar
if(mpBrowserFrame->CanCutUrlBarSelection())
{
canCutSelection = TRUE;
m_bUrlBarClipOp = TRUE;
}
}
pCmdUI->Enable(canCutSelection);
}
void CBrowserView::OnCopy()
{
if(m_bUrlBarClipOp)
{
// We need to operate on the URLBar selection
mpBrowserFrame->CopyUrlBarSelToClipboard();
m_bUrlBarClipOp = FALSE;
}
else
{
// We need to operate on the web page content
nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
if(clipCmds)
clipCmds->CopySelection();
}
}
void CBrowserView::OnUpdateCopy(CCmdUI* pCmdUI)
{
PRBool canCopySelection = PR_FALSE;
nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
if (clipCmds)
clipCmds->CanCopySelection(&canCopySelection);
if(!canCopySelection)
{
// Check to see if the Copy cmd is to copy the URL
// selection in the UrlBar
if(mpBrowserFrame->CanCopyUrlBarSelection())
{
canCopySelection = TRUE;
m_bUrlBarClipOp = TRUE;
}
}
pCmdUI->Enable(canCopySelection);
}
void CBrowserView::OnPaste()
{
if(m_bUrlBarClipOp)
{
mpBrowserFrame->PasteFromClipboardToUrlBar();
m_bUrlBarClipOp = FALSE;
}
else
{
nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
if(clipCmds)
clipCmds->Paste();
}
}
void CBrowserView::OnUndoUrlBarEditOp()
{
if(mpBrowserFrame->CanUndoUrlBarEditOp())
mpBrowserFrame->UndoUrlBarEditOp();
}
void CBrowserView::OnUpdatePaste(CCmdUI* pCmdUI)
{
PRBool canPaste = PR_FALSE;
nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
if (clipCmds)
clipCmds->CanPaste(&canPaste);
if(!canPaste)
{
if(mpBrowserFrame->CanPasteToUrlBar())
{
canPaste = TRUE;
m_bUrlBarClipOp = TRUE;
}
}
pCmdUI->Enable(canPaste);
}
void CBrowserView::OnSelectAll()
{
nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
if(clipCmds)
clipCmds->SelectAll();
}
void CBrowserView::OnSelectNone()
{
nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
if(clipCmds)
clipCmds->SelectNone();
}
void CBrowserView::OnFileOpen()
{
char *lpszFilter =
"HTML Files Only (*.htm;*.html)|*.htm;*.html|"
"All Files (*.*)|*.*||";
CFileDialog cf(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
lpszFilter, this);
if(cf.DoModal() == IDOK)
{
CString strFullPath = cf.GetPathName(); // Will be like: c:\tmp\junk.htm
OpenURL(strFullPath);
}
}
void CBrowserView::GetBrowserWindowTitle(nsCString& title)
{
nsXPIDLString idlStrTitle;
if(mBaseWindow)
mBaseWindow->GetTitle(getter_Copies(idlStrTitle));
title.AssignWithConversion(idlStrTitle);
// Sanitize the title of all illegal characters
title.CompressWhitespace(); // Remove whitespace from the ends
title.StripChars("\\*|:\"><?"); // Strip illegal characters
title.ReplaceChar('.', L'_'); // Dots become underscores
title.ReplaceChar('/', L'-'); // Forward slashes become hyphens
}
void CBrowserView::OnFileSaveAs()
{
nsCString fileName;
GetBrowserWindowTitle(fileName); // Suggest the window title as the filename
char *lpszFilter =
"Web Page, HTML Only (*.htm;*.html)|*.htm;*.html|"
"Web Page, Complete (*.htm;*.html)|*.htm;*.html|"
"Text File (*.txt)|*.txt||";
CFileDialog cf(FALSE, "htm", (const char *)fileName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
lpszFilter, this);
if(cf.DoModal() == IDOK)
{
CString strFullPath = cf.GetPathName(); // Will be like: c:\tmp\junk.htm
char *pStrFullPath = strFullPath.GetBuffer(0); // Get char * for later use
CString strDataPath;
char *pStrDataPath = NULL;
if(cf.m_ofn.nFilterIndex == 2)
{
// cf.m_ofn.nFilterIndex == 2 indicates
// user want to save the complete document including
// all frames, images, scripts, stylesheets etc.
int idxPeriod = strFullPath.ReverseFind('.');
strDataPath = strFullPath.Mid(0, idxPeriod);
strDataPath += "_files";
// At this stage strDataPath will be of the form
// c:\tmp\junk_files - assuming we're saving to a file
// named junk.htm
// Any images etc in the doc will be saved to a dir
// with the name c:\tmp\junk_files
pStrDataPath = strDataPath.GetBuffer(0); //Get char * for later use
}
// Save the file
nsCOMPtr<nsIWebBrowserPersist> persist(do_QueryInterface(mWebBrowser));
if(persist)
persist->SaveDocument(nsnull, pStrFullPath, pStrDataPath);
}
}
void CBrowserView::OpenURL(const char* pUrl)
{
if(mWebNav)
mWebNav->LoadURI(NS_ConvertASCIItoUCS2(pUrl).get(), nsIWebNavigation::LOAD_FLAGS_NONE);
}
void CBrowserView::OpenURL(const PRUnichar* pUrl)
{
if(mWebNav)
mWebNav->LoadURI(pUrl, nsIWebNavigation::LOAD_FLAGS_NONE);
}
CBrowserFrame* CBrowserView::CreateNewBrowserFrame(PRUint32 chromeMask,
PRInt32 x, PRInt32 y,
PRInt32 cx, PRInt32 cy,
PRBool bShowWindow)
{
CMfcEmbedApp *pApp = (CMfcEmbedApp *)AfxGetApp();
if(!pApp)
return NULL;
return pApp->CreateNewBrowserFrame(chromeMask, x, y, cx, cy, bShowWindow);
}
void CBrowserView::OpenURLInNewWindow(const PRUnichar* pUrl)
{
if(!pUrl)
return;
CBrowserFrame* pFrm = CreateNewBrowserFrame();
if(!pFrm)
return;
// Load the URL into it...
// Note that OpenURL() is overloaded - one takes a "char *"
// and the other a "PRUniChar *". We're using the "PRUnichar *"
// version here
pFrm->m_wndBrowserView.OpenURL(pUrl);
}
void CBrowserView::LoadHomePage()
{
OnNavHome();
}
void CBrowserView::OnCopyLinkLocation()
{
if(! mCtxMenuLinkUrl.Length())
return;
if (! OpenClipboard())
return;
HGLOBAL hClipData = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, mCtxMenuLinkUrl.Length() + 1);
if(! hClipData)
return;
char *pszClipData = (char*)::GlobalLock(hClipData);
if(!pszClipData)
return;
mCtxMenuLinkUrl.ToCString(pszClipData, mCtxMenuLinkUrl.Length() + 1);
GlobalUnlock(hClipData);
EmptyClipboard();
SetClipboardData(CF_TEXT, hClipData);
CloseClipboard();
}
void CBrowserView::OnOpenLinkInNewWindow()
{
if(mCtxMenuLinkUrl.Length())
OpenURLInNewWindow(mCtxMenuLinkUrl.get());
}
void CBrowserView::OnViewImageInNewWindow()
{
if(mCtxMenuImgSrc.Length())
OpenURLInNewWindow(mCtxMenuImgSrc.get());
}
void CBrowserView::OnSaveLinkAs()
{
if(! mCtxMenuLinkUrl.Length())
return;
// Try to get the file name part from the URL
// To do that we first construct an obj which supports
// nsIRUI interface. Makes it easy to extract portions
// of a URL like the filename, scheme etc. + We'll also
// use it while saving this link to a file
nsresult rv = NS_OK;
nsCOMPtr<nsIURI> linkURI;
rv = NS_NewURI(getter_AddRefs(linkURI), mCtxMenuLinkUrl);
if (NS_FAILED(rv))
return;
// Get the "path" portion (see nsIURI.h for more info
// on various parts of a URI)
nsXPIDLCString path;
linkURI->GetPath(getter_Copies(path));
// The path may have the "/" char in it - strip those
nsCAutoString fileName(path);
fileName.StripChars("\\/");
// Now, use this file name in a File Save As dlg...
char *lpszFilter =
"HTML Files (*.htm;*.html)|*.htm;*.html|"
"Text Files (*.txt)|*.txt|"
"All Files (*.*)|*.*||";
const char *pFileName = fileName.Length() ? fileName.get() : NULL;
CFileDialog cf(FALSE, "htm", pFileName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
lpszFilter, this);
if(cf.DoModal() == IDOK)
{
CString strFullPath = cf.GetPathName();
nsCOMPtr<nsIWebBrowserPersist> persist(do_QueryInterface(mWebBrowser));
if(persist)
persist->SaveURI(linkURI, nsnull, strFullPath.GetBuffer(0));
}
}
void CBrowserView::OnSaveImageAs()
{
if(! mCtxMenuImgSrc.Length())
return;
// Try to get the file name part from the URL
// To do that we first construct an obj which supports
// nsIRUI interface. Makes it easy to extract portions
// of a URL like the filename, scheme etc. + We'll also
// use it while saving this link to a file
nsresult rv = NS_OK;
nsCOMPtr<nsIURI> linkURI;
rv = NS_NewURI(getter_AddRefs(linkURI), mCtxMenuImgSrc);
if (NS_FAILED(rv))
return;
// Get the "path" portion (see nsIURI.h for more info
// on various parts of a URI)
nsXPIDLCString path;
linkURI->GetPath(getter_Copies(path));
// The path may have the "/" char in it - strip those
nsCAutoString fileName(path);
fileName.StripChars("\\/");
// Now, use this file name in a File Save As dlg...
char *lpszFilter = "All Files (*.*)|*.*||";
const char *pFileName = fileName.Length() ? fileName.get() : NULL;
CFileDialog cf(FALSE, NULL, pFileName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
lpszFilter, this);
if(cf.DoModal() == IDOK)
{
CString strFullPath = cf.GetPathName();
nsCOMPtr<nsIWebBrowserPersist> persist(do_QueryInterface(mWebBrowser));
if(persist)
persist->SaveURI(linkURI, nsnull, strFullPath.GetBuffer(0));
}
}
void CBrowserView::OnShowFindDlg()
{
// When the the user chooses the Find menu item
// and if a Find dlg. is already being shown
// just set focus to the existing dlg instead of
// creating a new one
if(m_pFindDlg)
{
m_pFindDlg->SetFocus();
return;
}
CString csSearchStr;
PRBool bMatchCase = PR_FALSE;
PRBool bMatchWholeWord = PR_FALSE;
PRBool bWrapAround = PR_FALSE;
PRBool bSearchBackwards = PR_FALSE;
// See if we can get and initialize the dlg box with
// the values/settings the user specified in the previous search
nsCOMPtr<nsIWebBrowserFind> finder(do_GetInterface(mWebBrowser));
if(finder)
{
nsXPIDLString stringBuf;
finder->GetSearchString(getter_Copies(stringBuf));
csSearchStr = stringBuf.get();
finder->GetMatchCase(&bMatchCase);
finder->GetEntireWord(&bMatchWholeWord);
finder->GetWrapFind(&bWrapAround);
finder->GetFindBackwards(&bSearchBackwards);
}
m_pFindDlg = new CFindDialog(csSearchStr, bMatchCase, bMatchWholeWord,
bWrapAround, bSearchBackwards, this);
m_pFindDlg->Create(TRUE, NULL, NULL, 0, this);
}
// This will be called whenever the user pushes the Find
// button in the Find dialog box
// This method gets bound to the WM_FINDMSG windows msg via the
//
// ON_REGISTERED_MESSAGE(WM_FINDMSG, OnFindMsg)
//
// message map entry.
//
// WM_FINDMSG (which is registered towards the beginning of this file)
// is the message via which the FindDialog communicates with this view
//
LRESULT CBrowserView::OnFindMsg(WPARAM wParam, LPARAM lParam)
{
nsCOMPtr<nsIWebBrowserFind> finder(do_GetInterface(mWebBrowser));
if(!finder)
return NULL;
// Get the pointer to the current Find dialog box
CFindDialog* dlg = (CFindDialog *) CFindReplaceDialog::GetNotifier(lParam);
if(!dlg)
return NULL;
// Has the user decided to terminate the dialog box?
if(dlg->IsTerminating())
return NULL;
if(dlg->FindNext())
{
nsString searchString;
searchString.AssignWithConversion(dlg->GetFindString().GetBuffer(0));
finder->SetSearchString(searchString.get());
finder->SetMatchCase(dlg->MatchCase() ? PR_TRUE : PR_FALSE);
finder->SetEntireWord(dlg->MatchWholeWord() ? PR_TRUE : PR_FALSE);
finder->SetWrapFind(dlg->WrapAround() ? PR_TRUE : PR_FALSE);
finder->SetFindBackwards(dlg->SearchBackwards() ? PR_TRUE : PR_FALSE);
PRBool didFind;
nsresult rv = finder->FindNext(&didFind);
return (NS_SUCCEEDED(rv) && didFind);
}
return 0;
}
void CBrowserView::OnFilePrint()
{
nsCOMPtr<nsIDOMWindow> domWindow;
mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
if(domWindow) {
nsCOMPtr<nsIWebBrowserPrint> print(do_GetInterface(mWebBrowser));
if(print)
{
CPrintProgressDialog dlg(mWebBrowser, domWindow);
nsCOMPtr<nsIURI> currentURI;
nsresult rv = mWebNav->GetCurrentURI(getter_AddRefs(currentURI));
if(NS_SUCCEEDED(rv) || currentURI)
{
nsXPIDLCString path;
currentURI->GetPath(getter_Copies(path));
dlg.SetURI(path.get());
}
m_bCurrentlyPrinting = TRUE;
dlg.DoModal();
m_bCurrentlyPrinting = FALSE;
}
}
}
/////////////////////////////////////////////////////////////////////////////
void CBrowserView::OnUpdateFilePrint(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_bCurrentlyPrinting);
}
// Called from the busy state related methods in the
// BrowserFrameGlue object
//
// When aBusy is TRUE it means that browser is busy loading a URL
// When aBusy is FALSE, it's done loading
// We use this to update our STOP tool bar button
//
// We basically note this state into a member variable
// The actual toolbar state will be updated in response to the
// ON_UPDATE_COMMAND_UI method - OnUpdateNavStop() being called
//
void CBrowserView::UpdateBusyState(PRBool aBusy)
{
mbDocumentLoading = aBusy;
}
void CBrowserView::SetCtxMenuLinkUrl(nsAutoString& strLinkUrl)
{
mCtxMenuLinkUrl = strLinkUrl;
}
void CBrowserView::SetCtxMenuImageSrc(nsAutoString& strImgSrc)
{
mCtxMenuImgSrc = strImgSrc;
}
void CBrowserView::Activate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
nsCOMPtr<nsIWebBrowserFocus> focus(do_GetInterface(mWebBrowser));
if(!focus)
return;
switch(nState) {
case WA_ACTIVE:
case WA_CLICKACTIVE:
focus->Activate();
break;
case WA_INACTIVE:
focus->Deactivate();
break;
default:
break;
}
}
// de: Start QA test cases here
// *********************************************************
// *********************************************************
void CBrowserView::OnTestsChangeUrl()
{
char *theUrl = "http://www.aol.com/";
CUrlDialog myDialog;
nsresult rv;
// nsCOMPtr<nsIURI> pURI;
if (!mWebNav)
{
AfxMessageBox("Web navigation object not found. Change URL test not performed.");
WriteToOutputFile("Web navigation object not found. Change URL test not performed.\r\n");
return;
}
if (myDialog.DoModal() == IDOK)
{
AfxMessageBox("Begin Change URL test.");
WriteToOutputFile("Begin Change URL test.\r\n");
strcpy(theUrl, myDialog.m_urlfield);
mWebNav->LoadURI(NS_ConvertASCIItoUCS2(theUrl).get(),
nsIWebNavigation::LOAD_FLAGS_NONE);
WriteToOutputFile("\r\nLoadURI() method is called.");
WriteToOutputFile("theUrl = ");
WriteToOutputFile(theUrl);
/*
char *uriSpec;
// GetcurrentURI() declared in nsIP3PUI.idl
// used with webNav obj in nsP3PObserverHTML.cpp, line 239
// this will be used as an indep routine to verify the URI load
rv = mWebNav->GetCurrentURI( getter_AddRefs( pURI ) );
if(NS_FAILED(rv) || !pURI)
AfxMessageBox("Bad result for GetCurrentURI().");
rv = pURI->GetSpec(&uriSpec);;
if (NS_FAILED(rv))
AfxMessageBox("Bad result for GetSpec().");
AfxMessageBox("Start URL validation test().");
if (strcmp(uriSpec, theUrl) == 0)
{
WriteToOutputFile("Url loaded successfully. Test Passed.");
}
else
{
WriteToOutputFile("Url didn't load successfully. Test Failed.");
}
*/
AfxMessageBox("End Change URL test.");
WriteToOutputFile("End Change URL test.\r\n");
}
else
{
AfxMessageBox("Change URL test not executed.");
WriteToOutputFile("Change URL test not executed.\r\n");
}
}
// *********************************************************
void CBrowserView::OnTestsGlobalHistory()
{
// create instance of myHistory object. Call's XPCOM
// service manager to pass the contract ID.
char *theUrl = "http://www.bogussite.com/";
CUrlDialog myDialog;
PRBool theRetVal = PR_FALSE;
nsresult rv;
nsCOMPtr<nsIGlobalHistory> myHistory(do_GetService(NS_GLOBALHISTORY_CONTRACTID));
if (!myHistory)
{
WriteToOutputFile("Couldn't find history object. No GH tests performed.\r\n");
AfxMessageBox("Couldn't find history object. No GH tests performed.");
return;
}
if (myDialog.DoModal() == IDOK)
{
AfxMessageBox("Begin IsVisited() and AddPage() tests.");
WriteToOutputFile("Begin IsVisited() and AddPage() tests.\r\n");
strcpy(theUrl, myDialog.m_urlfield);
WriteToOutputFile("theUrl = ");
WriteToOutputFile(theUrl);
// see if url is already in the GH file (pre-AddPage() test)
myHistory->IsVisited(theUrl, &theRetVal);
if (theRetVal)
{
WriteToOutputFile("URL has been visited. Won't execute AddPage().\r\n");
AfxMessageBox("URL has been visited. Won't execute AddPage().");
}
else
{
WriteToOutputFile("URL hasn't been visited. Will execute AddPage().\r\n");
AfxMessageBox("URL hasn't been visited. Will execute AddPage().");
// adds a url to the global history file
rv = myHistory->AddPage(theUrl);
// prints addPage() results to output file
if (NS_FAILED(rv))
{
WriteToOutputFile("Invalid results for AddPage(). Url not added. Test failed.\r\n");
return;
}
else
{
WriteToOutputFile("Valid results for AddPage(). Url added. Test passed.\r\n");
}
// checks if url was visited (post-AddPage() test)
myHistory->IsVisited(theUrl, &theRetVal);
if (theRetVal)
{
WriteToOutputFile("URL is visited; post-AddPage() test. IsVisited() test passed.\r\n");
}
else
{
WriteToOutputFile("URL isn't visited; post-AddPage() test. IsVisited() test failed.\r\n");
}
}
AfxMessageBox("End IsVisited() and AddPage() tests.");
WriteToOutputFile("End IsVisited() and AddPage() tests.\r\n");
}
else
{
AfxMessageBox("IsVisited() and AddPage() tests not executed.");
WriteToOutputFile("IsVisited() and AddPage() tests not executed.\r\n");
}
}
// *********************************************************
void CBrowserView::OnTestsCreateFile()
{
nsresult rv;
PRBool exists;
nsCOMPtr<nsILocalFile> theTestFile(do_GetService(NS_LOCAL_FILE_CONTRACTID));
if (!theTestFile)
{
WriteToOutputFile("File object doesn't exist. No File tests performed.\r\n");
AfxMessageBox("File object doesn't exist. No File tests performed.");
return;
}
AfxMessageBox("Start Create File test.");
WriteToOutputFile("\r\nStart Create File test.\r\n");
rv = theTestFile->InitWithPath("c:\\temp\\theFile.txt");
rv = theTestFile->Exists(&exists);
WriteToOutputFile("File doesn't exist. We'll create it.\r\n");
rv = theTestFile->Create(nsIFile::NORMAL_FILE_TYPE, 0777);
if (NS_FAILED(rv))
{
AfxMessageBox("We failed to create the file (theFile.txt).");
WriteToOutputFile("Create() test Failed.\r\n");
}
else
{
AfxMessageBox("We created the file (theFile.txt).");
WriteToOutputFile("Create() test Passed.\r\n");
}
}
// *********************************************************
void CBrowserView::OnTestsCreateprofile()
{
CProfilesDlg myDialog;
nsresult rv;
if (myDialog.DoModal() == IDOK)
{
// NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
nsCOMPtr<nsIProfile> theProfServ(do_GetService(NS_PROFILE_CONTRACTID));
if (!theProfServ)
{
WriteToOutputFile("Didn't get profile service. No profile tests performed.\r\n");
AfxMessageBox("Didn't get profile service. No profile tests performed.");
return;
}
if (theProfServ)
{
AfxMessageBox("Start Profile switch test.");
WriteToOutputFile("Start Profile switch test.\r\n");
AfxMessageBox("Retrieved profile service.");
rv = theProfServ->SetCurrentProfile(myDialog.m_SelectedProfile.get());
if (NS_SUCCEEDED(rv))
AfxMessageBox("SetCurrentProfile() passed. Profile switched.");
else
AfxMessageBox("SetCurrentProfile() didn't pass.Profile not switched.");
AfxMessageBox("End Profile switch test.");
WriteToOutputFile("End Profile switch test.\r\n");
}
}
else
{
AfxMessageBox("Profile switch test not executed.");
WriteToOutputFile("Profile switch test not executed.\r\n");
}
}
// *********************************************************
// *********************************************************
// TOOLS to help us
void CBrowserView::OnToolsRemoveGHPage()
{
char *theUrl = "http://www.bogussite.com/";
CUrlDialog myDialog;
PRBool theRetVal = PR_FALSE;
nsresult rv;
nsCOMPtr<nsIGlobalHistory> myGHistory(do_GetService(NS_GLOBALHISTORY_CONTRACTID));
if (!myGHistory)
{
WriteToOutputFile("Could not get the global history object.\r\n");
AfxMessageBox("Could not get the global history object.");
return;
}
nsCOMPtr<nsIBrowserHistory> myHistory = do_QueryInterface(myGHistory, &rv);
if(NS_FAILED(rv)) {
WriteToOutputFile("Could not get the history object.\r\n");
AfxMessageBox("Could not get the global history object.");
return;
}
// nsCOMPtr<nsIBrowserHistory> myHistory(do_GetInterface(myGHistory));
if (myDialog.DoModal() == IDOK)
{
WriteToOutputFile("Begin URL removal from the GH file.\r\n");
AfxMessageBox("Begin URL removal from the GH file.");
strcpy(theUrl, myDialog.m_urlfield);
myGHistory->IsVisited(theUrl, &theRetVal);
if (theRetVal)
{
rv = myHistory->RemovePage(theUrl);
if (NS_SUCCEEDED(rv))
{
WriteToOutputFile("\r\nThe URL was removed from the GH file.\r\n");
AfxMessageBox("The URL was removed from the GH file.");
}
else
{
WriteToOutputFile("\r\nThe URL wasn't removed from the GH file.\r\n");
AfxMessageBox("The URL wasn't removed from the GH file.");
}
}
else
{
WriteToOutputFile("The URL wasn't in the GH file.\r\n");
}
WriteToOutputFile("End URL removal from the GH file.\r\n");
AfxMessageBox("End URL removal from the GH file.");
}
else
{
WriteToOutputFile("URL removal from the GH file not executed.\r\n");
AfxMessageBox("URL removal from the GH file not executed.");
}
}
// ***********************************************************************
// ************************** Interface Tests ****************************
// ***********************************************************************
// nsIFile:
void CBrowserView::OnInterfacesNsifile()
{
nsCOMPtr<nsILocalFile> theTestFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
nsCOMPtr<nsILocalFile> theFileOpDir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
nsresult rv;
PRBool exists;
if (!theTestFile)
{
WriteToOutputFile("File object doesn't exist. No File tests performed.\r\n");
AfxMessageBox("File object doesn't exist. No File tests performed.");
return;
}
if (!theFileOpDir)
{
WriteToOutputFile("Dir object doesn't exist. No File tests performed.\r\n");
AfxMessageBox("File object doesn't exist. No File tests performed.");
return;
}
AfxMessageBox("Begin nsIFile tests.");
WriteToOutputFile("\r\nBegin testing nsIFile InitWithPath(), AppendRelativePath(), \
Create(), and Exists() methods.\r\n");
rv = theTestFile->InitWithPath("c:\\temp\\");
if (NS_FAILED(rv))
{
AfxMessageBox("We failed to initialize file path.");
WriteToOutputFile("InitWithPath() test Failed.");
}
else
{
AfxMessageBox("We initialized file path.");
WriteToOutputFile("InitWithPath() test Passed.");
}
rv = theTestFile->AppendRelativePath("myFile.txt");
if (NS_FAILED(rv))
{
AfxMessageBox("We failed to append the file.");
WriteToOutputFile("AppendRelativePath() test Failed.");
}
else
{
AfxMessageBox("We appended the file.");
WriteToOutputFile("AppendRelativePath() test Passed.");
}
rv = theTestFile->Exists(&exists);
if (!exists)
{
AfxMessageBox("File doesn't exist. We'll try creating it.");
WriteToOutputFile("File doesn't exist. We'll try creating it.\r\n");
rv = theTestFile->Create(nsIFile::NORMAL_FILE_TYPE, 0777);
if (NS_FAILED(rv))
{
AfxMessageBox("We failed to create the file (myFile.txt).");
WriteToOutputFile("Create() test Failed.");
}
else
{
AfxMessageBox("We created the file (myFile.txt).");
WriteToOutputFile("Create() test Passed.");
}
}
else
{
AfxMessageBox("File already exists (myFile.txt). We won't create it.");
WriteToOutputFile("File already exists (myFile.txt). We won't create it.");
}
rv = theTestFile->Exists(&exists);
if (!exists)
{
AfxMessageBox("File (myFile.txt) doesn't exist.");
WriteToOutputFile("Exists() test Failed.");
}
else
{
AfxMessageBox("File (myFile.txt) exists.");
WriteToOutputFile("Exists() test Passed.");
}
// FILE COPY test
AfxMessageBox("Start File Copy test.");
WriteToOutputFile("Start File Copy test.");
rv = theFileOpDir->InitWithPath("c:\\temp\\");
if (NS_FAILED(rv))
{
AfxMessageBox("The target dir wasn't found.");
WriteToOutputFile("The target dir wasn't found.");
}
else
{
AfxMessageBox("The target dir was found.");
WriteToOutputFile("The target dir was found.");
}
rv = theTestFile->InitWithPath("c:\\temp\\myFile.txt");
if (NS_FAILED(rv))
AfxMessageBox("The path wasn't found.");
else
AfxMessageBox("The path was found.");
rv = theTestFile->CopyTo(theFileOpDir, "myFile2.txt");
if (NS_FAILED(rv))
AfxMessageBox("rv CopyTo() test failed.");
else
AfxMessageBox("rv CopyTo() test succeeded.");
rv = theTestFile->InitWithPath("c:\\temp\\myFile2.txt");
rv = theTestFile->Exists(&exists);
if (!exists)
{
AfxMessageBox("File didn't copy.");
WriteToOutputFile("CopyTo() test Failed.");
}
else
{
AfxMessageBox("File copied.");
WriteToOutputFile("CopyTo() test Passed.");
}
// FILE MOVE test
AfxMessageBox("Start File Move test.");
WriteToOutputFile("Start File Move test.");
rv = theFileOpDir->InitWithPath("c:\\Program Files\\");
if (NS_FAILED(rv))
{
AfxMessageBox("The target dir wasn't found.");
WriteToOutputFile("The target dir wasn't found.");
}
rv = theTestFile->InitWithPath("c:\\temp\\myFile2.txt");
if (NS_FAILED(rv))
AfxMessageBox("The path wasn't found.");
rv = theTestFile->MoveTo(theFileOpDir, "myFile2.txt");
if (NS_FAILED(rv))
AfxMessageBox("rv MoveTo() test failed.");
else
AfxMessageBox("rv MoveTo() test succeeded.");
rv = theTestFile->InitWithPath("c:\\Program Files\\myFile2.txt");
rv = theTestFile->Exists(&exists);
if (!exists)
{
AfxMessageBox("File wasn't moved.");
WriteToOutputFile("MoveTo() test Failed.");
}
else
{
AfxMessageBox("File was moved.");
WriteToOutputFile("MoveTo() test Passed.");
}
AfxMessageBox("End nsIFile tests.");
WriteToOutputFile("\r\nEnd testing nsIFile InitWithPath(), AppendRelativePath(), \
Create(), and Exists() methods.\r\n");
}
// nsISHistory:
void CBrowserView::OnInterfacesNsishistory()
{
nsresult rv;
boolean modifyIndex;
long numEntries = 0;
nsCOMPtr<nsISHistory> theSessionHistory(do_CreateInstance(NS_SHISTORY_CONTRACTID));
nsCOMPtr<nsIHistoryEntry> theHistoryEntry(do_CreateInstance(NS_HISTORYENTRY_CONTRACTID));
nsCOMPtr<nsISHistoryListener> theSHListener(do_CreateInstance(NS_SHISTORYLISTENER_CONTRACTID));
// do_QueryInterface
// NS_HISTORYENTRY_CONTRACTID
// NS_SHISTORYLISTENER_CONTRACTID
if (!theSessionHistory)
{
AfxMessageBox("theSessionHistory object wasn't created. No tests performed.");
return;
}
if (!theHistoryEntry)
{
AfxMessageBox("NSISHistory Listener not added.");
return;
}
// addSHistoryListener test
nsWeakPtr weakling(
do_GetWeakReference(NS_STATIC_CAST(nsISHistoryListener*, mpBrowserImpl)));
rv = mWebBrowser->AddWebBrowserListener(weakling, NS_GET_IID(nsISHistoryListener));
if (NS_FAILED(rv))
{
AfxMessageBox("NSISHistory Listener not added.");
WriteToOutputFile("NSISHistory Listener not added.\r\n");
}
else
{
AfxMessageBox("NSISHistory Listener added.");
WriteToOutputFile("NSISHistory Listener added.\r\n");
}
/*
// getEntryAtIndex() test
theHistoryEntry = theSessionHistory->GetEntryAtIndex(index, modifyIndex);
if (!theHistoryEntry)
{
AfxMessageBox("We got the History Entry.");
}
else
{
AfxMessageBox("We didn't get the History Entry.");
}
*/
///*
// PurgeHistory() test
rv = theSessionHistory->PurgeHistory(numEntries);
if (NS_FAILED(rv))
AfxMessageBox("Purge History test failed.");
else
AfxMessageBox("Purge History test succeeded.");
//*/
// RemoveSHistoryListener test
/*
nsWeakPtr weakling(
do_GetWeakReference(NS_STATIC_CAST(nsISHistoryListener*, mpBrowserImpl)));
rv = theSessionHistory->RemoveSHistoryListener(weakling);
*/
}
// ***************** Local Methods ******************
void CBrowserView::WriteToOutputFile(char *pLine)
{
CStdioFile myFile;
CFileException e;
CString strFileName = "c:\\temp\\MFCoutfile";
if(! myFile.Open( strFileName, CStdioFile::modeCreate | CStdioFile::modeWrite
| CStdioFile::modeNoTruncate, &e ) )
{
CString failCause = "Unable to open file. Reason : ";
failCause += e.m_cause;
AfxMessageBox(failCause);
}
else
{
myFile.SeekToEnd();
CString strLine = pLine;
strLine += "\r\n";
myFile.WriteString(strLine);
myFile.Close();
}
}
// ***************** Bug Verifications ******************
void CBrowserView::OnVerifybugs70228()
{
/*
nsCOMPtr<nsIHelperAppLauncherDialog>
myHALD(do_GetService(NS_IHELPERAPPLAUNCHERDLG_CONTRACTID));
if (!myHALD)
AfxMessageBox("Object not created. It's NOT a service!");
else
AfxMessageBox("Object is created. But should it?! It's NOT a service!");
nsCOMPtr<nsIHelperAppLauncherDialog>
myHALD(do_CreateInstance(NS_IHELPERAPPLAUNCHERDLG_CONTRACTID));
if (!myHALD)
AfxMessageBox("Object not created. It should be. It's a component!");
else
AfxMessageBox("Object is created. It's a component!");
*/
/*
nsCOMPtr<nsIHelperAppLauncher>
myHAL(do_CreateInstance(NS_IHELPERAPPLAUNCHERDLG_CONTRACTID));
rv = myHALD->show(myHal, mySupp);
*/
}
|