OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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_thread_helpers.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #include "base/task.h" |
| 10 #include "chrome/browser/chrome_thread.h" |
| 11 |
| 12 // This is a wrapper for running Chrome Task objects from within a native run |
| 13 // loop. A typical use case is when Chrome work needs to get done but the main |
| 14 // message loop is blocked by a nested run loop (like an event tracking one). |
| 15 // This can run specific tasks in that nested loop. This owns the task and will |
| 16 // delete it and itself when done. |
| 17 @interface UITaskHelperMac : NSObject { |
| 18 @private |
| 19 Task* task_; |
| 20 } |
| 21 - (id)initWithTask:(Task*)task; |
| 22 - (void)runTask; |
| 23 @end |
| 24 |
| 25 @implementation UITaskHelperMac |
| 26 - (id)initWithTask:(Task*)task { |
| 27 if ((self = [super init])) { |
| 28 task_ = task; |
| 29 } |
| 30 return self; |
| 31 } |
| 32 |
| 33 - (void)runTask { |
| 34 task_->Run(); |
| 35 [self autorelease]; |
| 36 } |
| 37 |
| 38 - (void)dealloc { |
| 39 delete task_; |
| 40 [super dealloc]; |
| 41 } |
| 42 @end |
| 43 |
| 44 namespace ui_thread_helpers { |
| 45 |
| 46 bool PostTaskWhileRunningMenu(const tracked_objects::Location& from_here, |
| 47 Task* task) { |
| 48 // This deletes itself and the task after the task runs. |
| 49 UITaskHelperMac* runner = [[UITaskHelperMac alloc] initWithTask:task]; |
| 50 |
| 51 // Schedule the selector in multiple modes in case this was called while a |
| 52 // menu was not running. |
| 53 NSArray* modes = [NSArray arrayWithObjects:NSEventTrackingRunLoopMode, |
| 54 NSDefaultRunLoopMode, |
| 55 nil]; |
| 56 [runner performSelectorOnMainThread:@selector(runTask) |
| 57 withObject:nil |
| 58 waitUntilDone:NO |
| 59 modes:modes]; |
| 60 return true; |
| 61 } |
| 62 |
| 63 } // namespace ui_thread_helpers |
OLD | NEW |