1 // Copyright 2012 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.
5 #import "ios/chrome/browser/xcallback_parameters.h"
7 #include "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
12 NSString* const kSourceAppIdKey = @"sourceAppId";
13 NSString* const kSourceAppNameKey = @"sourceAppName";
14 NSString* const kSuccessURLKey = @"successURL";
15 NSString* const kCreateNewTabKey = @"createNewTab";
18 @interface XCallbackParameters () {
19 base::scoped_nsobject<NSString> _sourceAppId;
20 base::scoped_nsobject<NSString> _sourceAppName;
26 @implementation XCallbackParameters
28 @synthesize successURL = _successURL;
29 @synthesize createNewTab = _createNewTab;
31 - (instancetype)initWithSourceAppId:(NSString*)sourceAppId
32 sourceAppName:(NSString*)sourceAppName
33 successURL:(const GURL&)successURL
34 createNewTab:(BOOL)createNewTab {
37 _sourceAppId.reset([sourceAppId copy]);
38 _sourceAppName.reset([sourceAppName copy]);
39 _successURL = successURL;
40 _createNewTab = createNewTab;
45 - (instancetype)init {
50 - (NSString*)description {
51 return [NSString stringWithFormat:@"SourceApp: %@ (%@)\nSuccessURL: %s\n",
52 _sourceAppName.get(), _sourceAppId.get(),
53 _successURL.spec().c_str()];
56 - (NSString*)sourceAppId {
57 return _sourceAppId.get();
60 - (NSString*)sourceAppName {
61 return _sourceAppName.get();
64 #pragma mark - NSCoding Methods
66 - (instancetype)initWithCoder:(NSCoder*)aDecoder {
68 NSString* successURLStr = [aDecoder decodeObjectForKey:kSuccessURLKey];
70 _successURL = GURL(base::SysNSStringToUTF8(successURLStr));
73 [self initWithSourceAppId:[aDecoder decodeObjectForKey:kSourceAppIdKey]
74 sourceAppName:[aDecoder decodeObjectForKey:kSourceAppNameKey]
76 createNewTab:[aDecoder decodeBoolForKey:kCreateNewTabKey]];
79 - (void)encodeWithCoder:(NSCoder*)aCoder {
80 [aCoder encodeObject:_sourceAppId forKey:kSourceAppIdKey];
81 [aCoder encodeObject:_sourceAppName forKey:kSourceAppNameKey];
82 if (_successURL.is_valid()) {
83 NSString* successStr = base::SysUTF8ToNSString(_successURL.spec());
84 [aCoder encodeObject:successStr forKey:kSuccessURLKey];
86 [aCoder encodeBool:_createNewTab forKey:kCreateNewTabKey];
89 #pragma mark - NSCopying Methods
91 - (instancetype)copyWithZone:(NSZone*)zone {
92 XCallbackParameters* copy =
93 [[[self class] allocWithZone:zone] initWithSourceAppId:_sourceAppId
94 sourceAppName:_sourceAppName
95 successURL:_successURL
96 createNewTab:_createNewTab];