2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 Change History (most recent first):
25 $Log: ExplorerBarWindow.cpp,v $
26 Revision 1.21 2005/04/06 01:13:07 shersche
27 <rdar://problem/4066195> Use the product icon instead of globe icon for 'About' link.
29 Revision 1.20 2005/03/18 02:43:02 shersche
30 <rdar://problem/4046443> Use standard IE website icon for 'About Bonjour', only using globe icon if standard icon cannot be loaded
32 Revision 1.19 2005/03/16 03:46:27 shersche
33 <rdar://problem/4045657> Use Bonjour icon for all discovered sites
35 Revision 1.18 2005/02/26 01:24:05 shersche
36 Remove display lines in tree control
38 Revision 1.17 2005/02/25 19:57:30 shersche
39 <rdar://problem/4023323> Remove FTP browsing from plugin
41 Revision 1.16 2005/02/08 23:31:06 shersche
42 Move "About ..." item underneath WebSites, change icons for discovered sites and "About ..." item
44 Revision 1.15 2005/01/27 22:38:27 shersche
45 add About item to tree list
47 Revision 1.14 2005/01/25 17:55:39 shersche
48 <rdar://problem/3911084> Get bitmaps from non-localizable resource module
51 Revision 1.13 2005/01/06 21:13:09 shersche
52 <rdar://problem/3796779> Handle kDNSServiceErr_Firewall return codes
55 Revision 1.12 2004/10/26 00:56:03 cheshire
56 Use "#if 0" instead of commenting out code
58 Revision 1.11 2004/10/18 23:49:17 shersche
59 <rdar://problem/3841564> Remove trailing dot from hostname, because some flavors of Windows have difficulty parsing hostnames with a trailing dot.
62 Revision 1.10 2004/09/02 02:18:58 cheshire
63 Minor textual cleanup to improve readability
65 Revision 1.9 2004/09/02 02:11:56 cheshire
66 <rdar://problem/3783611> Fix incorrect testing of MoreComing flag
68 Revision 1.8 2004/07/26 05:47:31 shersche
69 use TXTRecord APIs, fix bug in locating service to be removed
71 Revision 1.7 2004/07/22 16:08:20 shersche
72 clean up debug print statements, re-enable code inadvertently commented out
74 Revision 1.6 2004/07/22 05:27:20 shersche
75 <rdar://problem/3735827> Check to make sure error isn't WSAEWOULDBLOCK when canceling browse
78 Revision 1.5 2004/07/20 06:49:18 shersche
79 clean up socket handling code
81 Revision 1.4 2004/07/13 21:24:21 rpantos
82 Fix for <rdar://problem/3701120>.
84 Revision 1.3 2004/06/27 14:59:59 shersche
85 reference count service info to handle multi-homed hosts
87 Revision 1.2 2004/06/23 16:09:34 shersche
88 Add the resolve DNSServiceRef to list of extant refs. This fixes the "doesn't resolve when double clicking" problem
90 Submitted by: Scott Herscher
92 Revision 1.1 2004/06/18 04:34:59 rpantos
93 Move to Clients from mDNSWindows
95 Revision 1.5 2004/04/15 01:00:05 bradley
96 Removed support for automatically querying for A/AAAA records when resolving names. Platforms
97 without .local name resolving support will need to manually query for A/AAAA records as needed.
99 Revision 1.4 2004/04/09 21:03:15 bradley
100 Changed port numbers to use network byte order for consistency with other platforms.
102 Revision 1.3 2004/04/08 09:43:43 bradley
103 Changed callback calling conventions to __stdcall so they can be used with C# delegates.
105 Revision 1.2 2004/02/21 04:36:19 bradley
106 Enable dot local name lookups now that the NSP is being installed.
108 Revision 1.1 2004/01/30 03:01:56 bradley
109 Explorer Plugin to browse for DNS-SD advertised Web and FTP servers from within Internet Explorer.
115 #include "CommonServices.h"
116 #include "DebugServices.h"
117 #include "WinServices.h"
120 #include "ExplorerBar.h"
121 #include "LoginDialog.h"
122 #include "Resource.h"
124 #include "ExplorerBarWindow.h"
125 #include "ExplorerPlugin.h"
130 #define new DEBUG_NEW
132 static char THIS_FILE
[] = __FILE__
;
136 #pragma mark == Constants ==
139 //===========================================================================================================================
141 //===========================================================================================================================
145 #define IDC_EXPLORER_TREE 1234
149 #define WM_PRIVATE_SERVICE_EVENT ( WM_USER + 0x100 )
153 #define kTXTRecordKeyPath "path"
157 #define kIEIconResource 32529
161 #pragma mark == Prototypes ==
164 //===========================================================================================================================
166 //===========================================================================================================================
168 DEBUG_LOCAL
int FindServiceArrayIndex( const ServiceInfoArray
&inArray
, const ServiceInfo
&inService
, int &outIndex
);
171 #pragma mark == Message Map ==
174 //===========================================================================================================================
176 //===========================================================================================================================
178 BEGIN_MESSAGE_MAP( ExplorerBarWindow
, CWnd
)
182 ON_NOTIFY( NM_DBLCLK
, IDC_EXPLORER_TREE
, OnDoubleClick
)
183 ON_MESSAGE( WM_PRIVATE_SERVICE_EVENT
, OnServiceEvent
)
190 //===========================================================================================================================
192 //===========================================================================================================================
194 ExplorerBarWindow::ExplorerBarWindow( void )
197 mResolveServiceRef
= NULL
;
200 //===========================================================================================================================
201 // ~ExplorerBarWindow
202 //===========================================================================================================================
204 ExplorerBarWindow::~ExplorerBarWindow( void )
213 //===========================================================================================================================
215 //===========================================================================================================================
217 int ExplorerBarWindow::OnCreate( LPCREATESTRUCT inCreateStruct
)
219 AFX_MANAGE_STATE( AfxGetStaticModuleState() );
221 HINSTANCE
module = NULL
;
227 err
= CWnd::OnCreate( inCreateStruct
);
228 require_noerr( err
, exit
);
230 GetClientRect( rect
);
231 mTree
.Create( WS_TABSTOP
| WS_VISIBLE
| WS_CHILD
| TVS_HASBUTTONS
| TVS_LINESATROOT
| TVS_NOHSCROLL
, rect
, this,
234 ServiceHandlerEntry
* e
;
238 e
= new ServiceHandlerEntry
;
240 e
->type
= "_http._tcp";
241 e
->urlScheme
= "http://";
244 e
->needsLogin
= false;
245 mServiceHandlers
.Add( e
);
247 s
.LoadString( IDS_ABOUT
);
248 m_about
= mTree
.InsertItem( s
, 0, 0 );
250 err
= DNSServiceBrowse( &e
->ref
, 0, 0, e
->type
, NULL
, BrowseCallBack
, e
);
251 require_noerr( err
, exit
);
253 err
= WSAAsyncSelect((SOCKET
) DNSServiceRefSockFD(e
->ref
), m_hWnd
, WM_PRIVATE_SERVICE_EVENT
, FD_READ
|FD_CLOSE
);
254 require_noerr( err
, exit
);
256 m_serviceRefs
.push_back(e
->ref
);
258 m_imageList
.Create( 16, 16, ILC_MASK
| ILC_COLOR16
, 2, 0);
260 bitmap
.Attach( ::LoadBitmap( GetNonLocalizedResources(), MAKEINTRESOURCE( IDB_LOGO
) ) );
261 m_imageList
.Add( &bitmap
, (CBitmap
*) NULL
);
264 mTree
.SetImageList(&m_imageList
, TVSIL_NORMAL
);
270 FreeLibrary( module );
274 // Cannot talk to the mDNSResponder service. Show the error message and exit (with kNoErr so they can see it).
277 if ( err
== kDNSServiceErr_Firewall
)
279 s
.LoadString( IDS_FIREWALL
);
283 s
.LoadString( IDS_MDNSRESPONDER_NOT_AVAILABLE
);
286 mTree
.DeleteAllItems();
287 mTree
.InsertItem( s
, 0, 0, TVI_ROOT
, TVI_LAST
);
295 //===========================================================================================================================
297 //===========================================================================================================================
299 void ExplorerBarWindow::OnDestroy( void )
301 // Stop any resolves that may still be pending (shouldn't be any).
305 // Clean up the extant browses
306 while (m_serviceRefs
.size() > 0)
309 // take the head of the list
311 DNSServiceRef ref
= m_serviceRefs
.front();
314 // Stop will remove it from the list
319 // Clean up the service handlers.
324 n
= (int) mServiceHandlers
.GetSize();
325 for( i
= 0; i
< n
; ++i
)
327 delete mServiceHandlers
[ i
];
333 //===========================================================================================================================
335 //===========================================================================================================================
337 void ExplorerBarWindow::OnSize( UINT inType
, int inX
, int inY
)
339 CWnd::OnSize( inType
, inX
, inY
);
340 mTree
.MoveWindow( 0, 0, inX
, inY
);
343 //===========================================================================================================================
345 //===========================================================================================================================
347 void ExplorerBarWindow::OnDoubleClick( NMHDR
*inNMHDR
, LRESULT
*outResult
)
350 ServiceInfo
* service
;
353 DEBUG_UNUSED( inNMHDR
);
355 item
= mTree
.GetSelectedItem();
356 require( item
, exit
);
358 // Tell Internet Explorer to go to the URL if it's about item
360 if ( item
== m_about
)
366 url
.LoadString( IDS_ABOUT_URL
);
367 mOwner
->GoToURL( url
);
371 service
= reinterpret_cast < ServiceInfo
* > ( mTree
.GetItemData( item
) );
372 require_quiet( service
, exit
);
374 err
= StartResolve( service
);
375 require_noerr( err
, exit
);
383 //===========================================================================================================================
385 //===========================================================================================================================
388 ExplorerBarWindow::OnServiceEvent(WPARAM inWParam
, LPARAM inLParam
)
390 if (WSAGETSELECTERROR(inLParam
) && !(HIWORD(inLParam
)))
392 dlog( kDebugLevelError
, "OnServiceEvent: window error\n" );
396 SOCKET sock
= (SOCKET
) inWParam
;
399 ServiceRefList::iterator it
;
401 for (it
= m_serviceRefs
.begin(); it
!= m_serviceRefs
.end(); it
++)
403 DNSServiceRef ref
= *it
;
407 if ((SOCKET
) DNSServiceRefSockFD(ref
) == sock
)
409 DNSServiceErrorType err
;
411 err
= DNSServiceProcessResult(ref
);
417 s
.LoadString( IDS_MDNSRESPONDER_NOT_AVAILABLE
);
418 mTree
.DeleteAllItems();
419 mTree
.InsertItem( s
, 0, 0, TVI_ROOT
, TVI_LAST
);
436 //===========================================================================================================================
438 //===========================================================================================================================
441 ExplorerBarWindow::BrowseCallBack(
443 DNSServiceFlags inFlags
,
444 uint32_t inInterfaceIndex
,
445 DNSServiceErrorType inErrorCode
,
448 const char * inDomain
,
451 ServiceHandlerEntry
* obj
;
452 ServiceInfo
* service
;
455 DEBUG_UNUSED( inRef
);
460 require_noerr( inErrorCode
, exit
);
461 obj
= reinterpret_cast < ServiceHandlerEntry
* > ( inContext
);
466 // set the UI to hold off on updates
468 obj
->obj
->mTree
.SetRedraw(FALSE
);
472 service
= new ServiceInfo
;
473 require_action( service
, exit
, err
= kNoMemoryErr
);
475 err
= UTF8StringToStringObject( inName
, service
->displayName
);
478 service
->name
= strdup( inName
);
479 require_action( service
->name
, exit
, err
= kNoMemoryErr
);
481 service
->type
= strdup( inType
);
482 require_action( service
->type
, exit
, err
= kNoMemoryErr
);
484 service
->domain
= strdup( inDomain
);
485 require_action( service
->domain
, exit
, err
= kNoMemoryErr
);
487 service
->ifi
= inInterfaceIndex
;
488 service
->handler
= obj
;
492 if (inFlags
& kDNSServiceFlagsAdd
) obj
->obj
->OnServiceAdd (service
);
493 else obj
->obj
->OnServiceRemove(service
);
499 dlog( kDebugLevelError
, "BrowseCallBack: exception thrown\n" );
504 // If no more coming, then update UI
506 if (obj
&& obj
->obj
&& ((inFlags
& kDNSServiceFlagsMoreComing
) == 0))
508 obj
->obj
->mTree
.SetRedraw(TRUE
);
509 obj
->obj
->mTree
.Invalidate();
518 //===========================================================================================================================
520 //===========================================================================================================================
522 LONG
ExplorerBarWindow::OnServiceAdd( ServiceInfo
* service
)
524 ServiceHandlerEntry
* handler
;
530 handler
= service
->handler
;
533 cmp
= FindServiceArrayIndex( handler
->array
, *service
, index
);
536 // Found a match so update the item. The index is index + 1 so subtract 1.
539 check( index
< handler
->array
.GetSize() );
541 handler
->array
[ index
]->refs
++;
549 // Insert the new item in sorted order.
551 afterItem
= ( index
> 0 ) ? handler
->array
[ index
- 1 ]->item
: m_about
;
552 handler
->array
.InsertAt( index
, service
);
553 service
->item
= mTree
.InsertItem( service
->displayName
, 0, 0, NULL
, afterItem
);
554 mTree
.SetItemData( service
->item
, (DWORD_PTR
) service
);
559 //===========================================================================================================================
561 //===========================================================================================================================
563 LONG
ExplorerBarWindow::OnServiceRemove( ServiceInfo
* service
)
565 ServiceHandlerEntry
* handler
;
571 handler
= service
->handler
;
574 // Search to see if we know about this service instance. If so, remove it from the list.
576 cmp
= FindServiceArrayIndex( handler
->array
, *service
, index
);
581 // Possibly found a match remove the item. The index
582 // is index + 1 so subtract 1.
584 check( index
< handler
->array
.GetSize() );
586 if ( --handler
->array
[ index
]->refs
== 0 )
588 mTree
.DeleteItem( handler
->array
[ index
]->item
);
589 delete handler
->array
[ index
];
590 handler
->array
.RemoveAt( index
);
602 //===========================================================================================================================
604 //===========================================================================================================================
606 OSStatus
ExplorerBarWindow::StartResolve( ServiceInfo
*inService
)
612 // Stop any current resolve that may be in progress.
616 // Resolve the service.
617 err
= DNSServiceResolve( &mResolveServiceRef
, 0, 0,
618 inService
->name
, inService
->type
, inService
->domain
, (DNSServiceResolveReply
) ResolveCallBack
, inService
->handler
);
619 require_noerr( err
, exit
);
621 err
= WSAAsyncSelect((SOCKET
) DNSServiceRefSockFD(mResolveServiceRef
), m_hWnd
, WM_PRIVATE_SERVICE_EVENT
, FD_READ
|FD_CLOSE
);
622 require_noerr( err
, exit
);
624 m_serviceRefs
.push_back(mResolveServiceRef
);
630 //===========================================================================================================================
632 //===========================================================================================================================
634 void ExplorerBarWindow::StopResolve( void )
636 if( mResolveServiceRef
)
638 Stop( mResolveServiceRef
);
639 mResolveServiceRef
= NULL
;
643 //===========================================================================================================================
645 //===========================================================================================================================
648 ExplorerBarWindow::ResolveCallBack(
650 DNSServiceFlags inFlags
,
651 uint32_t inInterfaceIndex
,
652 DNSServiceErrorType inErrorCode
,
653 const char * inFullName
,
654 const char * inHostName
,
660 ExplorerBarWindow
* obj
;
661 ServiceHandlerEntry
* handler
;
664 DEBUG_UNUSED( inRef
);
665 DEBUG_UNUSED( inFlags
);
666 DEBUG_UNUSED( inErrorCode
);
667 DEBUG_UNUSED( inFullName
);
669 require_noerr( inErrorCode
, exit
);
670 handler
= (ServiceHandlerEntry
*) inContext
;
677 ResolveInfo
* resolve
;
680 dlog( kDebugLevelNotice
, "resolved %s on ifi %d to %s\n", inFullName
, inInterfaceIndex
, inHostName
);
682 // Stop resolving after the first good result.
686 // Post a message to the main thread so it can handle it since MFC is not thread safe.
688 resolve
= new ResolveInfo
;
689 require_action( resolve
, exit
, err
= kNoMemoryErr
);
691 UTF8StringToStringObject( inHostName
, resolve
->host
);
693 // rdar://problem/3841564
695 // strip trailing dot from hostname because some flavors of Windows
696 // have trouble parsing it.
698 idx
= resolve
->host
.ReverseFind('.');
700 if ((idx
> 1) && ((resolve
->host
.GetLength() - 1) == idx
))
702 resolve
->host
.Delete(idx
, 1);
705 resolve
->port
= ntohs( inPort
);
706 resolve
->ifi
= inInterfaceIndex
;
707 resolve
->handler
= handler
;
709 err
= resolve
->txt
.SetData( inTXT
, inTXTSize
);
712 obj
->OnResolve(resolve
);
716 dlog( kDebugLevelError
, "ResolveCallBack: exception thrown\n" );
723 //===========================================================================================================================
725 //===========================================================================================================================
727 LONG
ExplorerBarWindow::OnResolve( ResolveInfo
* resolve
)
739 // Get login info if needed.
741 if( resolve
->handler
->needsLogin
)
745 if( !dialog
.GetLogin( username
, password
) )
751 // If the HTTP TXT record is a "path=" entry, use it as the resource path. Otherwise, use "/".
754 if( strcmp( resolve
->handler
->type
, "_http._tcp" ) == 0 )
759 resolve
->txt
.GetData( &txtData
, &txtLen
);
761 path
= (uint8_t*) TXTRecordGetValuePtr(txtLen
, txtData
, kTXTRecordKeyPath
, &pathSize
);
765 path
= (uint8_t*) "";
771 path
= (uint8_t *) "";
775 // Build the URL in the following format:
777 // <urlScheme>[<username>[:<password>]@]<name/ip>[<path>]
779 url
.AppendFormat( TEXT( "%S" ), resolve
->handler
->urlScheme
); // URL Scheme
780 if( username
.GetLength() > 0 )
782 url
.AppendFormat( TEXT( "%s" ), username
); // Username
783 if( password
.GetLength() > 0 )
785 url
.AppendFormat( TEXT( ":%s" ), password
); // Password
787 url
.AppendFormat( TEXT( "@" ) );
790 url
+= resolve
->host
; // Host
791 url
.AppendFormat( TEXT( ":%d" ), resolve
->port
); // :Port
792 url
.AppendFormat( TEXT( "%S" ), pathPrefix
); // Path Prefix ("/" or empty).
793 url
.AppendFormat( TEXT( "%.*S" ), (int) pathSize
, (char *) path
); // Path (possibly empty).
795 // Tell Internet Explorer to go to the URL.
798 mOwner
->GoToURL( url
);
805 //===========================================================================================================================
807 //===========================================================================================================================
808 void ExplorerBarWindow::Stop( DNSServiceRef ref
)
810 m_serviceRefs
.remove( ref
);
812 WSAAsyncSelect(DNSServiceRefSockFD( ref
), m_hWnd
, WM_PRIVATE_SERVICE_EVENT
, 0);
814 DNSServiceRefDeallocate( ref
);
822 //===========================================================================================================================
823 // FindServiceArrayIndex
824 //===========================================================================================================================
826 DEBUG_LOCAL
int FindServiceArrayIndex( const ServiceInfoArray
&inArray
, const ServiceInfo
&inService
, int &outIndex
)
836 hi
= (int)( inArray
.GetSize() - 1 );
839 mid
= ( lo
+ hi
) / 2;
840 result
= inService
.displayName
.CompareNoCase( inArray
[ mid
]->displayName
);
844 result
= ( (int) inService
.ifi
) - ( (int) inArray
[ mid
]->ifi
);
851 else if( result
< 0 )
862 mid
+= 1; // Bump index so new item is inserted after matching item.
864 else if( result
> 0 )