Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7269)

Unified Diff: chrome/browser/ui/cocoa/applescript/tab_applescript.mm

Issue 11742028: Suspend Apple Events during their processing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix test Created 7 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/cocoa/applescript/tab_applescript.mm
diff --git a/chrome/browser/ui/cocoa/applescript/tab_applescript.mm b/chrome/browser/ui/cocoa/applescript/tab_applescript.mm
index 99c1b323bf149e7eb58c815e326afcd47f3304d9..b128a57241abe0db6520013c32863e3ad3fb32b9 100644
--- a/chrome/browser/ui/cocoa/applescript/tab_applescript.mm
+++ b/chrome/browser/ui/cocoa/applescript/tab_applescript.mm
@@ -4,17 +4,15 @@
#import "chrome/browser/ui/cocoa/applescript/tab_applescript.h"
-#import <Carbon/Carbon.h>
-#import <Foundation/NSAppleEventDescriptor.h>
-
+#include "base/bind.h"
#include "base/file_path.h"
#include "base/logging.h"
#import "base/memory/scoped_nsobject.h"
#include "base/sys_string_conversions.h"
-#include "base/utf_string_conversions.h"
#include "chrome/browser/printing/print_view_manager.h"
#include "chrome/browser/sessions/session_id.h"
#include "chrome/browser/sessions/session_tab_helper.h"
+#include "chrome/browser/ui/cocoa/applescript/apple_event_util.h"
#include "chrome/browser/ui/cocoa/applescript/error_applescript.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/navigation_controller.h"
@@ -32,100 +30,22 @@ using content::RenderViewHost;
using content::Referrer;
using content::WebContents;
-@interface AnyResultValue : NSObject {
- @private
- scoped_nsobject<NSAppleEventDescriptor> descriptor;
-}
-- (id)initWithDescriptor:(NSAppleEventDescriptor*)desc;
-- (NSAppleEventDescriptor *)scriptingAnyDescriptor;
-@end
+namespace {
-@implementation AnyResultValue
-
-- (id)initWithDescriptor:(NSAppleEventDescriptor*)desc {
- if (self = [super init]) {
- descriptor.reset([desc retain]);
- }
- return self;
-}
+void ResumeAppleEventAndSendReply(NSAppleEventManagerSuspensionID suspension_id,
+ const base::Value* result_value) {
+ NSAppleEventDescriptor* result_descriptor =
+ chrome::mac::ValueToAppleEventDescriptor(result_value);
-- (NSAppleEventDescriptor *)scriptingAnyDescriptor {
- return descriptor.get();
+ NSAppleEventManager* manager = [NSAppleEventManager sharedAppleEventManager];
+ NSAppleEventDescriptor* reply_event =
+ [manager replyAppleEventForSuspensionID:suspension_id];
+ [reply_event setParamDescriptor:result_descriptor
+ forKeyword:keyDirectObject];
+ [manager resumeWithSuspensionID:suspension_id];
}
-@end
-
-static NSAppleEventDescriptor* valueToDescriptor(Value* value) {
- NSAppleEventDescriptor* descriptor = nil;
- switch (value->GetType()) {
- case Value::TYPE_NULL:
- descriptor = [NSAppleEventDescriptor
- descriptorWithTypeCode:cMissingValue];
- break;
- case Value::TYPE_BOOLEAN: {
- bool bool_value;
- value->GetAsBoolean(&bool_value);
- descriptor = [NSAppleEventDescriptor descriptorWithBoolean:bool_value];
- break;
- }
- case Value::TYPE_INTEGER: {
- int int_value;
- value->GetAsInteger(&int_value);
- descriptor = [NSAppleEventDescriptor descriptorWithInt32:int_value];
- break;
- }
- case Value::TYPE_DOUBLE: {
- double double_value;
- value->GetAsDouble(&double_value);
- descriptor = [NSAppleEventDescriptor
- descriptorWithDescriptorType:typeIEEE64BitFloatingPoint
- bytes:&double_value
- length:sizeof(double_value)];
- break;
- }
- case Value::TYPE_STRING: {
- std::string string_value;
- value->GetAsString(&string_value);
- descriptor = [NSAppleEventDescriptor descriptorWithString:
- base::SysUTF8ToNSString(string_value)];
- break;
- }
- case Value::TYPE_BINARY:
- NOTREACHED();
- break;
- case Value::TYPE_DICTIONARY: {
- DictionaryValue* dictionary_value = static_cast<DictionaryValue*>(value);
- descriptor = [NSAppleEventDescriptor recordDescriptor];
- NSAppleEventDescriptor* userRecord = [NSAppleEventDescriptor
- listDescriptor];
- for (DictionaryValue::key_iterator iter(dictionary_value->begin_keys());
- iter != dictionary_value->end_keys(); ++iter) {
- Value* item;
- if (dictionary_value->Get(*iter, &item)) {
- [userRecord insertDescriptor:[NSAppleEventDescriptor
- descriptorWithString:base::SysUTF8ToNSString(*iter)] atIndex:0];
- [userRecord insertDescriptor:valueToDescriptor(item) atIndex:0];
- }
- }
- // Description of what keyASUserRecordFields does.
- // http://www.mail-archive.com/cocoa-dev%40lists.apple.com/msg40149.html
- [descriptor setDescriptor:userRecord forKeyword:keyASUserRecordFields];
- break;
- }
- case Value::TYPE_LIST: {
- ListValue* list_value;
- value->GetAsList(&list_value);
- descriptor = [NSAppleEventDescriptor listDescriptor];
- for (unsigned i = 0; i < list_value->GetSize(); ++i) {
- Value* item;
- list_value->Get(i, &item);
- [descriptor insertDescriptor:valueToDescriptor(item) atIndex:0];
- }
- break;
- }
- }
- return descriptor;
-}
+} // namespace
@interface TabAppleScript()
@property (nonatomic, copy) NSString* tempURL;
@@ -141,8 +61,7 @@ static NSAppleEventDescriptor* valueToDescriptor(Value* value) {
SessionID::id_type futureSessionIDOfTab = session.id() + 1;
// Holds the SessionID that the new tab is going to get.
scoped_nsobject<NSNumber> numID(
- [[NSNumber alloc]
- initWithInt:futureSessionIDOfTab]);
+ [[NSNumber alloc] initWithInt:futureSessionIDOfTab]);
[self setUniqueID:numID];
}
return self;
@@ -160,8 +79,8 @@ static NSAppleEventDescriptor* valueToDescriptor(Value* value) {
}
if ((self = [super init])) {
- // It is safe to be weak, if a tab goes away (eg user closing a tab)
- // the applescript runtime calls tabs in AppleScriptWindow and this
+ // It is safe to be weak; if a tab goes away (e.g. the user closes a tab)
+ // the AppleScript runtime calls tabs in AppleScriptWindow and this
// particular tab is never returned.
webContents_ = webContents;
SessionTabHelper* session_tab_helper =
@@ -175,8 +94,8 @@ static NSAppleEventDescriptor* valueToDescriptor(Value* value) {
- (void)setWebContents:(content::WebContents*)webContents {
DCHECK(webContents);
- // It is safe to be weak, if a tab goes away (eg user closing a tab)
- // the applescript runtime calls tabs in AppleScriptWindow and this
+ // It is safe to be weak; if a tab goes away (e.g. the user closes a tab)
+ // the AppleScript runtime calls tabs in AppleScriptWindow and this
// particular tab is never returned.
webContents_ = webContents;
SessionTabHelper* session_tab_helper =
@@ -402,11 +321,19 @@ static NSAppleEventDescriptor* valueToDescriptor(Value* value) {
return nil;
}
+ NSAppleEventManager* manager = [NSAppleEventManager sharedAppleEventManager];
+ NSAppleEventManagerSuspensionID suspensionID =
+ [manager suspendCurrentAppleEvent];
+ content::RenderViewHost::JavascriptResultCallback callback =
+ base::Bind(&ResumeAppleEventAndSendReply, suspensionID);
+
string16 script = base::SysNSStringToUTF16(
[[command evaluatedArguments] objectForKey:@"javascript"]);
- Value* value = view->ExecuteJavascriptAndGetValue(string16(), script);
- NSAppleEventDescriptor* descriptor = valueToDescriptor(value);
- return [[[AnyResultValue alloc] initWithDescriptor:descriptor] autorelease];
+ view->ExecuteJavascriptInWebFrameCallbackResult(string16(), // frame_xpath
+ script,
+ callback);
+
+ return nil;
}
@end

Powered by Google App Engine
This is Rietveld 408576698