Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / browser / xcallback_parameters.mm
bloba660a9053161657d8016abaa40ebee29bdf55a86
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"
11 namespace {
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;
21   GURL _successURL;
22   BOOL _createNewTab;
24 @end
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 {
35   self = [super init];
36   if (self) {
37     _sourceAppId.reset([sourceAppId copy]);
38     _sourceAppName.reset([sourceAppName copy]);
39     _successURL = successURL;
40     _createNewTab = createNewTab;
41   }
42   return self;
45 - (instancetype)init {
46   NOTREACHED();
47   return nil;
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 {
67   GURL successURL;
68   NSString* successURLStr = [aDecoder decodeObjectForKey:kSuccessURLKey];
69   if (successURLStr)
70     _successURL = GURL(base::SysNSStringToUTF8(successURLStr));
72   return
73       [self initWithSourceAppId:[aDecoder decodeObjectForKey:kSourceAppIdKey]
74                   sourceAppName:[aDecoder decodeObjectForKey:kSourceAppNameKey]
75                      successURL:successURL
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];
85   }
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];
97   return copy;
100 @end