Chromium Code Reviews| Index: chrome/browser/ui/cocoa/applescript/apple_event_util.mm |
| diff --git a/chrome/browser/ui/cocoa/applescript/apple_event_util.mm b/chrome/browser/ui/cocoa/applescript/apple_event_util.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..783b129177d8ae83d3b7782947f55aa2d129a18e |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/applescript/apple_event_util.mm |
| @@ -0,0 +1,96 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "chrome/browser/ui/cocoa/applescript/apple_event_util.h" |
| + |
| +#import <Carbon/Carbon.h> |
| + |
| +#include "base/logging.h" |
| +#include "base/sys_string_conversions.h" |
| +#include "base/values.h" |
| + |
| +NSAppleEventDescriptor* ValueToAppleEventDescriptor(const base::Value* value) { |
| + NSAppleEventDescriptor* descriptor = nil; |
| + |
| + switch (value->GetType()) { |
| + case base::Value::TYPE_NULL: |
| + descriptor = [NSAppleEventDescriptor |
| + descriptorWithTypeCode:cMissingValue]; |
| + break; |
| + |
| + case base::Value::TYPE_BOOLEAN: { |
| + bool bool_value; |
| + value->GetAsBoolean(&bool_value); |
| + descriptor = [NSAppleEventDescriptor descriptorWithBoolean:bool_value]; |
| + break; |
| + } |
| + |
| + case base::Value::TYPE_INTEGER: { |
| + int int_value; |
| + value->GetAsInteger(&int_value); |
| + descriptor = [NSAppleEventDescriptor descriptorWithInt32:int_value]; |
| + break; |
| + } |
| + |
| + case base::Value::TYPE_DOUBLE: { |
| + double double_value; |
| + value->GetAsDouble(&double_value); |
| + descriptor = [NSAppleEventDescriptor |
| + descriptorWithDescriptorType:typeIEEE64BitFloatingPoint |
| + bytes:&double_value |
| + length:sizeof(double_value)]; |
| + break; |
| + } |
| + |
| + case base::Value::TYPE_STRING: { |
| + std::string string_value; |
|
Robert Sesek
2013/01/04 17:07:30
Are these user-facing strings? If so this should p
Avi (use Gerrit)
2013/01/04 18:05:20
These are strings coming back from Javascript; I d
|
| + value->GetAsString(&string_value); |
| + descriptor = [NSAppleEventDescriptor descriptorWithString: |
| + base::SysUTF8ToNSString(string_value)]; |
| + break; |
| + } |
| + |
| + case base::Value::TYPE_BINARY: |
| + NOTREACHED(); |
| + break; |
| + |
| + case base::Value::TYPE_DICTIONARY: { |
| + const base::DictionaryValue* dictionary_value = |
| + static_cast<const base::DictionaryValue*>(value); |
|
Robert Sesek
2013/01/04 17:07:30
You should use GetAsDictionary here.
Avi (use Gerrit)
2013/01/04 18:05:20
Done.
|
| + descriptor = [NSAppleEventDescriptor recordDescriptor]; |
| + NSAppleEventDescriptor* userRecord = [NSAppleEventDescriptor |
| + listDescriptor]; |
| + for (DictionaryValue::key_iterator iter(dictionary_value->begin_keys()); |
| + iter != dictionary_value->end_keys(); ++iter) { |
| + const base::Value* item; |
| + if (dictionary_value->Get(*iter, &item)) { |
| + [userRecord insertDescriptor:[NSAppleEventDescriptor |
| + descriptorWithString:base::SysUTF8ToNSString(*iter)] |
| + atIndex:0]; |
| + [userRecord insertDescriptor:ValueToAppleEventDescriptor(item) |
| + atIndex:0]; |
| + } |
| + } |
| + // Description of what keyASUserRecordFields does. |
| + // http://lists.apple.com/archives/cocoa-dev/2009/Jul/msg01216.html |
| + [descriptor setDescriptor:userRecord forKeyword:keyASUserRecordFields]; |
| + break; |
| + } |
| + |
| + case base::Value::TYPE_LIST: { |
| + const base::ListValue* list_value; |
| + value->GetAsList(&list_value); |
| + descriptor = [NSAppleEventDescriptor listDescriptor]; |
| + for (unsigned i = 0; i < list_value->GetSize(); ++i) { |
|
Robert Sesek
2013/01/04 17:07:30
size_t
Avi (use Gerrit)
2013/01/04 18:05:20
Done.
|
| + const base::Value* item; |
| + list_value->Get(i, &item); |
| + [descriptor insertDescriptor:ValueToAppleEventDescriptor(item) |
| + atIndex:0]; |
| + } |
| + break; |
| + } |
| + } |
| + |
| + return descriptor; |
| +} |