Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / views_content_client / views_content_client_main_parts_mac.mm
blob2c86c375140e0e02a1234b71dcdf3af6ba5da6c4
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> {
18  @private
19   base::Closure task_;
22 // Set the task to run after receiving -applicationDidFinishLaunching:.
23 - (void)setTask:(const base::Closure&)task;
25 @end
27 namespace ui {
29 namespace {
31 class ViewsContentClientMainPartsMac : public ViewsContentClientMainParts {
32  public:
33   ViewsContentClientMainPartsMac(
34       const content::MainFunctionParams& content_params,
35       ViewsContentClient* views_content_client);
36   virtual ~ViewsContentClientMainPartsMac();
38   // content::BrowserMainParts:
39   virtual void PreMainMessageLoopRun() OVERRIDE;
41  private:
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];
75 }  // namespace
77 // static
78 ViewsContentClientMainParts* ViewsContentClientMainParts::Create(
79     const content::MainFunctionParams& content_params,
80     ViewsContentClient* views_content_client) {
81   return
82       new ViewsContentClientMainPartsMac(content_params, views_content_client);
85 }  // namespace ui
87 @implementation ViewsContentClientAppController
89 - (void)setTask:(const base::Closure&)task {
90   task_ = 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:)
112               keyEquivalent:@"q"];
113   [appMenuItem setSubmenu:appMenu];
115   task_.Run();
118 @end