Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_GCD_PRIVATE_GCD_PRIVATE_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_GCD_PRIVATE_GCD_PRIVATE_API_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_GCD_PRIVATE_GCD_PRIVATE_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_GCD_PRIVATE_GCD_PRIVATE_API_H_ |
| 7 | 7 |
| 8 #include "base/memory/linked_ptr.h" | 8 #include "base/memory/linked_ptr.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "chrome/browser/extensions/chrome_extension_function.h" | 10 #include "chrome/browser/extensions/chrome_extension_function.h" |
| 11 #include "chrome/browser/local_discovery/cloud_device_list_delegate.h" | 11 #include "chrome/browser/local_discovery/cloud_device_list_delegate.h" |
| 12 #include "chrome/browser/local_discovery/gcd_api_flow.h" | 12 #include "chrome/browser/local_discovery/gcd_api_flow.h" |
| 13 #include "chrome/browser/local_discovery/privet_device_lister.h" | 13 #include "chrome/browser/local_discovery/privet_device_lister.h" |
| 14 #include "chrome/browser/local_discovery/privetv3_session.h" | |
| 14 #include "chrome/browser/local_discovery/service_discovery_shared_client.h" | 15 #include "chrome/browser/local_discovery/service_discovery_shared_client.h" |
| 15 #include "chrome/common/extensions/api/gcd_private.h" | 16 #include "chrome/common/extensions/api/gcd_private.h" |
| 16 #include "extensions/browser/browser_context_keyed_api_factory.h" | 17 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 17 #include "extensions/browser/event_router.h" | 18 #include "extensions/browser/event_router.h" |
| 18 | 19 |
| 19 namespace extensions { | 20 namespace extensions { |
| 20 | 21 |
| 22 class GcdPrivateSessionHolder; | |
| 23 | |
| 21 class GcdPrivateAPI : public BrowserContextKeyedAPI, | 24 class GcdPrivateAPI : public BrowserContextKeyedAPI, |
| 22 public EventRouter::Observer, | 25 public EventRouter::Observer, |
| 23 public local_discovery::PrivetDeviceLister::Delegate { | 26 public local_discovery::PrivetDeviceLister::Delegate { |
| 24 public: | 27 public: |
| 28 typedef base::Callback<void(int session_id, | |
| 29 api::gcd_private::Status status, | |
| 30 const std::string& code, | |
| 31 api::gcd_private::ConfirmationType type)> | |
| 32 ConfirmationCodeCallback; | |
| 33 | |
| 34 typedef base::Callback<void(api::gcd_private::Status status)> | |
| 35 SessionEstablishedCallback; | |
| 36 | |
| 37 typedef base::Callback<void(api::gcd_private::Status status, | |
| 38 const base::DictionaryValue& response)> | |
| 39 MessageResponseCallback; | |
| 40 | |
| 25 class GCDApiFlowFactoryForTests { | 41 class GCDApiFlowFactoryForTests { |
| 26 public: | 42 public: |
| 27 virtual ~GCDApiFlowFactoryForTests() {} | 43 virtual ~GCDApiFlowFactoryForTests() {} |
| 28 | 44 |
| 29 virtual scoped_ptr<local_discovery::GCDApiFlow> CreateGCDApiFlow() = 0; | 45 virtual scoped_ptr<local_discovery::GCDApiFlow> CreateGCDApiFlow() = 0; |
| 30 }; | 46 }; |
| 31 | 47 |
| 32 explicit GcdPrivateAPI(content::BrowserContext* context); | 48 explicit GcdPrivateAPI(content::BrowserContext* context); |
| 33 virtual ~GcdPrivateAPI(); | 49 virtual ~GcdPrivateAPI(); |
| 34 | 50 |
| 35 static void SetGCDApiFlowFactoryForTests(GCDApiFlowFactoryForTests* factory); | 51 static void SetGCDApiFlowFactoryForTests(GCDApiFlowFactoryForTests* factory); |
| 36 | 52 |
| 37 // BrowserContextKeyedAPI implementation. | 53 // BrowserContextKeyedAPI implementation. |
| 38 static BrowserContextKeyedAPIFactory<GcdPrivateAPI>* GetFactoryInstance(); | 54 static BrowserContextKeyedAPIFactory<GcdPrivateAPI>* GetFactoryInstance(); |
| 39 | 55 |
| 40 bool QueryForDevices(); | 56 bool QueryForDevices(); |
| 41 | 57 |
| 58 void EstablishSession(std::string ip_address, | |
| 59 int port, | |
| 60 ConfirmationCodeCallback callback); | |
| 61 | |
| 62 void ConfirmCode(int session_id, SessionEstablishedCallback callback); | |
| 63 | |
| 64 void SendMessage(int session_id, | |
| 65 const std::string& api, | |
| 66 const base::DictionaryValue& input, | |
| 67 MessageResponseCallback callback); | |
| 68 | |
| 42 private: | 69 private: |
| 43 friend class BrowserContextKeyedAPIFactory<GcdPrivateAPI>; | 70 friend class BrowserContextKeyedAPIFactory<GcdPrivateAPI>; |
| 44 | 71 |
| 45 typedef std::map<std::string /* id_string */, | 72 typedef std::map<std::string /* id_string */, |
| 46 linked_ptr<api::gcd_private::GCDDevice> > GCDDeviceMap; | 73 linked_ptr<api::gcd_private::GCDDevice> > GCDDeviceMap; |
| 47 | 74 |
| 75 typedef std::map<int /*session_id*/, linked_ptr<GcdPrivateSessionHolder> > | |
|
Vitaly Buka (NO REVIEWS)
2014/07/24 20:50:18
You don't need map, ScopedVector<> should be enoug
Noam Samuel
2014/07/24 21:11:22
Not sure what the benefit of using a vector over a
Noam Samuel
2014/07/24 21:55:45
Replaced std::vector< linked_ptr< >> with ScopedVe
| |
| 76 GCDSessionMap; | |
| 77 | |
| 48 // EventRouter::Observer implementation. | 78 // EventRouter::Observer implementation. |
| 49 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE; | 79 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE; |
| 50 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE; | 80 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE; |
| 51 | 81 |
| 52 // BrowserContextKeyedAPI implementation. | 82 // BrowserContextKeyedAPI implementation. |
| 53 static const char* service_name() { return "GcdPrivateAPI"; } | 83 static const char* service_name() { return "GcdPrivateAPI"; } |
| 54 | 84 |
| 55 // local_discovery::PrivetDeviceLister implementation. | 85 // local_discovery::PrivetDeviceLister implementation. |
| 56 virtual void DeviceChanged( | 86 virtual void DeviceChanged( |
| 57 bool added, | 87 bool added, |
| 58 const std::string& name, | 88 const std::string& name, |
| 59 const local_discovery::DeviceDescription& description) OVERRIDE; | 89 const local_discovery::DeviceDescription& description) OVERRIDE; |
| 60 virtual void DeviceRemoved(const std::string& name) OVERRIDE; | 90 virtual void DeviceRemoved(const std::string& name) OVERRIDE; |
| 61 virtual void DeviceCacheFlushed() OVERRIDE; | 91 virtual void DeviceCacheFlushed() OVERRIDE; |
| 62 | 92 |
| 63 int num_device_listeners_; | 93 int num_device_listeners_; |
| 64 scoped_refptr<local_discovery::ServiceDiscoverySharedClient> | 94 scoped_refptr<local_discovery::ServiceDiscoverySharedClient> |
| 65 service_discovery_client_; | 95 service_discovery_client_; |
| 66 scoped_ptr<local_discovery::PrivetDeviceLister> privet_device_lister_; | 96 scoped_ptr<local_discovery::PrivetDeviceLister> privet_device_lister_; |
| 67 GCDDeviceMap known_devices_; | 97 GCDDeviceMap known_devices_; |
| 68 | 98 |
| 99 GCDSessionMap sessions_; | |
| 100 int last_session_id_; | |
| 101 | |
| 69 content::BrowserContext* const browser_context_; | 102 content::BrowserContext* const browser_context_; |
| 70 }; | 103 }; |
| 71 | 104 |
| 105 class GcdPrivateRequest : public local_discovery::PrivetV3Session::Request { | |
|
Vitaly Buka (NO REVIEWS)
2014/07/24 20:50:18
this should be defined in cc file
Noam Samuel
2014/07/24 21:55:45
Done.
| |
| 106 public: | |
| 107 GcdPrivateRequest(const std::string& api, | |
| 108 const base::DictionaryValue& input, | |
| 109 const GcdPrivateAPI::MessageResponseCallback& callback, | |
| 110 GcdPrivateSessionHolder* session_holder); | |
| 111 virtual ~GcdPrivateRequest(); | |
| 112 | |
| 113 // local_discovery::PrivetV3Session::Request implementation. | |
| 114 virtual std::string GetName() OVERRIDE; | |
| 115 virtual const base::DictionaryValue& GetInput() OVERRIDE; | |
| 116 virtual void OnError( | |
| 117 local_discovery::PrivetURLFetcher::ErrorType error) OVERRIDE; | |
| 118 virtual void OnParsedJson(const base::DictionaryValue& value, | |
| 119 bool has_error) OVERRIDE; | |
| 120 | |
| 121 private: | |
| 122 std::string api_; | |
| 123 scoped_ptr<base::DictionaryValue> input_; | |
| 124 GcdPrivateAPI::MessageResponseCallback callback_; | |
| 125 GcdPrivateSessionHolder* session_holder_; | |
| 126 }; | |
| 127 | |
| 128 class GcdPrivateSessionHolder | |
|
Vitaly Buka (NO REVIEWS)
2014/07/24 20:50:19
GcdPrivateSessionHolder also could be hidden in CC
Noam Samuel
2014/07/24 21:55:45
Done.
| |
| 129 : public local_discovery::PrivetV3Session::Delegate { | |
| 130 public: | |
| 131 typedef base::Callback<void(api::gcd_private::Status status, | |
| 132 const std::string& code, | |
| 133 api::gcd_private::ConfirmationType type)> | |
| 134 ConfirmationCodeCallback; | |
| 135 | |
| 136 GcdPrivateSessionHolder(const std::string& ip_address, | |
| 137 int port, | |
| 138 net::URLRequestContextGetter* request_context); | |
| 139 virtual ~GcdPrivateSessionHolder(); | |
| 140 | |
| 141 void Start(const ConfirmationCodeCallback& callback); | |
| 142 | |
| 143 void ConfirmCode(const GcdPrivateAPI::SessionEstablishedCallback& callback); | |
| 144 | |
| 145 void SendMessage(const std::string& api, | |
| 146 const base::DictionaryValue& input, | |
| 147 GcdPrivateAPI::MessageResponseCallback callback); | |
| 148 | |
| 149 void DeleteRequest(GcdPrivateRequest* request); | |
| 150 | |
| 151 private: | |
| 152 // local_discovery::PrivetV3Session::Delegate implementation. | |
| 153 virtual void OnSetupConfirmationNeeded( | |
| 154 const std::string& confirmation_code) OVERRIDE; | |
| 155 virtual void OnSessionEstablished() OVERRIDE; | |
| 156 virtual void OnCannotEstablishSession() OVERRIDE; | |
| 157 | |
| 158 scoped_ptr<local_discovery::PrivetHTTPClient> http_client_; | |
| 159 scoped_ptr<local_discovery::PrivetV3Session> privet_session_; | |
| 160 typedef std::vector<linked_ptr<GcdPrivateRequest> > RequestVector; | |
| 161 RequestVector requests_; | |
| 162 | |
| 163 ConfirmationCodeCallback confirm_callback_; | |
| 164 GcdPrivateAPI::SessionEstablishedCallback session_established_callback_; | |
| 165 }; | |
| 166 | |
| 72 class GcdPrivateGetCloudDeviceListFunction | 167 class GcdPrivateGetCloudDeviceListFunction |
| 73 : public ChromeAsyncExtensionFunction, | 168 : public ChromeAsyncExtensionFunction, |
| 74 public local_discovery::CloudDeviceListDelegate { | 169 public local_discovery::CloudDeviceListDelegate { |
| 75 public: | 170 public: |
| 76 DECLARE_EXTENSION_FUNCTION("gcdPrivate.getCloudDeviceList", | 171 DECLARE_EXTENSION_FUNCTION("gcdPrivate.getCloudDeviceList", |
| 77 GCDPRIVATE_GETCLOUDDEVICELIST) | 172 GCDPRIVATE_GETCLOUDDEVICELIST) |
| 78 | 173 |
| 79 GcdPrivateGetCloudDeviceListFunction(); | 174 GcdPrivateGetCloudDeviceListFunction(); |
| 80 | 175 |
| 81 protected: | 176 protected: |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 DECLARE_EXTENSION_FUNCTION("gcdPrivate.establishSession", | 229 DECLARE_EXTENSION_FUNCTION("gcdPrivate.establishSession", |
| 135 GCDPRIVATE_ESTABLISHSESSION) | 230 GCDPRIVATE_ESTABLISHSESSION) |
| 136 | 231 |
| 137 GcdPrivateEstablishSessionFunction(); | 232 GcdPrivateEstablishSessionFunction(); |
| 138 | 233 |
| 139 protected: | 234 protected: |
| 140 virtual ~GcdPrivateEstablishSessionFunction(); | 235 virtual ~GcdPrivateEstablishSessionFunction(); |
| 141 | 236 |
| 142 // AsyncExtensionFunction overrides. | 237 // AsyncExtensionFunction overrides. |
| 143 virtual bool RunAsync() OVERRIDE; | 238 virtual bool RunAsync() OVERRIDE; |
| 239 | |
| 240 private: | |
| 241 void OnConfirmCodeCallback( | |
| 242 int session_id, | |
| 243 api::gcd_private::Status status, | |
| 244 const std::string& confirm_code, | |
| 245 api::gcd_private::ConfirmationType confirmation_type); | |
| 144 }; | 246 }; |
| 145 | 247 |
| 146 class GcdPrivateConfirmCodeFunction : public ChromeAsyncExtensionFunction { | 248 class GcdPrivateConfirmCodeFunction : public ChromeAsyncExtensionFunction { |
| 147 public: | 249 public: |
| 148 DECLARE_EXTENSION_FUNCTION("gcdPrivate.confirmCode", GCDPRIVATE_CONFIRMCODE) | 250 DECLARE_EXTENSION_FUNCTION("gcdPrivate.confirmCode", GCDPRIVATE_CONFIRMCODE) |
| 149 | 251 |
| 150 GcdPrivateConfirmCodeFunction(); | 252 GcdPrivateConfirmCodeFunction(); |
| 151 | 253 |
| 152 protected: | 254 protected: |
| 153 virtual ~GcdPrivateConfirmCodeFunction(); | 255 virtual ~GcdPrivateConfirmCodeFunction(); |
| 154 | 256 |
| 155 // AsyncExtensionFunction overrides. | 257 // AsyncExtensionFunction overrides. |
| 156 virtual bool RunAsync() OVERRIDE; | 258 virtual bool RunAsync() OVERRIDE; |
| 157 | 259 |
| 158 private: | 260 private: |
| 261 void OnSessionEstablishedCallback(api::gcd_private::Status status); | |
| 159 }; | 262 }; |
| 160 | 263 |
| 161 class GcdPrivateSendMessageFunction : public ChromeAsyncExtensionFunction { | 264 class GcdPrivateSendMessageFunction : public ChromeAsyncExtensionFunction { |
| 162 public: | 265 public: |
| 163 DECLARE_EXTENSION_FUNCTION("gcdPrivate.sendMessage", GCDPRIVATE_SENDMESSAGE) | 266 DECLARE_EXTENSION_FUNCTION("gcdPrivate.sendMessage", GCDPRIVATE_SENDMESSAGE) |
| 164 | 267 |
| 165 GcdPrivateSendMessageFunction(); | 268 GcdPrivateSendMessageFunction(); |
| 166 | 269 |
| 167 protected: | 270 protected: |
| 168 virtual ~GcdPrivateSendMessageFunction(); | 271 virtual ~GcdPrivateSendMessageFunction(); |
| 169 | 272 |
| 170 // AsyncExtensionFunction overrides. | 273 // AsyncExtensionFunction overrides. |
| 171 virtual bool RunAsync() OVERRIDE; | 274 virtual bool RunAsync() OVERRIDE; |
| 172 | 275 |
| 173 private: | 276 private: |
| 277 void OnMessageSentCallback(api::gcd_private::Status status, | |
| 278 const base::DictionaryValue& value); | |
| 174 }; | 279 }; |
| 175 | 280 |
| 176 class GcdPrivateTerminateSessionFunction : public ChromeAsyncExtensionFunction { | 281 class GcdPrivateTerminateSessionFunction : public ChromeAsyncExtensionFunction { |
| 177 public: | 282 public: |
| 178 DECLARE_EXTENSION_FUNCTION("gcdPrivate.terminateSession", | 283 DECLARE_EXTENSION_FUNCTION("gcdPrivate.terminateSession", |
| 179 GCDPRIVATE_TERMINATESESSION) | 284 GCDPRIVATE_TERMINATESESSION) |
| 180 | 285 |
| 181 GcdPrivateTerminateSessionFunction(); | 286 GcdPrivateTerminateSessionFunction(); |
| 182 | 287 |
| 183 protected: | 288 protected: |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 263 | 368 |
| 264 // AsyncExtensionFunction overrides. | 369 // AsyncExtensionFunction overrides. |
| 265 virtual bool RunAsync() OVERRIDE; | 370 virtual bool RunAsync() OVERRIDE; |
| 266 | 371 |
| 267 private: | 372 private: |
| 268 }; | 373 }; |
| 269 | 374 |
| 270 } // namespace extensions | 375 } // namespace extensions |
| 271 | 376 |
| 272 #endif // CHROME_BROWSER_EXTENSIONS_API_GCD_PRIVATE_GCD_PRIVATE_API_H_ | 377 #endif // CHROME_BROWSER_EXTENSIONS_API_GCD_PRIVATE_GCD_PRIVATE_API_H_ |
| OLD | NEW |