Fully support converting NSDictionary to JS userInfo object

This commit is contained in:
Cheng Zhao 2016-05-05 16:26:44 +09:00
parent 8e1d2479ac
commit 60bd60e1ed
10 changed files with 105 additions and 51 deletions

View file

@ -6,7 +6,9 @@
#import "atom/browser/mac/atom_application.h"
#include "atom/browser/browser.h"
#include "atom/browser/mac/dict_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
@implementation AtomApplicationDelegate
@ -63,19 +65,13 @@
continueUserActivity:(NSUserActivity*)userActivity
restorationHandler:(void (^)(NSArray*restorableObjects))restorationHandler {
std::string activity_type(base::SysNSStringToUTF8(userActivity.activityType));
std::map<std::string, std::string> user_info;
base::scoped_nsobject<NSArray> keys([userActivity.userInfo allKeys]);
for (NSString* key in keys.get()) {
NSString* value = [userActivity.userInfo objectForKey:key];
std::string key_str(base::SysNSStringToUTF8(key));
std::string value_str(base::SysNSStringToUTF8(value));
user_info[key_str] = value_str;
}
scoped_ptr<base::DictionaryValue> user_info =
atom::NSDictionaryToDictionaryValue(userActivity.userInfo);
if (!user_info)
return NO;
atom::Browser* browser = atom::Browser::Get();
return browser->ContinueUserActivity(activity_type, user_info) ? YES : NO;
return browser->ContinueUserActivity(activity_type, *user_info) ? YES : NO;
}
@end

View file

@ -0,0 +1,26 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_MAC_DICT_UTIL_H_
#define ATOM_BROWSER_MAC_DICT_UTIL_H_
#import <Foundation/Foundation.h>
#include "base/memory/scoped_ptr.h"
namespace base {
class Value;
class DictionaryValue;
}
namespace atom {
NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value);
scoped_ptr<base::DictionaryValue> NSDictionaryToDictionaryValue(
NSDictionary* dict);
} // namespace atom
#endif // ATOM_BROWSER_MAC_DICT_UTIL_H_

View file

@ -0,0 +1,42 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/mac/dict_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/values.h"
namespace atom {
NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value) {
std::string json;
if (!base::JSONWriter::Write(value, &json))
return nil;
NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
id obj = [NSJSONSerialization JSONObjectWithData:jsonData
options:0
error:nil];
if (![obj isKindOfClass:[NSDictionary class]])
return nil;
return obj;
}
scoped_ptr<base::DictionaryValue> NSDictionaryToDictionaryValue(
NSDictionary* dict) {
NSData* data = [NSJSONSerialization dataWithJSONObject:dict
options:0
error:nil];
if (!data)
return nullptr;
base::scoped_nsobject<NSString> json =
[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
scoped_ptr<base::Value> value =
base::JSONReader::Read([json UTF8String]);
return base::DictionaryValue::From(std::move(value));
}
} // namespace atom