Home | History | Annotate | Download | only in bookmarks
      1 // Copyright (c) 2012 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 #include "chrome/browser/ui/bookmarks/bookmark_context_menu_controller.h"
      6 
      7 #include "chrome/app/chrome_command_ids.h"
      8 #include "chrome/browser/bookmarks/bookmark_utils.h"
      9 #include "chrome/browser/profiles/profile.h"
     10 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
     11 #include "chrome/browser/ui/browser.h"
     12 #include "chrome/browser/ui/browser_finder.h"
     13 #include "chrome/browser/ui/browser_window.h"
     14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     15 #include "content/public/browser/page_navigator.h"
     16 #include "content/public/browser/user_metrics.h"
     17 #include "content/public/browser/web_contents.h"
     18 #include "win8/util/win8_util.h"
     19 
     20 using content::OpenURLParams;
     21 using content::PageNavigator;
     22 using content::UserMetricsAction;
     23 using content::WebContents;
     24 
     25 namespace {
     26 
     27 // A PageNavigator implementation that creates a new Browser. This is used when
     28 // opening a url and there is no Browser open. The Browser is created the first
     29 // time the PageNavigator method is invoked.
     30 class NewBrowserPageNavigator : public PageNavigator {
     31  public:
     32   explicit NewBrowserPageNavigator(Profile* profile)
     33       : profile_(profile),
     34         browser_(NULL) {}
     35 
     36   virtual ~NewBrowserPageNavigator() {
     37     if (browser_)
     38       browser_->window()->Show();
     39   }
     40 
     41   Browser* browser() const { return browser_; }
     42 
     43   virtual WebContents* OpenURL(const OpenURLParams& params) OVERRIDE {
     44     if (!browser_) {
     45       Profile* profile = (params.disposition == OFF_THE_RECORD) ?
     46           profile_->GetOffTheRecordProfile() : profile_;
     47       browser_ = new Browser(Browser::CreateParams(profile,
     48                                                    chrome::GetActiveDesktop()));
     49     }
     50 
     51     OpenURLParams forward_params = params;
     52     forward_params.disposition = NEW_FOREGROUND_TAB;
     53     return browser_->OpenURL(forward_params);
     54   }
     55 
     56  private:
     57   Profile* profile_;
     58   Browser* browser_;
     59 
     60   DISALLOW_COPY_AND_ASSIGN(NewBrowserPageNavigator);
     61 };
     62 
     63 }  // namespace
     64 
     65 bool BookmarkContextMenuController::IsPlatformCommandIdEnabled(
     66     int command_id,
     67     bool* enabled) const {
     68   // In Windows 8 metro mode no new window option on a regular chrome window
     69   // and no new incognito window option on an incognito chrome window.
     70   if (win8::IsSingleWindowMetroMode()) {
     71     if (command_id == IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW &&
     72         !profile_->IsOffTheRecord()) {
     73       *enabled = false;
     74       return true;
     75     } else if (command_id == IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO &&
     76                profile_->IsOffTheRecord()) {
     77       *enabled = false;
     78       return true;
     79     }
     80   }
     81   return false;
     82 }
     83 
     84 bool BookmarkContextMenuController::ExecutePlatformCommand(int command_id,
     85                                                            int event_flags) {
     86   if (win8::IsSingleWindowMetroMode()) {
     87     switch (command_id) {
     88       // We need to handle the open in new window and open in incognito window
     89       // commands to ensure that they first look for an existing browser object
     90       // to handle the request. If we find one then a new foreground tab is
     91       // opened, else a new browser object is created.
     92       case IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW:
     93       case IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO: {
     94         Profile* profile_to_use = profile_;
     95         if (command_id == IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW) {
     96           if (profile_to_use->IsOffTheRecord())
     97             profile_to_use = profile_to_use->GetOriginalProfile();
     98 
     99           content::RecordAction(
    100               UserMetricsAction("BookmarkBar_ContextMenu_OpenAllInNewWindow"));
    101         } else {
    102           if (!profile_to_use->IsOffTheRecord())
    103             profile_to_use = profile_to_use->GetOffTheRecordProfile();
    104 
    105           content::RecordAction(
    106               UserMetricsAction("BookmarkBar_ContextMenu_OpenAllIncognito"));
    107         }
    108 
    109         NewBrowserPageNavigator navigator_impl(profile_to_use);
    110 
    111         // TODO(robertshield): FTB - Switch this to HOST_DESKTOP_TYPE_ASH when
    112         //                     we make that the default for metro.
    113         Browser* browser =
    114             chrome::FindTabbedBrowser(profile_to_use,
    115                                       false,
    116                                       chrome::HOST_DESKTOP_TYPE_NATIVE);
    117         PageNavigator* navigator = NULL;
    118         if (!browser || !browser->tab_strip_model()->GetActiveWebContents()) {
    119           navigator = &navigator_impl;
    120         } else {
    121           browser->window()->Activate();
    122           navigator = browser->tab_strip_model()->GetActiveWebContents();
    123         }
    124 
    125         chrome::OpenAll(parent_window_, navigator, selection_,
    126                         NEW_FOREGROUND_TAB, profile_to_use);
    127         bookmark_utils::RecordBookmarkLaunch(
    128             bookmark_utils::LAUNCH_CONTEXT_MENU);
    129         return true;
    130       }
    131 
    132       default:
    133         break;
    134     }
    135   }
    136 
    137   return false;
    138 }
    139