Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / MapServiceRequest.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Threading.Tasks;
20
21 namespace Tizen.Maps
22 {
23     /// <summary>
24     /// Base class for map service request
25     /// </summary>
26     /// <since_tizen>3</since_tizen>
27     /// <typeparam name="T"></typeparam>
28     public abstract class MapServiceRequest<T> : IDisposable
29     {
30         protected TaskCompletionSource<IEnumerable<T>> _requestTask;
31         protected string errMessage;
32         protected int? _requestID;
33         protected ServiceRequestType _type;
34
35         internal Action startExecutionAction;
36         internal Interop.ErrorCode errorCode;
37
38         internal MapService _service;
39
40         /// <summary>
41         /// Creates a map service request.
42         /// </summary>
43         /// <param name="service">map service object</param>
44         /// <param name="type">Request type</param>
45         internal MapServiceRequest(MapService service, ServiceRequestType type)
46         {
47             _service = service;
48             _type = type;
49         }
50
51         /// <summary>
52         /// Sends a request to map service provider.
53         /// </summary>
54         /// <since_tizen>3</since_tizen>
55         /// <returns>Response from map service provider</returns>
56         /// <privilege>http://tizen.org/privilege/mapservice</privilege>
57         /// <privilege>http://tizen.org/privilege/internet</privilege>
58         /// <privilege>http://tizen.org/privilege/network.get</privilege>
59         /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
60         /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
61         /// <exception cref="System.InvalidOperationException">Thrown when the result is invalid.</exception>
62         /// <exception cref="System.ArgumentException">Thrown when arguments are invalid</exception>
63         public async Task<IEnumerable<T>> GetResponseAsync()
64         {
65             IEnumerable<T> task = null;
66             if (_requestTask == null || _requestTask.Task.IsCanceled)
67             {
68                 _requestTask = new TaskCompletionSource<IEnumerable<T>>();
69                 startExecutionAction();
70                 task = await _requestTask.Task.ConfigureAwait(false);
71             }
72             errorCode.WarnIfFailed(errMessage);
73             return task;
74         }
75
76         internal void Cancel()
77         {
78             if (_requestTask?.Task.IsCompleted == false)
79             {
80                 _requestTask?.SetCanceled();
81                 if (_requestID != null)
82                 {
83                     var err = Interop.CancelRequest(_service.handle, (int)_requestID);
84                     err.ThrowIfFailed($"Unable to cancel service request, Type: {_type}, ID: {_requestID}");
85                 }
86
87                 errorCode = Interop.ErrorCode.Canceled;
88             }
89         }
90
91         #region IDisposable Support
92         private bool _disposedValue = false;
93
94         protected virtual void Dispose(bool disposing)
95         {
96             if (!_disposedValue)
97             {
98                 if (disposing)
99                 {
100                     Cancel();
101                     _service.Dispose();
102                 }
103                 _disposedValue = true;
104             }
105         }
106
107         /// <summary>
108         /// Releases all resources used by this object.
109         /// </summary>
110         /// <since_tizen>3</since_tizen>
111         public void Dispose()
112         {
113             Dispose(true);
114             GC.SuppressFinalize(this);
115         }
116         #endregion
117     }
118 }