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/mac/scoped_nsobject.h"
8 #include "base/strings/sys_string_conversions.h"
11 NSString* const kSourceAppIdKey = @"sourceAppId";
12 NSString* const kSourceAppNameKey = @"sourceAppName";
13 NSString* const kSuccessURLKey = @"successURL";
14 NSString* const kCreateNewTabKey = @"createNewTab";
17 @interface XCallbackParameters () {
18 base::scoped_nsobject<NSString> _sourceAppId;
19 base::scoped_nsobject<NSString> _sourceAppName;
25 @implementation XCallbackParameters
27 @synthesize successURL = _successURL;
28 @synthesize createNewTab = _createNewTab;
30 - (instancetype)initWithSourceAppId:(NSString*)sourceAppId
31 sourceAppName:(NSString*)sourceAppName
32 successURL:(const GURL&)successURL
33 createNewTab:(BOOL)createNewTab {
36 _sourceAppId.reset([sourceAppId copy]);
37 _sourceAppName.reset([sourceAppName copy]);
38 _successURL = successURL;
39 _createNewTab = createNewTab;
44 - (NSString*)description {
45 return [NSString stringWithFormat:@"SourceApp: %@ (%@)\nSuccessURL: %s\n",
46 _sourceAppName.get(), _sourceAppId.get(),
47 _successURL.spec().c_str()];
50 - (NSString*)sourceAppId {
51 return _sourceAppId.get();
54 - (NSString*)sourceAppName {
55 return _sourceAppName.get();
58 #pragma mark - NSCoding Methods
60 - (instancetype)initWithCoder:(NSCoder*)aDecoder {
62 NSString* successURLStr = [aDecoder decodeObjectForKey:kSuccessURLKey];
64 _successURL = GURL(base::SysNSStringToUTF8(successURLStr));
67 [self initWithSourceAppId:[aDecoder decodeObjectForKey:kSourceAppIdKey]
68 sourceAppName:[aDecoder decodeObjectForKey:kSourceAppNameKey]
70 createNewTab:[aDecoder decodeBoolForKey:kCreateNewTabKey]];
73 - (void)encodeWithCoder:(NSCoder*)aCoder {
74 [aCoder encodeObject:_sourceAppId forKey:kSourceAppIdKey];
75 [aCoder encodeObject:_sourceAppName forKey:kSourceAppNameKey];
76 if (_successURL.is_valid()) {
77 NSString* successStr = base::SysUTF8ToNSString(_successURL.spec());
78 [aCoder encodeObject:successStr forKey:kSuccessURLKey];
80 [aCoder encodeBool:_createNewTab forKey:kCreateNewTabKey];
83 #pragma mark - NSCopying Methods
85 - (instancetype)copyWithZone:(NSZone*)zone {
86 XCallbackParameters* copy = [[[self class] allocWithZone:zone] init];
87 copy->_sourceAppId.reset([_sourceAppId copy]);
88 copy->_sourceAppName.reset([_sourceAppName copy]);
89 copy->_successURL = _successURL;
90 copy->_createNewTab = _createNewTab;