| 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 CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_BROWSER_TARGET_H_ | 5 #ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_BROWSER_TARGET_H_ |
| 6 #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_BROWSER_TARGET_H_ | 6 #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_BROWSER_TARGET_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" |
| 12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" |
| 14 | 16 |
| 15 namespace base { | 17 namespace base { |
| 16 | 18 |
| 17 class DictionaryValue; | 19 class DictionaryValue; |
| 18 class MessageLoopProxy; | 20 class MessageLoopProxy; |
| 19 class Value; | 21 class Value; |
| 20 | 22 |
| 21 } // namespace base | 23 } // namespace base |
| 22 | 24 |
| 23 namespace net { | 25 namespace net { |
| 24 class HttpServer; | 26 class HttpServer; |
| 25 } // namespace net | 27 } // namespace net |
| 26 | 28 |
| 27 | |
| 28 namespace content { | 29 namespace content { |
| 29 | 30 |
| 30 // This class handles the "Browser" target for remote debugging. | 31 // This class handles the "Browser" target for remote debugging. |
| 31 class DevToolsBrowserTarget { | 32 class DevToolsBrowserTarget { |
| 32 public: | 33 public: |
| 33 // A thin interface to send notifications over WebSocket. | 34 typedef base::Callback<void(const std::string& method, |
| 34 class Notifier { | 35 base::DictionaryValue* params, |
| 36 base::Value* error)> Notifier; |
| 37 |
| 38 class DomainHandler { |
| 35 public: | 39 public: |
| 36 virtual ~Notifier() {} | 40 typedef base::Callback<base::DictionaryValue*( |
| 41 const base::DictionaryValue* params, |
| 42 base::Value** error_out)> CommandHandler; |
| 43 virtual ~DomainHandler(); |
| 37 | 44 |
| 38 virtual void Notify(const std::string& data) = 0; | 45 // Returns the domain name for this handler. |
| 46 std::string domain() { return domain_; } |
| 47 |
| 48 void RegisterCommandHandler(const std::string& command, |
| 49 CommandHandler handler); |
| 39 | 50 |
| 40 protected: | 51 protected: |
| 41 Notifier() {} | 52 explicit DomainHandler(const std::string& domain); |
| 53 |
| 54 // |params| and |error_out| ownership is transferred to the |
| 55 // caller. |
| 56 virtual base::DictionaryValue* HandleCommand( |
| 57 const std::string& method, |
| 58 const base::DictionaryValue* params, |
| 59 base::Value** error_out); |
| 60 |
| 61 // Sends notification to the client. Passes ownership of |params| and |
| 62 // |error|. |
| 63 void SendNotification(const std::string& method, |
| 64 base::DictionaryValue* params, |
| 65 base::Value* error); |
| 42 | 66 |
| 43 private: | 67 private: |
| 44 DISALLOW_COPY_AND_ASSIGN(Notifier); | 68 friend class DevToolsBrowserTarget; |
| 69 void set_notifier(Notifier notifier) { notifier_ = notifier; } |
| 70 |
| 71 std::string domain_; |
| 72 Notifier notifier_; |
| 73 typedef std::map<std::string, CommandHandler> CommandHandlers; |
| 74 CommandHandlers command_handlers_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(DomainHandler); |
| 45 }; | 77 }; |
| 46 | 78 |
| 47 class Handler { | |
| 48 public: | |
| 49 virtual ~Handler(); | |
| 50 | |
| 51 // Returns the domain name for this handler. | |
| 52 virtual std::string Domain() = 0; | |
| 53 | |
| 54 // |return_value| and |error_message_out| ownership is transferred to the | |
| 55 // caller. | |
| 56 virtual base::Value* OnProtocolCommand( | |
| 57 const std::string& method, | |
| 58 const base::DictionaryValue* params, | |
| 59 base::Value** error_message_out) = 0; | |
| 60 | |
| 61 void set_notifier(Notifier* notifier); | |
| 62 Notifier* notifier() const; | |
| 63 | |
| 64 protected: | |
| 65 Handler(); | |
| 66 | |
| 67 private: | |
| 68 Notifier* notifier_; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(Handler); | |
| 71 }; | |
| 72 | |
| 73 | |
| 74 | |
| 75 DevToolsBrowserTarget(base::MessageLoopProxy* message_loop_proxy, | 79 DevToolsBrowserTarget(base::MessageLoopProxy* message_loop_proxy, |
| 76 net::HttpServer* server, | 80 net::HttpServer* server, |
| 77 int connection_id); | 81 int connection_id); |
| 78 ~DevToolsBrowserTarget(); | 82 ~DevToolsBrowserTarget(); |
| 79 | 83 |
| 80 int connection_id() const { return connection_id_; } | 84 int connection_id() const { return connection_id_; } |
| 81 | 85 |
| 82 // Takes ownership of |handler|. | 86 // Takes ownership of |handler|. |
| 83 void RegisterHandler(Handler* handler); | 87 void RegisterDomainHandler(DomainHandler* handler); |
| 84 | 88 |
| 85 std::string HandleMessage(const std::string& data); | 89 std::string HandleMessage(const std::string& data); |
| 86 | 90 |
| 87 private: | 91 private: |
| 88 scoped_ptr<Notifier> notifier_; | 92 // Sends notification to the client. Passes ownership of |params| and |
| 89 const int connection_id_; | 93 // |error|. |
| 90 | 94 void SendNotification(const std::string& method, |
| 91 typedef std::map<std::string, Handler*> HandlersMap; | 95 base::DictionaryValue* params, |
| 92 HandlersMap handlers_; | 96 base::Value* error); |
| 93 | 97 |
| 94 // Takes ownership of |error_object|. | 98 // Takes ownership of |error_object|. |
| 95 std::string SerializeErrorResponse(int request_id, base::Value* error_object); | 99 std::string SerializeErrorResponse(int request_id, base::Value* error_object); |
| 96 | 100 |
| 97 base::Value* CreateErrorObject(int error_code, const std::string& message); | 101 std::string SerializeResponse(int request_id, base::DictionaryValue* result); |
| 98 | 102 |
| 99 std::string SerializeResponse(int request_id, base::Value* response); | 103 base::MessageLoopProxy* const message_loop_proxy_; |
| 104 net::HttpServer* const http_server_; |
| 105 const int connection_id_; |
| 106 |
| 107 typedef std::map<std::string, DomainHandler*> DomainHandlerMap; |
| 108 DomainHandlerMap handlers_; |
| 109 base::WeakPtrFactory<DevToolsBrowserTarget> weak_factory_; |
| 100 | 110 |
| 101 DISALLOW_COPY_AND_ASSIGN(DevToolsBrowserTarget); | 111 DISALLOW_COPY_AND_ASSIGN(DevToolsBrowserTarget); |
| 102 }; | 112 }; |
| 103 | 113 |
| 104 } // namespace content | 114 } // namespace content |
| 105 | 115 |
| 106 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_BROWSER_TARGET_H_ | 116 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_BROWSER_TARGET_H_ |
| OLD | NEW |