1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "chrome/browser/ui/cocoa/history_menu_cocoa_controller.h"
6
7#include "base/memory/scoped_vector.h"
8#include "chrome/app/chrome_command_ids.h"  // IDC_HISTORY_MENU
9#import "chrome/browser/app_controller_mac.h"
10#include "chrome/browser/history/history.h"
11#include "chrome/browser/history/history_types.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/ui/browser.h"
14#include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
15#include "chrome/browser/ui/cocoa/event_utils.h"
16#include "webkit/glue/window_open_disposition.h"
17
18@implementation HistoryMenuCocoaController
19
20- (id)initWithBridge:(HistoryMenuBridge*)bridge {
21  if ((self = [super init])) {
22    bridge_ = bridge;
23    DCHECK(bridge_);
24  }
25  return self;
26}
27
28- (BOOL)validateMenuItem:(NSMenuItem*)menuItem {
29  AppController* controller = [NSApp delegate];
30  return [controller keyWindowIsNotModal];
31}
32
33// Open the URL of the given history item in the current tab.
34- (void)openURLForItem:(const HistoryMenuBridge::HistoryItem*)node {
35  Browser* browser = Browser::GetOrCreateTabbedBrowser(bridge_->profile());
36  WindowOpenDisposition disposition =
37      event_utils::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
38
39  // If this item can be restored using TabRestoreService, do so. Otherwise,
40  // just load the URL.
41  TabRestoreService* service = bridge_->profile()->GetTabRestoreService();
42  if (node->session_id && service) {
43    service->RestoreEntryById(browser->tab_restore_service_delegate(),
44        node->session_id, false);
45  } else {
46    DCHECK(node->url.is_valid());
47    browser->OpenURL(node->url, GURL(), disposition,
48                     PageTransition::AUTO_BOOKMARK);
49  }
50}
51
52- (IBAction)openHistoryMenuItem:(id)sender {
53  const HistoryMenuBridge::HistoryItem* item =
54      bridge_->HistoryItemForMenuItem(sender);
55  [self openURLForItem:item];
56}
57
58@end  // HistoryMenuCocoaController
59