Bug 563321: Work around focus regression when creating new tabs. sr=pink
authorStuart Morgan <stuart.morgan@alumni.case.edu>
Wed, 23 Jun 2010 20:37:46 -0700
changeset 3123 ce88f8c24e8312cdf2d12d35489d3b7c6aabb146
parent 3122 f1315c2a2799eeba4ab364856d40b0e12d0bd49a
child 3124 312e30c5613fbdf6cab03ca0bbd3e94ba4766e55
push id56
push userstuart.morgan@alumni.case.edu
push dateThu, 24 Jun 2010 03:37:52 +0000
reviewerspink
bugs563321
Bug 563321: Work around focus regression when creating new tabs. sr=pink
src/embedding/CHBrowserView.h
src/embedding/CHBrowserView.mm
--- a/src/embedding/CHBrowserView.h
+++ b/src/embedding/CHBrowserView.h
@@ -204,16 +204,17 @@ extern const char* const kHTMLMIMEType;
 @interface CHBrowserView : NSView 
 {
   nsIWebBrowser*        _webBrowser;
   CHBrowserListener*    _listener;
   NSWindow*             mWindow;
 
   nsIPrintSettings*     mPrintSettings; // we own this
   BOOL                  mUseGlobalPrintSettings;
+  BOOL                  mHasPendingActivation;
 }
 
 // class method to get at the browser view for a given nsIDOMWindow
 + (CHBrowserView*)browserViewFromDOMWindow:(nsIDOMWindow*)inWindow;
 
 // NSView overrides
 - (id)initWithFrame:(NSRect)frame;
 - (id)initWithFrame:(NSRect)frame andWindow:(NSWindow*)aWindow;
--- a/src/embedding/CHBrowserView.mm
+++ b/src/embedding/CHBrowserView.mm
@@ -450,16 +450,19 @@ const char* const kHTMLMIMEType = "text/
   if (flags & NSLoadFlagsBypassClassifier) {
     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CLASSIFIER;
   }
 
   nsresult rv = nav->LoadURI(specStr.get(), navFlags, referrerURI, nsnull, nsnull);
   if (NS_FAILED(rv)) {
     // XXX need to throw
   }
+
+  if (mHasPendingActivation && ([[self window] firstResponder] == self))
+    [self setActive:YES];
 }
 
 - (void)reload:(unsigned int)flags
 {
   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
   if ( !nav )
     return;
 
@@ -1503,24 +1506,37 @@ const char* const kHTMLMIMEType = "text/
   [[self browserContainer] willShowPrompt];
 }
 
 - (void)doAfterPromptDismissal
 {
   [[self browserContainer] didDismissPrompt];
 }
 
-- (void)setActive: (BOOL)aIsActive
+- (void)setActive:(BOOL)aIsActive
 {
   nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
   if (wbf) {
-    if (aIsActive)
+    if (aIsActive) {
+      // If presShell is NULL, core will get into a broken focus state, so
+      // defer the call until we have loaded something.
+      nsCOMPtr<nsIDocShell> docShell = [self docShell];
+      nsCOMPtr<nsIPresShell> presShell;
+      if (docShell)
+        docShell->GetPresShell(getter_AddRefs(presShell));
+      if (!presShell) {
+        mHasPendingActivation = YES;
+        return;
+      }
+
       wbf->Activate();
-    else
+      mHasPendingActivation = NO;
+    } else {
       wbf->Deactivate();
+    }
   }
 }
 
 -(NSMenu*)contextMenu
 {
 	return [[self browserContainer] contextMenu];
 }