aboutsummaryrefslogtreecommitdiffhomepage
path: root/GoogleUtilities/Network/Private
diff options
context:
space:
mode:
Diffstat (limited to 'GoogleUtilities/Network/Private')
-rw-r--r--GoogleUtilities/Network/Private/GULMutableDictionary.h46
-rw-r--r--GoogleUtilities/Network/Private/GULNetwork.h87
-rw-r--r--GoogleUtilities/Network/Private/GULNetworkConstants.h79
-rw-r--r--GoogleUtilities/Network/Private/GULNetworkLoggerProtocol.h51
-rw-r--r--GoogleUtilities/Network/Private/GULNetworkMessageCode.h44
-rw-r--r--GoogleUtilities/Network/Private/GULNetworkURLSession.h60
6 files changed, 367 insertions, 0 deletions
diff --git a/GoogleUtilities/Network/Private/GULMutableDictionary.h b/GoogleUtilities/Network/Private/GULMutableDictionary.h
new file mode 100644
index 0000000..a8cc45b
--- /dev/null
+++ b/GoogleUtilities/Network/Private/GULMutableDictionary.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import <Foundation/Foundation.h>
+
+/// A mutable dictionary that provides atomic accessor and mutators.
+@interface GULMutableDictionary : NSObject
+
+/// Returns an object given a key in the dictionary or nil if not found.
+- (id)objectForKey:(id)key;
+
+/// Updates the object given its key or adds it to the dictionary if it is not in the dictionary.
+- (void)setObject:(id)object forKey:(id<NSCopying>)key;
+
+/// Removes the object given its session ID from the dictionary.
+- (void)removeObjectForKey:(id)key;
+
+/// Removes all objects.
+- (void)removeAllObjects;
+
+/// Returns the number of current objects in the dictionary.
+- (NSUInteger)count;
+
+/// Returns an object given a key in the dictionary or nil if not found.
+- (id)objectForKeyedSubscript:(id<NSCopying>)key;
+
+/// Updates the object given its key or adds it to the dictionary if it is not in the dictionary.
+- (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key;
+
+/// Returns the immutable dictionary.
+- (NSDictionary *)dictionary;
+
+@end
diff --git a/GoogleUtilities/Network/Private/GULNetwork.h b/GoogleUtilities/Network/Private/GULNetwork.h
new file mode 100644
index 0000000..0e75ae5
--- /dev/null
+++ b/GoogleUtilities/Network/Private/GULNetwork.h
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import <Foundation/Foundation.h>
+
+#import "GULNetworkConstants.h"
+#import "GULNetworkLoggerProtocol.h"
+#import "GULNetworkURLSession.h"
+
+/// Delegate protocol for GULNetwork events.
+@protocol GULNetworkReachabilityDelegate
+
+/// Tells the delegate to handle events when the network reachability changes to connected or not
+/// connected.
+- (void)reachabilityDidChange;
+
+@end
+
+/// The Network component that provides network status and handles network requests and responses.
+/// This is not thread safe.
+///
+/// NOTE:
+/// User must add FIRAnalytics handleEventsForBackgroundURLSessionID:completionHandler to the
+/// AppDelegate application:handleEventsForBackgroundURLSession:completionHandler:
+@interface GULNetwork : NSObject
+
+/// Indicates if network connectivity is available.
+@property(nonatomic, readonly, getter=isNetworkConnected) BOOL networkConnected;
+
+/// Indicates if there are any uploads in progress.
+@property(nonatomic, readonly, getter=hasUploadInProgress) BOOL uploadInProgress;
+
+/// An optional delegate that can be used in the event when network reachability changes.
+@property(nonatomic, weak) id<GULNetworkReachabilityDelegate> reachabilityDelegate;
+
+/// An optional delegate that can be used to log messages, warnings or errors that occur in the
+/// network operations.
+@property(nonatomic, weak) id<GULNetworkLoggerDelegate> loggerDelegate;
+
+/// Indicates whether the logger should display debug messages.
+@property(nonatomic, assign) BOOL isDebugModeEnabled;
+
+/// The time interval in seconds for the network request to timeout.
+@property(nonatomic, assign) NSTimeInterval timeoutInterval;
+
+/// Initializes with the default reachability host.
+- (instancetype)init;
+
+/// Initializes with a custom reachability host.
+- (instancetype)initWithReachabilityHost:(NSString *)reachabilityHost;
+
+/// Handles events when background session with the given ID has finished.
++ (void)handleEventsForBackgroundURLSessionID:(NSString *)sessionID
+ completionHandler:(GULNetworkSystemCompletionHandler)completionHandler;
+
+/// Compresses and sends a POST request with the provided data to the URL. The session will be
+/// background session if usingBackgroundSession is YES. Otherwise, the POST session is default
+/// session. Returns a session ID or nil if an error occurs.
+- (NSString *)postURL:(NSURL *)url
+ payload:(NSData *)payload
+ queue:(dispatch_queue_t)queue
+ usingBackgroundSession:(BOOL)usingBackgroundSession
+ completionHandler:(GULNetworkCompletionHandler)handler;
+
+/// Sends a GET request with the provided data to the URL. The session will be background session
+/// if usingBackgroundSession is YES. Otherwise, the GET session is default session. Returns a
+/// session ID or nil if an error occurs.
+- (NSString *)getURL:(NSURL *)url
+ headers:(NSDictionary *)headers
+ queue:(dispatch_queue_t)queue
+ usingBackgroundSession:(BOOL)usingBackgroundSession
+ completionHandler:(GULNetworkCompletionHandler)handler;
+
+@end
diff --git a/GoogleUtilities/Network/Private/GULNetworkConstants.h b/GoogleUtilities/Network/Private/GULNetworkConstants.h
new file mode 100644
index 0000000..44d440b
--- /dev/null
+++ b/GoogleUtilities/Network/Private/GULNetworkConstants.h
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import <Foundation/Foundation.h>
+#import <GoogleUtilities/GULLogger.h>
+
+/// Error codes in Firebase Network error domain.
+/// Note: these error codes should never change. It would make it harder to decode the errors if
+/// we inadvertently altered any of these codes in a future SDK version.
+typedef NS_ENUM(NSInteger, GULNetworkErrorCode) {
+ /// Unknown error.
+ GULNetworkErrorCodeUnknown = 0,
+ /// Error occurs when the request URL is invalid.
+ GULErrorCodeNetworkInvalidURL = 1,
+ /// Error occurs when request cannot be constructed.
+ GULErrorCodeNetworkRequestCreation = 2,
+ /// Error occurs when payload cannot be compressed.
+ GULErrorCodeNetworkPayloadCompression = 3,
+ /// Error occurs when session task cannot be created.
+ GULErrorCodeNetworkSessionTaskCreation = 4,
+ /// Error occurs when there is no response.
+ GULErrorCodeNetworkInvalidResponse = 5
+};
+
+#pragma mark - Network constants
+
+/// The prefix of the ID of the background session.
+extern NSString *const kGULNetworkBackgroundSessionConfigIDPrefix;
+
+/// The sub directory to store the files of data that is being uploaded in the background.
+extern NSString *const kGULNetworkApplicationSupportSubdirectory;
+
+/// Name of the temporary directory that stores files for background uploading.
+extern NSString *const kGULNetworkTempDirectoryName;
+
+/// The period when the temporary uploading file can stay.
+extern const NSTimeInterval kGULNetworkTempFolderExpireTime;
+
+/// The default network request timeout interval.
+extern const NSTimeInterval kGULNetworkTimeOutInterval;
+
+/// The host to check the reachability of the network.
+extern NSString *const kGULNetworkReachabilityHost;
+
+/// The key to get the error context of the UserInfo.
+extern NSString *const kGULNetworkErrorContext;
+
+#pragma mark - Network Status Code
+
+extern const int kGULNetworkHTTPStatusOK;
+extern const int kGULNetworkHTTPStatusNoContent;
+extern const int kGULNetworkHTTPStatusCodeMultipleChoices;
+extern const int kGULNetworkHTTPStatusCodeMovedPermanently;
+extern const int kGULNetworkHTTPStatusCodeFound;
+extern const int kGULNetworkHTTPStatusCodeNotModified;
+extern const int kGULNetworkHTTPStatusCodeMovedTemporarily;
+extern const int kGULNetworkHTTPStatusCodeNotFound;
+extern const int kGULNetworkHTTPStatusCodeCannotAcceptTraffic;
+extern const int kGULNetworkHTTPStatusCodeUnavailable;
+
+#pragma mark - Error Domain
+
+extern NSString *const kGULNetworkErrorDomain;
+
+/// The logger service for GULNetwork.
+extern GULLoggerService kGULLoggerNetwork;
diff --git a/GoogleUtilities/Network/Private/GULNetworkLoggerProtocol.h b/GoogleUtilities/Network/Private/GULNetworkLoggerProtocol.h
new file mode 100644
index 0000000..f1be590
--- /dev/null
+++ b/GoogleUtilities/Network/Private/GULNetworkLoggerProtocol.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import <Foundation/Foundation.h>
+
+#import <GoogleUtilities/GULLoggerLevel.h>
+
+#import "GULNetworkMessageCode.h"
+
+/// The log levels used by GULNetworkLogger.
+typedef NS_ENUM(NSInteger, GULNetworkLogLevel) {
+ kGULNetworkLogLevelError = GULLoggerLevelError,
+ kGULNetworkLogLevelWarning = GULLoggerLevelWarning,
+ kGULNetworkLogLevelInfo = GULLoggerLevelInfo,
+ kGULNetworkLogLevelDebug = GULLoggerLevelDebug,
+};
+
+@protocol GULNetworkLoggerDelegate <NSObject>
+
+@required
+/// Tells the delegate to log a message with an array of contexts and the log level.
+- (void)GULNetwork_logWithLevel:(GULNetworkLogLevel)logLevel
+ messageCode:(GULNetworkMessageCode)messageCode
+ message:(NSString *)message
+ contexts:(NSArray *)contexts;
+
+/// Tells the delegate to log a message with a context and the log level.
+- (void)GULNetwork_logWithLevel:(GULNetworkLogLevel)logLevel
+ messageCode:(GULNetworkMessageCode)messageCode
+ message:(NSString *)message
+ context:(id)context;
+
+/// Tells the delegate to log a message with the log level.
+- (void)GULNetwork_logWithLevel:(GULNetworkLogLevel)logLevel
+ messageCode:(GULNetworkMessageCode)messageCode
+ message:(NSString *)message;
+
+@end
diff --git a/GoogleUtilities/Network/Private/GULNetworkMessageCode.h b/GoogleUtilities/Network/Private/GULNetworkMessageCode.h
new file mode 100644
index 0000000..ce78e60
--- /dev/null
+++ b/GoogleUtilities/Network/Private/GULNetworkMessageCode.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Make sure these codes do not overlap with any contained in the FIRAMessageCode enum.
+typedef NS_ENUM(NSInteger, GULNetworkMessageCode) {
+ // GULNetwork.m
+ kGULNetworkMessageCodeNetwork000 = 900000, // I-NET900000
+ kGULNetworkMessageCodeNetwork001 = 900001, // I-NET900001
+ kGULNetworkMessageCodeNetwork002 = 900002, // I-NET900002
+ kGULNetworkMessageCodeNetwork003 = 900003, // I-NET900003
+ // GULNetworkURLSession.m
+ kGULNetworkMessageCodeURLSession000 = 901000, // I-NET901000
+ kGULNetworkMessageCodeURLSession001 = 901001, // I-NET901001
+ kGULNetworkMessageCodeURLSession002 = 901002, // I-NET901002
+ kGULNetworkMessageCodeURLSession003 = 901003, // I-NET901003
+ kGULNetworkMessageCodeURLSession004 = 901004, // I-NET901004
+ kGULNetworkMessageCodeURLSession005 = 901005, // I-NET901005
+ kGULNetworkMessageCodeURLSession006 = 901006, // I-NET901006
+ kGULNetworkMessageCodeURLSession007 = 901007, // I-NET901007
+ kGULNetworkMessageCodeURLSession008 = 901008, // I-NET901008
+ kGULNetworkMessageCodeURLSession009 = 901009, // I-NET901009
+ kGULNetworkMessageCodeURLSession010 = 901010, // I-NET901010
+ kGULNetworkMessageCodeURLSession011 = 901011, // I-NET901011
+ kGULNetworkMessageCodeURLSession012 = 901012, // I-NET901012
+ kGULNetworkMessageCodeURLSession013 = 901013, // I-NET901013
+ kGULNetworkMessageCodeURLSession014 = 901014, // I-NET901014
+ kGULNetworkMessageCodeURLSession015 = 901015, // I-NET901015
+ kGULNetworkMessageCodeURLSession016 = 901016, // I-NET901016
+ kGULNetworkMessageCodeURLSession017 = 901017, // I-NET901017
+ kGULNetworkMessageCodeURLSession018 = 901018, // I-NET901018
+};
diff --git a/GoogleUtilities/Network/Private/GULNetworkURLSession.h b/GoogleUtilities/Network/Private/GULNetworkURLSession.h
new file mode 100644
index 0000000..81190c6
--- /dev/null
+++ b/GoogleUtilities/Network/Private/GULNetworkURLSession.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import <Foundation/Foundation.h>
+
+#import "GULNetworkLoggerProtocol.h"
+
+typedef void (^GULNetworkCompletionHandler)(NSHTTPURLResponse *response,
+ NSData *data,
+ NSError *error);
+typedef void (^GULNetworkURLSessionCompletionHandler)(NSHTTPURLResponse *response,
+ NSData *data,
+ NSString *sessionID,
+ NSError *error);
+typedef void (^GULNetworkSystemCompletionHandler)(void);
+
+/// The protocol that uses NSURLSession for iOS >= 7.0 to handle requests and responses.
+@interface GULNetworkURLSession
+ : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate>
+
+/// Indicates whether the background network is enabled. Default value is NO.
+@property(nonatomic, getter=isBackgroundNetworkEnabled) BOOL backgroundNetworkEnabled;
+
+/// The logger delegate to log message, errors or warnings that occur during the network operations.
+@property(nonatomic, weak) id<GULNetworkLoggerDelegate> loggerDelegate;
+
+/// Calls the system provided completion handler after the background session is finished.
++ (void)handleEventsForBackgroundURLSessionID:(NSString *)sessionID
+ completionHandler:(GULNetworkSystemCompletionHandler)completionHandler;
+
+/// Initializes with logger delegate.
+- (instancetype)initWithNetworkLoggerDelegate:(id<GULNetworkLoggerDelegate>)networkLoggerDelegate
+ NS_DESIGNATED_INITIALIZER;
+
+- (instancetype)init NS_UNAVAILABLE;
+
+/// Sends an asynchronous POST request and calls the provided completion handler when the request
+/// completes or when errors occur, and returns an ID of the session/connection.
+- (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
+ completionHandler:(GULNetworkURLSessionCompletionHandler)handler;
+
+/// Sends an asynchronous GET request and calls the provided completion handler when the request
+/// completes or when errors occur, and returns an ID of the session.
+- (NSString *)sessionIDFromAsyncGETRequest:(NSURLRequest *)request
+ completionHandler:(GULNetworkURLSessionCompletionHandler)handler;
+
+@end