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/views/tab_contents/render_view_context_menu_win.h"
6
7#include "chrome/app/chrome_command_ids.h"
8#include "chrome/browser/chrome_notification_types.h"
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/tab_contents/retargeting_details.h"
11#include "chrome/browser/ui/browser_finder.h"
12#include "chrome/browser/ui/tabs/tab_strip_model.h"
13#include "content/public/browser/web_contents.h"
14#include "win8/util/win8_util.h"
15
16using content::WebContents;
17
18RenderViewContextMenuWin::RenderViewContextMenuWin(
19    WebContents* web_contents,
20    const content::ContextMenuParams& params)
21    : RenderViewContextMenuViews(web_contents, params) {
22}
23
24RenderViewContextMenuWin::~RenderViewContextMenuWin() {
25}
26
27// static
28RenderViewContextMenuViews* RenderViewContextMenuViews::Create(
29    content::WebContents* web_contents,
30    const content::ContextMenuParams& params) {
31  return new RenderViewContextMenuWin(web_contents, params);
32}
33
34bool RenderViewContextMenuWin::IsCommandIdVisible(int command_id) const {
35  // In windows 8 metro mode no new window option on normal browser windows.
36  if (win8::IsSingleWindowMetroMode() && !profile_->IsOffTheRecord() &&
37      command_id == IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW) {
38    return false;
39  }
40  return RenderViewContextMenu::IsCommandIdVisible(command_id);
41}
42
43void RenderViewContextMenuWin::ExecuteCommand(int command_id,
44                                              int event_flags) {
45  if (win8::IsSingleWindowMetroMode() &&
46      command_id == IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW) {
47    // The open link in new window command should only be enabled for
48    // incognito windows in metro mode.
49    DCHECK(profile_->IsOffTheRecord());
50    // We directly go to the Browser object to open the url in effect
51    // bypassing the delegate. Currently the Browser is the only class which
52    // implements the delegate for the context menu. This would break if there
53    // are other delegates for the context menu. This is ok for now as this
54    // code only executes for Windows 8 metro mode.
55    // TODO(robertshield): FTB - Switch this to HOST_DESKTOP_TYPE_ASH when
56    //                     we make that the default for metro.
57    Browser* browser =
58        chrome::FindTabbedBrowser(profile_->GetOriginalProfile(),
59                                  false,
60                                  chrome::HOST_DESKTOP_TYPE_NATIVE);
61    if (browser) {
62      content::OpenURLParams url_params(
63          params_.link_url,
64          content::Referrer(params_.frame_url.is_empty() ?
65                                params_.page_url : params_.frame_url,
66                            params_.referrer_policy),
67          NEW_FOREGROUND_TAB,
68          content::PAGE_TRANSITION_LINK,
69          false);
70      WebContents* source_web_contents =
71          browser->tab_strip_model()->GetActiveWebContents();
72      WebContents* new_contents = source_web_contents->OpenURL(url_params);
73      DCHECK(new_contents);
74      return;
75    }
76  }
77  RenderViewContextMenu::ExecuteCommand(command_id, event_flags);
78}
79
80void RenderViewContextMenuWin::SetExternal() {
81  external_ = true;
82}
83