OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_CONTROLLER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_CONTROLLER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_CONTROLLER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <map> | 10 #include <map> |
11 | 11 |
12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
13 #include "chrome/browser/extensions/api/api_resource.h" | 13 #include "chrome/browser/extensions/api/api_resource.h" |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
17 class SerialConnection; | 17 class SerialConnection; |
18 class Socket; | 18 class Socket; |
| 19 class UsbDeviceResource; |
19 | 20 |
20 // kSrcIdKey, or "srcId," binds an APIResource to the onEvent closure that was | 21 // kSrcIdKey, or "srcId," binds an APIResource to the onEvent closure that was |
21 // optionally passed to the APIResource's create() method. We generated it in | 22 // optionally passed to the APIResource's create() method. We generated it in |
22 // schema_generated_bindings.js; the application code is unaware of it. | 23 // schema_generated_bindings.js; the application code is unaware of it. |
23 extern const char kSrcIdKey[]; | 24 extern const char kSrcIdKey[]; |
24 | 25 |
25 // APIController keeps track of a collection of APIResources and provides a | 26 // APIController keeps track of a collection of APIResources and provides a |
26 // convenient set of methods to work with them. | 27 // convenient set of methods to work with them. |
27 class APIResourceController { | 28 class APIResourceController { |
28 public: | 29 public: |
29 APIResourceController(); | 30 APIResourceController(); |
30 virtual ~APIResourceController(); | 31 virtual ~APIResourceController(); |
31 | 32 |
32 // Takes ownership of api_resource. | 33 // Takes ownership of api_resource. |
33 int AddAPIResource(APIResource* api_resource); | 34 int AddAPIResource(APIResource* api_resource); |
34 bool RemoveAPIResource(int api_resource_id); | 35 bool RemoveAPIResource(int api_resource_id); |
35 | 36 |
36 // APIResourceController knows about all classes derived from APIResource. | 37 // APIResourceController knows about all classes derived from APIResource. |
37 // This is intentional to avoid scattering potentially unsafe static_cast<> | 38 // This is intentional to avoid scattering potentially unsafe static_cast<> |
38 // operations throughout the codebase. | 39 // operations throughout the codebase. |
39 // | 40 // |
40 // Another alternative we considered and rejected was templatizing | 41 // Another alternative we considered and rejected was templatizing |
41 // APIResourceController and scattering specialized versions throughout the | 42 // APIResourceController and scattering specialized versions throughout the |
42 // codebase. | 43 // codebase. |
43 Socket* GetSocket(int api_resource_id) const; | 44 Socket* GetSocket(int api_resource_id) const; |
44 SerialConnection* GetSerialConnection(int api_resource_id) const; | 45 SerialConnection* GetSerialConnection(int api_resource_id) const; |
| 46 UsbDeviceResource* GetUsbDeviceResource(int api_resource_id) const; |
45 | 47 |
46 private: | 48 private: |
47 int next_api_resource_id_; | 49 int next_api_resource_id_; |
48 typedef std::map<int, linked_ptr<APIResource> > APIResourceMap; | 50 typedef std::map<int, linked_ptr<APIResource> > APIResourceMap; |
49 APIResourceMap api_resource_map_; | 51 APIResourceMap api_resource_map_; |
50 | 52 |
51 APIResource* GetAPIResource(APIResource::APIResourceType api_resource_type, | 53 APIResource* GetAPIResource(APIResource::APIResourceType api_resource_type, |
52 int api_resource_id) const; | 54 int api_resource_id) const; |
53 APIResource* GetAPIResource(int api_resource_id) const; | 55 APIResource* GetAPIResource(int api_resource_id) const; |
54 | 56 |
55 int GenerateAPIResourceId(); | 57 int GenerateAPIResourceId(); |
56 | 58 |
57 DISALLOW_COPY_AND_ASSIGN(APIResourceController); | 59 DISALLOW_COPY_AND_ASSIGN(APIResourceController); |
58 }; | 60 }; |
59 | 61 |
60 } // namespace extensions | 62 } // namespace extensions |
61 | 63 |
62 #endif // CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_CONTROLLER_H_ | 64 #endif // CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_CONTROLLER_H_ |
OLD | NEW |