Modify documentation of APIs
[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     /// <typeparam name="T"></typeparam>
27     public abstract class MapServiceRequest<T> : IDisposable
28     {
29         protected TaskCompletionSource<IEnumerable<T>> _requestTask;
30         protected string errMessage;
31         protected int? _requestID;
32         protected ServiceRequestType _type;
33
34         internal Action startExecutionAction;
35         internal Interop.ErrorCode errorCode;
36
37         internal MapService _service;
38
39         /// <summary>
40         /// Creates a map service request.
41         /// </summary>
42         /// <param name="service">map service object</param>
43         /// <param name="type">Request type</param>
44         internal MapServiceRequest(MapService service, ServiceRequestType type)
45         {
46             _service = service;
47             _type = type;
48         }
49
50         /// <summary>
51         /// Sends a request to map service provider.
52         /// </summary>
53         /// <returns>Response from map service provider</returns>
54         /// <privilege>http://tizen.org/privilege/mapservice</privilege>
55         /// <privilege>http://tizen.org/privilege/internet</privilege>
56         /// <privilege>http://tizen.org/privilege/network.get</privilege>
57         /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
58         /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
59         /// <exception cref="System.InvalidOperationException">Thrown when the result is invalid.</exception>
60         /// <exception cref="System.ArgumentException">Thrown when arguments are invalid</exception>
61         public async Task<IEnumerable<T>> GetResponseAsync()
62         {
63             IEnumerable<T> task = null;
64             if (_requestTask == null || _requestTask.Task.IsCanceled)
65             {
66                 _requestTask = new TaskCompletionSource<IEnumerable<T>>();
67                 startExecutionAction();
68                 task = await _requestTask.Task;
69             }
70             errorCode.WarnIfFailed(errMessage);
71             return task;
72         }
73
74         internal void Cancel()
75         {
76             if (_requestTask?.Task.IsCompleted == false)
77             {
78                 _requestTask?.SetCanceled();
79                 if (_requestID != null)
80                 {
81                     var err = Interop.CancelRequest(_service.handle, (int)_requestID);
82                     err.ThrowIfFailed($"Unable to cancel service request, Type: {_type}, ID: {_requestID}");
83                 }
84
85                 errorCode = Interop.ErrorCode.Canceled;
86             }
87         }
88
89         #region IDisposable Support
90         private bool _disposedValue = false;
91
92         protected virtual void Dispose(bool disposing)
93         {
94             if (!_disposedValue)
95             {
96                 if (disposing)
97                 {
98                     Cancel();
99                     _service.Dispose();
100                 }
101                 _disposedValue = true;
102             }
103         }
104
105         /// <summary>
106         /// Releases all resources used by this object.
107         /// </summary>
108         public void Dispose()
109         {
110             Dispose(true);
111             GC.SuppressFinalize(this);
112         }
113         #endregion
114     }
115 }