1 // Copyright 2014 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.
5 #import <Cocoa/Cocoa.h>
7 #include "base/files/file_path.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/path_service.h"
10 #include "content/public/common/content_paths.h"
11 #include "content/shell/browser/shell_browser_context.h"
12 #include "ui/views_content_client/views_content_client.h"
13 #include "ui/views_content_client/views_content_client_main_parts.h"
15 // A simple NSApplicationDelegate that provides a basic mainMenu and can
16 // activate a task when the application has finished loading.
17 @interface ViewsContentClientAppController : NSObject<NSApplicationDelegate> {
22 // Set the task to run after receiving -applicationDidFinishLaunching:.
23 - (void)setTask:(const base::Closure&)task;
31 class ViewsContentClientMainPartsMac : public ViewsContentClientMainParts {
33 ViewsContentClientMainPartsMac(
34 const content::MainFunctionParams& content_params,
35 ViewsContentClient* views_content_client);
36 virtual ~ViewsContentClientMainPartsMac();
38 // content::BrowserMainParts:
39 virtual void PreMainMessageLoopRun() OVERRIDE;
42 base::scoped_nsobject<ViewsContentClientAppController> app_controller_;
44 DISALLOW_COPY_AND_ASSIGN(ViewsContentClientMainPartsMac);
47 ViewsContentClientMainPartsMac::ViewsContentClientMainPartsMac(
48 const content::MainFunctionParams& content_params,
49 ViewsContentClient* views_content_client)
50 : ViewsContentClientMainParts(content_params, views_content_client) {
51 // Cache the child process path to avoid triggering an AssertIOAllowed.
52 base::FilePath child_process_exe;
53 PathService::Get(content::CHILD_PROCESS_EXE, &child_process_exe);
55 app_controller_.reset([[ViewsContentClientAppController alloc] init]);
56 [[NSApplication sharedApplication] setDelegate:app_controller_];
59 void ViewsContentClientMainPartsMac::PreMainMessageLoopRun() {
60 ViewsContentClientMainParts::PreMainMessageLoopRun();
62 // On Mac, the task must be deferred to applicationDidFinishLaunching. If not,
63 // the widget can activate, but (even if configured) the mainMenu won't be
64 // ready to switch over in the OSX UI, so it will look strange.
65 NSWindow* window_context = nil;
66 [app_controller_ setTask:base::Bind(views_content_client()->task(),
67 base::Unretained(browser_context()),
68 base::Unretained(window_context))];
71 ViewsContentClientMainPartsMac::~ViewsContentClientMainPartsMac() {
72 [[NSApplication sharedApplication] setDelegate:nil];
78 ViewsContentClientMainParts* ViewsContentClientMainParts::Create(
79 const content::MainFunctionParams& content_params,
80 ViewsContentClient* views_content_client) {
82 new ViewsContentClientMainPartsMac(content_params, views_content_client);
87 @implementation ViewsContentClientAppController
89 - (void)setTask:(const base::Closure&)task {
93 - (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
94 // To get key events, the application needs to have an activation policy.
95 // Unbundled apps (i.e. those without an Info.plist) default to
96 // NSApplicationActivationPolicyProhibited.
97 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
99 // Create a basic mainMenu object using the executable filename.
100 base::scoped_nsobject<NSMenu> mainMenu([[NSMenu alloc] initWithTitle:@""]);
101 NSMenuItem* appMenuItem =
102 [mainMenu addItemWithTitle:@"" action:NULL keyEquivalent:@""];
103 [NSApp setMainMenu:mainMenu];
105 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]);
106 NSString* appName = [[NSProcessInfo processInfo] processName];
107 // TODO(tapted): Localize "Quit" if this is ever used for a released binary.
108 // At the time of writing, ui_strings.grd has "Close" but not "Quit".
109 NSString* quitTitle = [@"Quit " stringByAppendingString:appName];
110 [appMenu addItemWithTitle:quitTitle
111 action:@selector(terminate:)
113 [appMenuItem setSubmenu:appMenu];