[Maps] Modify diposing routines
[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 a 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         internal TaskCompletionSource<IEnumerable<T>> _requestTask;
31         internal string errMessage;
32         internal int? _requestID;
33         internal 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         /// Destroy the MapServiceRequest object.
53         /// </summary>
54         ~MapServiceRequest()
55         {
56             Dispose(false);
57         }
58
59         /// <summary>
60         /// Sends a request to the map service provider.
61         /// </summary>
62         /// <since_tizen> 3 </since_tizen>
63         /// <returns>Response from the map service provider.</returns>
64         /// <privilege>http://tizen.org/privilege/mapservice</privilege>
65         /// <privilege>http://tizen.org/privilege/internet</privilege>
66         /// <privilege>http://tizen.org/privilege/network.get</privilege>
67         /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
68         /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
69         /// <exception cref="System.InvalidOperationException">Thrown when the result is invalid.</exception>
70         /// <exception cref="System.ArgumentException">Thrown when arguments are invalid.</exception>
71         public async Task<IEnumerable<T>> GetResponseAsync()
72         {
73             IEnumerable<T> task = null;
74             if (_requestTask == null || _requestTask.Task.IsCanceled)
75             {
76                 _requestTask = new TaskCompletionSource<IEnumerable<T>>();
77                 startExecutionAction();
78                 task = await _requestTask.Task.ConfigureAwait(false);
79             }
80             errorCode.WarnIfFailed(errMessage);
81             return task;
82         }
83
84         internal void Cancel()
85         {
86             if (_requestTask?.Task.IsCompleted == false)
87             {
88                 _requestTask?.SetCanceled();
89                 if (_requestID != null)
90                 {
91                     var err = Interop.CancelRequest(_service.handle, (int)_requestID);
92                     err.ThrowIfFailed($"Unable to cancel service request, Type: {_type}, ID: {_requestID}");
93                 }
94
95                 errorCode = Interop.ErrorCode.Canceled;
96             }
97         }
98
99         #region IDisposable Support
100         private bool _disposedValue = false;
101
102         /// <summary>
103         /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
104         /// </summary>
105         /// <param name="disposing">If true, managed and unmanaged resources can be disposed, otherwise only unmanaged resources can be disposed.</param>
106         /// <since_tizen> 3 </since_tizen>
107         protected virtual void Dispose(bool disposing)
108         {
109             if (!_disposedValue)
110             {
111                 if (disposing)
112                 {
113                     Cancel();
114                 }
115                 _disposedValue = true;
116             }
117         }
118
119         /// <summary>
120         /// Releases all the resources used by this object.
121         /// </summary>
122         /// <since_tizen> 3 </since_tizen>
123         public void Dispose()
124         {
125             Dispose(true);
126             GC.SuppressFinalize(this);
127         }
128         #endregion
129     }
130 }