Upstream version 11.39.250.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / browser_window_cocoa.mm
index 1d2ef5d..e62920f 100644 (file)
@@ -8,6 +8,7 @@
 #include "base/command_line.h"
 #include "base/logging.h"
 #include "base/mac/mac_util.h"
+#import "base/mac/sdk_forward_declarations.h"
 #include "base/message_loop/message_loop.h"
 #include "base/prefs/pref_service.h"
 #include "base/strings/sys_string_conversions.h"
 #include "chrome/browser/download/download_shelf.h"
 #include "chrome/browser/extensions/tab_helper.h"
 #include "chrome/browser/fullscreen.h"
-#include "chrome/browser/password_manager/password_manager.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/shell_integration.h"
+#include "chrome/browser/signin/signin_header_helper.h"
+#include "chrome/browser/translate/chrome_translate_client.h"
 #include "chrome/browser/ui/browser.h"
 #include "chrome/browser/ui/browser_command_controller.h"
-#include "chrome/browser/ui/browser_commands.h"
+#include "chrome/browser/ui/browser_commands_mac.h"
 #include "chrome/browser/ui/browser_list.h"
 #include "chrome/browser/ui/browser_window_state.h"
-#import "chrome/browser/ui/cocoa/browser/avatar_button_controller.h"
-#import "chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller.h"
 #import "chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.h"
-#import "chrome/browser/ui/cocoa/browser/password_generation_bubble_controller.h"
 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
 #import "chrome/browser/ui/cocoa/browser_window_utils.h"
 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
 #import "chrome/browser/ui/cocoa/info_bubble_view.h"
 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
 #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h"
+#import "chrome/browser/ui/cocoa/profiles/avatar_base_controller.h"
+#import "chrome/browser/ui/cocoa/profiles/avatar_menu_bubble_controller.h"
 #include "chrome/browser/ui/cocoa/restart_browser.h"
 #include "chrome/browser/ui/cocoa/status_bubble_mac.h"
 #include "chrome/browser/ui/cocoa/task_manager_mac.h"
 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
 #import "chrome/browser/ui/cocoa/web_dialog_window_controller.h"
-#import "chrome/browser/ui/cocoa/website_settings_bubble_controller.h"
+#import "chrome/browser/ui/cocoa/website_settings/website_settings_bubble_controller.h"
+#include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
 #include "chrome/browser/ui/search/search_model.h"
 #include "chrome/browser/ui/tabs/tab_strip_model.h"
 #include "chrome/browser/web_applications/web_app.h"
 #include "chrome/common/chrome_switches.h"
 #include "chrome/common/pref_names.h"
-#include "components/autofill/core/common/password_form.h"
+#include "components/translate/core/browser/language_state.h"
 #include "content/public/browser/native_web_keyboard_event.h"
 #include "content/public/browser/notification_details.h"
 #include "content/public/browser/notification_source.h"
 #include "content/public/browser/web_contents.h"
-#include "content/public/browser/web_contents_view.h"
-#include "grit/chromium_strings.h"
-#include "grit/generated_resources.h"
 #include "ui/base/l10n/l10n_util_mac.h"
 #include "ui/gfx/rect.h"
 
@@ -67,32 +66,12 @@ using content::NativeWebKeyboardEvent;
 using content::SSLStatus;
 using content::WebContents;
 
-// Replicate specific 10.7 SDK declarations for building with prior SDKs.
-#if !defined(MAC_OS_X_VERSION_10_7) || \
-    MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
-
-enum {
-  NSWindowAnimationBehaviorDefault = 0,
-  NSWindowAnimationBehaviorNone = 2,
-  NSWindowAnimationBehaviorDocumentWindow = 3,
-  NSWindowAnimationBehaviorUtilityWindow = 4,
-  NSWindowAnimationBehaviorAlertPanel = 5
-};
-typedef NSInteger NSWindowAnimationBehavior;
-
-@interface NSWindow (LionSDKDeclarations)
-- (NSWindowAnimationBehavior)animationBehavior;
-- (void)setAnimationBehavior:(NSWindowAnimationBehavior)newAnimationBehavior;
-@end
-
-#endif  // MAC_OS_X_VERSION_10_7
-
 namespace {
 
 NSPoint GetPointForBubble(content::WebContents* web_contents,
                           int x_offset,
                           int y_offset) {
-  NSView* view = web_contents->GetView()->GetNativeView();
+  NSView* view = web_contents->GetNativeView();
   NSRect bounds = [view bounds];
   NSPoint point;
   point.x = NSMinX(bounds) + x_offset;
@@ -146,7 +125,13 @@ void BrowserWindowCocoa::Show() {
     [window() setAnimationBehavior:NSWindowAnimationBehaviorNone];
   }
 
-  [window() makeKeyAndOrderFront:controller_];
+  {
+    TRACE_EVENT0("ui", "BrowserWindowCocoa::Show makeKeyAndOrderFront");
+    // This call takes up a substantial part of startup time, and an even more
+    // substantial part of startup time when any CALayers are part of the
+    // window's NSView heirarchy.
+    [window() makeKeyAndOrderFront:controller_];
+  }
 
   // When creating windows from nibs it is necessary to |makeKeyAndOrderFront:|
   // prior to |orderOut:| then |miniaturize:| when restoring windows in the
@@ -155,7 +140,7 @@ void BrowserWindowCocoa::Show() {
     [window() orderOut:controller_];
     [window() miniaturize:controller_];
   } else if (initial_show_state_ == ui::SHOW_STATE_FULLSCREEN) {
-    chrome::ToggleFullscreenMode(browser_);
+    chrome::ToggleFullscreenWithChromeOrFallback(browser_);
   }
   initial_show_state_ = ui::SHOW_STATE_DEFAULT;
 
@@ -298,7 +283,11 @@ void BrowserWindowCocoa::UpdateLoadingAnimations(bool should_animate) {
 }
 
 void BrowserWindowCocoa::SetStarredState(bool is_starred) {
-  [controller_ setStarredState:is_starred ? YES : NO];
+  [controller_ setStarredState:is_starred];
+}
+
+void BrowserWindowCocoa::SetTranslateIconToggled(bool is_lit) {
+  [controller_ setCurrentPageIsTranslated:is_lit];
 }
 
 void BrowserWindowCocoa::OnActiveTabChanged(content::WebContents* old_contents,
@@ -362,24 +351,24 @@ void BrowserWindowCocoa::Restore() {
     [window() deminiaturize:controller_];
 }
 
-void BrowserWindowCocoa::EnterFullscreen(
-      const GURL& url, FullscreenExitBubbleType bubble_type) {
-  // When simplified fullscreen is enabled, always enter normal fullscreen.
-  const CommandLine* command_line = CommandLine::ForCurrentProcess();
-  if (command_line->HasSwitch(switches::kEnableSimplifiedFullscreen)) {
-    if (url.is_empty())
-      [controller_ enterFullscreen];
-    else
-      [controller_ enterFullscreenForURL:url bubbleType:bubble_type];
+// See browser_window_controller.h for a detailed explanation of the logic in
+// this method.
+void BrowserWindowCocoa::EnterFullscreen(const GURL& url,
+                                         FullscreenExitBubbleType bubble_type) {
+  if (browser_->fullscreen_controller()->IsWindowFullscreenForTabOrPending()) {
+    [controller_ enterWebContentFullscreenForURL:url bubbleType:bubble_type];
     return;
   }
 
-  [controller_ enterPresentationModeForURL:url
-                                bubbleType:bubble_type];
+  if (url.is_empty()) {
+    [controller_ enterPresentationMode];
+  } else {
+    [controller_ enterExtensionFullscreenForURL:url bubbleType:bubble_type];
+  }
 }
 
 void BrowserWindowCocoa::ExitFullscreen() {
-  [controller_ exitFullscreen];
+  [controller_ exitAnyFullscreen];
 }
 
 void BrowserWindowCocoa::UpdateFullscreenExitBubbleContent(
@@ -394,9 +383,7 @@ bool BrowserWindowCocoa::ShouldHideUIForFullscreen() const {
 }
 
 bool BrowserWindowCocoa::IsFullscreen() const {
-  if ([controller_ inPresentationMode])
-    CHECK([controller_ isFullscreen]);  // Presentation mode must be fullscreen.
-  return [controller_ isFullscreen];
+  return [controller_ isInAnyFullscreenMode];
 }
 
 bool BrowserWindowCocoa::IsFullscreenBubbleVisible() const {
@@ -455,7 +442,8 @@ void BrowserWindowCocoa::FocusInfobars() {
 }
 
 bool BrowserWindowCocoa::IsBookmarkBarVisible() const {
-  return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar);
+  return browser_->profile()->GetPrefs()->GetBoolean(
+      bookmarks::prefs::kShowBookmarkBar);
 }
 
 bool BrowserWindowCocoa::IsBookmarkBarAnimating() const {
@@ -493,16 +481,33 @@ void BrowserWindowCocoa::ShowBookmarkBubble(const GURL& url,
                       alreadyBookmarked:(already_bookmarked ? YES : NO)];
 }
 
+void BrowserWindowCocoa::ShowBookmarkAppBubble(
+    const WebApplicationInfo& web_app_info,
+    const std::string& extension_id) {
+  NOTIMPLEMENTED();
+}
+
 void BrowserWindowCocoa::ShowTranslateBubble(
-      content::WebContents* contents,
-      TranslateBubbleModel::ViewState view_state) {
+    content::WebContents* contents,
+    translate::TranslateStep step,
+    translate::TranslateErrors::Type error_type,
+    bool is_user_gesture) {
+  ChromeTranslateClient* chrome_translate_client =
+      ChromeTranslateClient::FromWebContents(contents);
+  translate::LanguageState& language_state =
+      chrome_translate_client->GetLanguageState();
+  language_state.SetTranslateEnabled(true);
+
+  [controller_ showTranslateBubbleForWebContents:contents
+                                            step:step
+                                       errorType:error_type];
 }
 
 #if defined(ENABLE_ONE_CLICK_SIGNIN)
 void BrowserWindowCocoa::ShowOneClickSigninBubble(
     OneClickSigninBubbleType type,
-    const string16& email,
-    const string16& error_message,
+    const base::string16& email,
+    const base::string16& error_message,
     const StartSyncCallback& start_sync_callback) {
   WebContents* web_contents =
         browser_->tab_strip_model()->GetActiveWebContents();
@@ -527,6 +532,7 @@ bool BrowserWindowCocoa::IsDownloadShelfVisible() const {
 }
 
 DownloadShelf* BrowserWindowCocoa::GetDownloadShelf() {
+  [controller_ createAndAddDownloadShelf];
   DownloadShelfController* shelfController = [controller_ downloadShelf];
   return [shelfController bridge];
 }
@@ -571,8 +577,10 @@ bool BrowserWindowCocoa::PreHandleKeyboardEvent(
   if (![BrowserWindowUtils shouldHandleKeyboardEvent:event])
     return false;
 
-  if (event.type == WebKit::WebInputEvent::RawKeyDown &&
-      [controller_ handledByExtensionCommand:event.os_event])
+  if (event.type == blink::WebInputEvent::RawKeyDown &&
+      [controller_
+          handledByExtensionCommand:event.os_event
+                           priority:ui::AcceleratorManager::kHighPriority])
     return true;
 
   int id = [BrowserWindowUtils getCommandId:event];
@@ -607,44 +615,27 @@ void BrowserWindowCocoa::Paste() {
   [NSApp sendAction:@selector(paste:) to:nil from:nil];
 }
 
-void BrowserWindowCocoa::OpenTabpose() {
-  [controller_ openTabpose];
-}
-
 void BrowserWindowCocoa::EnterFullscreenWithChrome() {
-  // This method cannot be called if simplified fullscreen is enabled.
-  const CommandLine* command_line = CommandLine::ForCurrentProcess();
-  DCHECK(!command_line->HasSwitch(switches::kEnableSimplifiedFullscreen));
-
   CHECK(chrome::mac::SupportsSystemFullscreen());
-  if ([controller_ inPresentationMode])
-    [controller_ exitPresentationMode];
-  else
-    [controller_ enterFullscreen];
+  [controller_ enterFullscreenWithChrome];
+}
+
+void BrowserWindowCocoa::EnterFullscreenWithoutChrome() {
+  [controller_ enterPresentationMode];
 }
 
 bool BrowserWindowCocoa::IsFullscreenWithChrome() {
-  // The WithChrome mode does not exist when simplified fullscreen is enabled.
-  const CommandLine* command_line = CommandLine::ForCurrentProcess();
-  if (command_line->HasSwitch(switches::kEnableSimplifiedFullscreen))
-    return false;
   return IsFullscreen() && ![controller_ inPresentationMode];
 }
 
 bool BrowserWindowCocoa::IsFullscreenWithoutChrome() {
-  // Presentation mode does not exist if simplified fullscreen is enabled.  The
-  // WithoutChrome mode simply maps to whether or not the window is fullscreen.
-  const CommandLine* command_line = CommandLine::ForCurrentProcess();
-  if (command_line->HasSwitch(switches::kEnableSimplifiedFullscreen))
-    return IsFullscreen();
-
   return IsFullscreen() && [controller_ inPresentationMode];
 }
 
 WindowOpenDisposition BrowserWindowCocoa::GetDispositionForPopupBounds(
     const gfx::Rect& bounds) {
-  // In Lion fullscreen mode, convert popups into tabs.
-  if (chrome::mac::SupportsSystemFullscreen() && IsFullscreen())
+  // When using Cocoa's System Fullscreen mode, convert popups into tabs.
+  if ([controller_ isInAppKitFullscreen])
     return NEW_FOREGROUND_TAB;
   return NEW_POPUP;
 }
@@ -702,66 +693,27 @@ void BrowserWindowCocoa::ShowAvatarBubble(WebContents* web_contents,
   [menu showWindow:nil];
 }
 
-void BrowserWindowCocoa::ShowAvatarBubbleFromAvatarButton() {
-  AvatarButtonController* controller = [controller_ avatarButtonController];
-  [controller showAvatarBubble:[controller buttonView]];
-}
-
-void BrowserWindowCocoa::ShowPasswordGenerationBubble(
-    const gfx::Rect& rect,
-    const autofill::PasswordForm& form,
-    autofill::PasswordGenerator* password_generator) {
-  WebContents* web_contents =
-      browser_->tab_strip_model()->GetActiveWebContents();
-  // We want to point to the middle of the rect instead of the right side.
-  NSPoint point = GetPointForBubble(web_contents,
-                                    rect.x() + rect.width()/2,
-                                    rect.bottom());
-
-  PasswordGenerationBubbleController* controller =
-      [[PasswordGenerationBubbleController alloc]
-        initWithWindow:browser_->window()->GetNativeWindow()
-            anchoredAt:point
-        renderViewHost:web_contents->GetRenderViewHost()
-        passwordManager:PasswordManager::FromWebContents(web_contents)
-        usingGenerator:password_generator
-               forForm:form];
-  [controller showWindow:nil];
+void BrowserWindowCocoa::ShowAvatarBubbleFromAvatarButton(
+    AvatarBubbleMode mode,
+    const signin::ManageAccountsParams& manage_accounts_params) {
+  AvatarBaseController* controller = [controller_ avatarButtonController];
+  NSView* anchor = [controller buttonView];
+  if ([anchor isHiddenOrHasHiddenAncestor])
+    anchor = [[controller_ toolbarController] wrenchButton];
+  [controller showAvatarBubbleAnchoredAt:anchor
+                                withMode:mode
+                         withServiceType:manage_accounts_params.service_type];
 }
 
 int
 BrowserWindowCocoa::GetRenderViewHeightInsetWithDetachedBookmarkBar() {
   if (browser_->bookmark_bar_state() != BookmarkBar::DETACHED)
     return 0;
-  // TODO(sail): please make this work with cocoa, then enable
-  // BrowserTest.GetSizeForNewRenderView and
-  // WebContentsImplBrowserTest.GetSizeForNewRenderView.
-  // This function should return the extra height of the render view when
-  // detached bookmark bar is hidden.
-  // However, I (kuan) return 0 for now to retain the original behavior,
-  // because I encountered the following problem on cocoa:
-  // 1) When a navigation is requested,
-  //    WebContentsImpl::CreateRenderViewForRenderManager creates the new
-  //    RenderWidgetHostView at the size specified by
-  //    WebContentsDelegate::GetSizeForNewRenderView implemented by Browser.
-  // 2) When the pending navigation entry is committed,
-  //    WebContentsImpl::UpdateRenderViewSizeForRenderManager udpates the size
-  //    of WebContentsView to the size in (1).
-  // 3) WebContentsImpl::DidNavigateMainFramePostCommit() is called, where
-  //    the detached bookmark bar is hidden, resulting in relayout of tab
-  //    contents area.
-  // On cocoa, (2) causes RenderWidgetHostView to resize (enlarge) further.
-  // e.g. if size in (1) is size A, and this function returns height H, height
-  // of RenderWidgetHostView after (2) becomes A.height() + H; it's supposed to
-  // stay at A.height().
-  // Then, in (3), WebContentsView and RenderWidgetHostView enlarge even
-  // further, both by another H, i.e. WebContentsView's height becomes
-  // A.height() + H and RenderWidgetHostView's height becomes A.height() + 2H.
-  // Strangely, the RenderWidgetHostView for the previous navigation entry also
-  // gets enlarged by H.
-  // I believe these "automatic" resizing are caused by setAutoresizingMask of
-  // of the cocoa view in WebContentsViewMac, which defeats the purpose of
-  // WebContentsDelegate::GetSizeForNewRenderView i.e. to prevent resizing of
-  // RenderWidgetHostView in (2) and (3).
-  return 0;
+  return 40;
+}
+
+void BrowserWindowCocoa::ExecuteExtensionCommand(
+    const extensions::Extension* extension,
+    const extensions::Command& command) {
+  [cocoa_controller() executeExtensionCommand:extension->id() command:command];
 }