Fully support converting NSDictionary to JS userInfo object
This commit is contained in:
parent
8e1d2479ac
commit
60bd60e1ed
10 changed files with 105 additions and 51 deletions
|
@ -254,7 +254,7 @@ void App::OnFinishLaunching() {
|
||||||
void App::OnContinueUserActivity(
|
void App::OnContinueUserActivity(
|
||||||
bool* prevent_default,
|
bool* prevent_default,
|
||||||
const std::string& type,
|
const std::string& type,
|
||||||
const std::map<std::string, std::string>& user_info) {
|
const base::DictionaryValue& user_info) {
|
||||||
*prevent_default = Emit("continue-activity", type, user_info);
|
*prevent_default = Emit("continue-activity", type, user_info);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
#define ATOM_BROWSER_API_ATOM_API_APP_H_
|
#define ATOM_BROWSER_API_ATOM_API_APP_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
|
||||||
|
|
||||||
#include "atom/browser/api/event_emitter.h"
|
#include "atom/browser/api/event_emitter.h"
|
||||||
#include "atom/browser/atom_browser_client.h"
|
#include "atom/browser/atom_browser_client.h"
|
||||||
|
@ -76,7 +75,7 @@ class App : public AtomBrowserClient::Delegate,
|
||||||
void OnContinueUserActivity(
|
void OnContinueUserActivity(
|
||||||
bool* prevent_default,
|
bool* prevent_default,
|
||||||
const std::string& type,
|
const std::string& type,
|
||||||
const std::map<std::string, std::string>& user_info) override;
|
const base::DictionaryValue& user_info) override;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// content::ContentBrowserClient:
|
// content::ContentBrowserClient:
|
||||||
|
|
|
@ -137,19 +137,6 @@ void Browser::Activate(bool has_visible_windows) {
|
||||||
OnActivate(has_visible_windows));
|
OnActivate(has_visible_windows));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(OS_MACOSX)
|
|
||||||
bool Browser::ContinueUserActivity(
|
|
||||||
const std::string& type,
|
|
||||||
const std::map<std::string, std::string>& user_info) {
|
|
||||||
bool prevent_default = false;
|
|
||||||
FOR_EACH_OBSERVER(BrowserObserver,
|
|
||||||
observers_,
|
|
||||||
OnContinueUserActivity(&prevent_default, type, user_info));
|
|
||||||
|
|
||||||
return prevent_default;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void Browser::WillFinishLaunching() {
|
void Browser::WillFinishLaunching() {
|
||||||
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWillFinishLaunching());
|
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWillFinishLaunching());
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
|
||||||
|
|
||||||
#include "base/macros.h"
|
#include "base/macros.h"
|
||||||
#include "base/compiler_specific.h"
|
#include "base/compiler_specific.h"
|
||||||
|
@ -95,15 +94,14 @@ class Browser : public WindowListObserver {
|
||||||
|
|
||||||
// Creates an activity and sets it as the one currently in use.
|
// Creates an activity and sets it as the one currently in use.
|
||||||
void SetUserActivity(const std::string& type,
|
void SetUserActivity(const std::string& type,
|
||||||
const std::map<std::string, std::string>& user_info);
|
const base::DictionaryValue& user_info);
|
||||||
|
|
||||||
// Returns the type name of the current user activity.
|
// Returns the type name of the current user activity.
|
||||||
std::string GetCurrentActivityType();
|
std::string GetCurrentActivityType();
|
||||||
|
|
||||||
// Resumes an activity via hand-off.
|
// Resumes an activity via hand-off.
|
||||||
bool ContinueUserActivity(
|
bool ContinueUserActivity(const std::string& type,
|
||||||
const std::string& type,
|
const base::DictionaryValue& user_info);
|
||||||
const std::map<std::string, std::string>& user_info);
|
|
||||||
|
|
||||||
// Bounce the dock icon.
|
// Bounce the dock icon.
|
||||||
enum BounceType {
|
enum BounceType {
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include "atom/browser/mac/atom_application.h"
|
#include "atom/browser/mac/atom_application.h"
|
||||||
#include "atom/browser/mac/atom_application_delegate.h"
|
#include "atom/browser/mac/atom_application_delegate.h"
|
||||||
|
#include "atom/browser/mac/dict_util.h"
|
||||||
#include "atom/browser/native_window.h"
|
#include "atom/browser/native_window.h"
|
||||||
#include "atom/browser/window_list.h"
|
#include "atom/browser/window_list.h"
|
||||||
#include "base/mac/bundle_locations.h"
|
#include "base/mac/bundle_locations.h"
|
||||||
|
@ -89,24 +90,14 @@ void Browser::SetAppUserModelID(const base::string16& name) {
|
||||||
|
|
||||||
void Browser::SetUserActivity(
|
void Browser::SetUserActivity(
|
||||||
const std::string& type,
|
const std::string& type,
|
||||||
const std::map<std::string, std::string>& user_info) {
|
const base::DictionaryValue& user_info) {
|
||||||
NSString* type_ns = [NSString stringWithUTF8String:type.c_str()];
|
NSString* nstype = [NSString stringWithUTF8String:type.c_str()];
|
||||||
NSUserActivity* user_activity =
|
NSUserActivity* userActivity =
|
||||||
[[NSUserActivity alloc] initWithActivityType:type_ns];
|
[[NSUserActivity alloc] initWithActivityType:nstype];
|
||||||
|
userActivity.userInfo = DictionaryValueToNSDictionary(user_info);
|
||||||
|
[userActivity becomeCurrent];
|
||||||
|
|
||||||
base::scoped_nsobject<NSMutableDictionary> user_info_args(
|
[[AtomApplication sharedApplication] setCurrentActivity:userActivity];
|
||||||
[[NSMutableDictionary alloc] init]);
|
|
||||||
for (auto const &pair : user_info) {
|
|
||||||
NSString* value_ns = [NSString stringWithUTF8String:pair.second.c_str()];
|
|
||||||
NSString* key_ns = [NSString stringWithUTF8String:pair.first.c_str()];
|
|
||||||
|
|
||||||
[user_info_args.get() setObject:value_ns forKey:key_ns];
|
|
||||||
}
|
|
||||||
|
|
||||||
user_activity.userInfo = user_info_args.get();
|
|
||||||
[user_activity becomeCurrent];
|
|
||||||
|
|
||||||
[[AtomApplication sharedApplication] setCurrentActivity:user_activity];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Browser::GetCurrentActivityType() {
|
std::string Browser::GetCurrentActivityType() {
|
||||||
|
@ -115,6 +106,16 @@ std::string Browser::GetCurrentActivityType() {
|
||||||
return base::SysNSStringToUTF8(user_activity.activityType);
|
return base::SysNSStringToUTF8(user_activity.activityType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Browser::ContinueUserActivity(
|
||||||
|
const std::string& type,
|
||||||
|
const base::DictionaryValue& user_info) {
|
||||||
|
bool prevent_default = false;
|
||||||
|
FOR_EACH_OBSERVER(BrowserObserver,
|
||||||
|
observers_,
|
||||||
|
OnContinueUserActivity(&prevent_default, type, user_info));
|
||||||
|
return prevent_default;
|
||||||
|
}
|
||||||
|
|
||||||
std::string Browser::GetExecutableFileVersion() const {
|
std::string Browser::GetExecutableFileVersion() const {
|
||||||
return brightray::GetApplicationVersion();
|
return brightray::GetApplicationVersion();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,13 @@
|
||||||
#define ATOM_BROWSER_BROWSER_OBSERVER_H_
|
#define ATOM_BROWSER_BROWSER_OBSERVER_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
|
||||||
|
|
||||||
#include "build/build_config.h"
|
#include "build/build_config.h"
|
||||||
|
|
||||||
|
namespace base {
|
||||||
|
class DictionaryValue;
|
||||||
|
}
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
class LoginHandler;
|
class LoginHandler;
|
||||||
|
@ -53,7 +56,7 @@ class BrowserObserver {
|
||||||
virtual void OnContinueUserActivity(
|
virtual void OnContinueUserActivity(
|
||||||
bool* prevent_default,
|
bool* prevent_default,
|
||||||
const std::string& type,
|
const std::string& type,
|
||||||
const std::map<std::string, std::string>& user_info) {}
|
const base::DictionaryValue& user_info) {}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -6,7 +6,9 @@
|
||||||
|
|
||||||
#import "atom/browser/mac/atom_application.h"
|
#import "atom/browser/mac/atom_application.h"
|
||||||
#include "atom/browser/browser.h"
|
#include "atom/browser/browser.h"
|
||||||
|
#include "atom/browser/mac/dict_util.h"
|
||||||
#include "base/strings/sys_string_conversions.h"
|
#include "base/strings/sys_string_conversions.h"
|
||||||
|
#include "base/values.h"
|
||||||
|
|
||||||
@implementation AtomApplicationDelegate
|
@implementation AtomApplicationDelegate
|
||||||
|
|
||||||
|
@ -63,19 +65,13 @@
|
||||||
continueUserActivity:(NSUserActivity*)userActivity
|
continueUserActivity:(NSUserActivity*)userActivity
|
||||||
restorationHandler:(void (^)(NSArray*restorableObjects))restorationHandler {
|
restorationHandler:(void (^)(NSArray*restorableObjects))restorationHandler {
|
||||||
std::string activity_type(base::SysNSStringToUTF8(userActivity.activityType));
|
std::string activity_type(base::SysNSStringToUTF8(userActivity.activityType));
|
||||||
|
scoped_ptr<base::DictionaryValue> user_info =
|
||||||
std::map<std::string, std::string> user_info;
|
atom::NSDictionaryToDictionaryValue(userActivity.userInfo);
|
||||||
base::scoped_nsobject<NSArray> keys([userActivity.userInfo allKeys]);
|
if (!user_info)
|
||||||
|
return NO;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
atom::Browser* browser = atom::Browser::Get();
|
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
|
@end
|
||||||
|
|
26
atom/browser/mac/dict_util.h
Normal file
26
atom/browser/mac/dict_util.h
Normal 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_
|
42
atom/browser/mac/dict_util.mm
Normal file
42
atom/browser/mac/dict_util.mm
Normal 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
|
|
@ -183,6 +183,8 @@
|
||||||
'atom/browser/mac/atom_application.mm',
|
'atom/browser/mac/atom_application.mm',
|
||||||
'atom/browser/mac/atom_application_delegate.h',
|
'atom/browser/mac/atom_application_delegate.h',
|
||||||
'atom/browser/mac/atom_application_delegate.mm',
|
'atom/browser/mac/atom_application_delegate.mm',
|
||||||
|
'atom/browser/mac/dict_util.h',
|
||||||
|
'atom/browser/mac/dict_util.mm',
|
||||||
'atom/browser/native_window.cc',
|
'atom/browser/native_window.cc',
|
||||||
'atom/browser/native_window.h',
|
'atom/browser/native_window.h',
|
||||||
'atom/browser/native_window_views_win.cc',
|
'atom/browser/native_window_views_win.cc',
|
||||||
|
|
Loading…
Reference in a new issue