| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_CONTROLLER_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_CONTROLLER_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <map> | |
| 10 | |
| 11 #include "base/memory/linked_ptr.h" | |
| 12 #include "chrome/browser/extensions/api/api_resource.h" | |
| 13 | |
| 14 namespace extensions { | |
| 15 | |
| 16 class SerialConnection; | |
| 17 class Socket; | |
| 18 class UsbDeviceResource; | |
| 19 | |
| 20 // 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 // schema_generated_bindings.js; the application code is unaware of it. | |
| 23 extern const char kSrcIdKey[]; | |
| 24 | |
| 25 // APIController keeps track of a collection of APIResources and provides a | |
| 26 // convenient set of methods to work with them. | |
| 27 class APIResourceController { | |
| 28 public: | |
| 29 APIResourceController(); | |
| 30 virtual ~APIResourceController(); | |
| 31 | |
| 32 // Takes ownership of api_resource. | |
| 33 int AddAPIResource(APIResource* api_resource); | |
| 34 | |
| 35 // APIResourceController knows about all classes derived from APIResource. | |
| 36 // This is intentional to avoid scattering potentially unsafe static_cast<> | |
| 37 // operations throughout the codebase. | |
| 38 // | |
| 39 // Another alternative we considered and rejected was templatizing | |
| 40 // APIResourceController and scattering specialized versions throughout the | |
| 41 // codebase. | |
| 42 | |
| 43 bool RemoveSocket(int api_resource_id); | |
| 44 bool RemoveSerialConnection(int api_resource_id); | |
| 45 bool RemoveUsbDeviceResource(int api_resource_id); | |
| 46 | |
| 47 Socket* GetSocket(int api_resource_id) const; | |
| 48 SerialConnection* GetSerialConnection(int api_resource_id) const; | |
| 49 UsbDeviceResource* GetUsbDeviceResource(int api_resource_id) const; | |
| 50 | |
| 51 private: | |
| 52 typedef std::map<int, linked_ptr<APIResource> > APIResourceMap; | |
| 53 | |
| 54 APIResource* GetAPIResource(APIResource::APIResourceType api_resource_type, | |
| 55 int api_resource_id) const; | |
| 56 bool RemoveAPIResource(APIResource::APIResourceType api_resource_type, | |
| 57 int api_resource_id); | |
| 58 | |
| 59 int GenerateAPIResourceId(); | |
| 60 | |
| 61 APIResourceMap* GetResourceMapForType( | |
| 62 APIResource::APIResourceType api_resource_type) const; | |
| 63 | |
| 64 int next_api_resource_id_; | |
| 65 | |
| 66 // We need finer-grained control over the lifetime of these instances | |
| 67 // than RAII can give us. | |
| 68 APIResourceMap* socket_resource_map_; | |
| 69 APIResourceMap* serial_connection_resource_map_; | |
| 70 APIResourceMap* usb_device_resource_map_; | |
| 71 | |
| 72 DISALLOW_COPY_AND_ASSIGN(APIResourceController); | |
| 73 }; | |
| 74 | |
| 75 } // namespace extensions | |
| 76 | |
| 77 #endif // CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_CONTROLLER_H_ | |
| OLD | NEW |