OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "base/json/json_reader.h" | 5 #include "base/json/json_reader.h" |
6 #include "base/json/json_writer.h" | 6 #include "base/json/json_writer.h" |
7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/debugger/devtools_manager.h" | 10 #include "chrome/browser/debugger/devtools_manager.h" |
11 #include "chrome/browser/debugger/devtools_protocol_handler.h" | 11 #include "chrome/browser/debugger/devtools_protocol_handler.h" |
12 #include "chrome/browser/debugger/devtools_remote_message.h" | 12 #include "chrome/browser/debugger/devtools_remote_message.h" |
13 #include "chrome/browser/debugger/devtools_remote_service.h" | 13 #include "chrome/browser/debugger/devtools_remote_service.h" |
14 #include "chrome/browser/debugger/inspectable_tab_proxy.h" | 14 #include "chrome/browser/debugger/inspectable_tab_proxy.h" |
15 #include "chrome/browser/tab_contents/navigation_controller.h" | 15 #include "chrome/browser/tab_contents/navigation_controller.h" |
16 #include "chrome/browser/tab_contents/navigation_entry.h" | 16 #include "chrome/browser/tab_contents/navigation_entry.h" |
17 #include "chrome/common/devtools_messages.h" | 17 #include "chrome/common/devtools_messages.h" |
18 | 18 |
19 const std::string DevToolsRemoteServiceCommand::kPing = "ping"; | 19 const char DevToolsRemoteServiceCommand::kPing[] = "ping"; |
20 const std::string DevToolsRemoteServiceCommand::kVersion = "version"; | 20 const char DevToolsRemoteServiceCommand::kVersion[] = "version"; |
21 const std::string DevToolsRemoteServiceCommand::kListTabs = "list_tabs"; | 21 const char DevToolsRemoteServiceCommand::kListTabs[] = "list_tabs"; |
22 | 22 |
23 const std::wstring DevToolsRemoteService::kCommandWide = L"command"; | 23 const char DevToolsRemoteService::kToolName[] = "DevToolsService"; |
24 const std::wstring DevToolsRemoteService::kDataWide = L"data"; | 24 |
25 const std::wstring DevToolsRemoteService::kResultWide = L"result"; | 25 namespace { |
26 const std::string DevToolsRemoteService::kToolName = "DevToolsService"; | 26 const char kCommandKey[] = "command"; |
| 27 const char kDataKey[] = "data"; |
| 28 const char kResultKey[] = "result"; |
| 29 } // namespace |
27 | 30 |
28 DevToolsRemoteService::DevToolsRemoteService(DevToolsProtocolHandler* delegate) | 31 DevToolsRemoteService::DevToolsRemoteService(DevToolsProtocolHandler* delegate) |
29 : delegate_(delegate) {} | 32 : delegate_(delegate) {} |
30 | 33 |
31 DevToolsRemoteService::~DevToolsRemoteService() {} | 34 DevToolsRemoteService::~DevToolsRemoteService() {} |
32 | 35 |
33 void DevToolsRemoteService::HandleMessage( | 36 void DevToolsRemoteService::HandleMessage( |
34 const DevToolsRemoteMessage& message) { | 37 const DevToolsRemoteMessage& message) { |
35 scoped_ptr<Value> request(base::JSONReader::Read(message.content(), false)); | 38 scoped_ptr<Value> request(base::JSONReader::Read(message.content(), false)); |
36 if (request.get() == NULL) { | 39 if (request.get() == NULL) { |
37 // Bad JSON | 40 // Bad JSON |
38 NOTREACHED(); | 41 NOTREACHED(); |
39 return; | 42 return; |
40 } | 43 } |
41 DictionaryValue* json; | 44 DictionaryValue* json; |
42 if (request->IsType(Value::TYPE_DICTIONARY)) { | 45 if (request->IsType(Value::TYPE_DICTIONARY)) { |
43 json = static_cast<DictionaryValue*>(request.get()); | 46 json = static_cast<DictionaryValue*>(request.get()); |
44 if (!json->HasKey(kCommandWide)) { | 47 if (!json->HasKey(kCommandKey)) { |
45 NOTREACHED(); // Broken protocol - no "command" specified | 48 NOTREACHED(); // Broken protocol - no "command" specified |
46 return; | 49 return; |
47 } | 50 } |
48 } else { | 51 } else { |
49 NOTREACHED(); // Broken protocol - not a JS object | 52 NOTREACHED(); // Broken protocol - not a JS object |
50 return; | 53 return; |
51 } | 54 } |
52 ProcessJson(json, message); | 55 ProcessJson(json, message); |
53 } | 56 } |
54 | 57 |
55 void DevToolsRemoteService::ProcessJson(DictionaryValue* json, | 58 void DevToolsRemoteService::ProcessJson(DictionaryValue* json, |
56 const DevToolsRemoteMessage& message) { | 59 const DevToolsRemoteMessage& message) { |
57 static const std::string kOkResponse = "ok"; // "Ping" response | 60 static const std::string kOkResponse = "ok"; // "Ping" response |
58 static const std::string kVersion = "0.1"; // Current protocol version | 61 static const std::string kVersion = "0.1"; // Current protocol version |
59 std::string command; | 62 std::string command; |
60 DictionaryValue response; | 63 DictionaryValue response; |
61 | 64 |
62 json->GetString(kCommandWide, &command); | 65 json->GetString(kCommandKey, &command); |
63 response.SetString(kCommandWide, command); | 66 response.SetString(kCommandKey, command); |
64 | 67 |
65 if (command == DevToolsRemoteServiceCommand::kPing) { | 68 if (command == DevToolsRemoteServiceCommand::kPing) { |
66 response.SetInteger(kResultWide, Result::kOk); | 69 response.SetInteger(kResultKey, Result::kOk); |
67 response.SetString(kDataWide, kOkResponse); | 70 response.SetString(kDataKey, kOkResponse); |
68 } else if (command == DevToolsRemoteServiceCommand::kVersion) { | 71 } else if (command == DevToolsRemoteServiceCommand::kVersion) { |
69 response.SetInteger(kResultWide, Result::kOk); | 72 response.SetInteger(kResultKey, Result::kOk); |
70 response.SetString(kDataWide, kVersion); | 73 response.SetString(kDataKey, kVersion); |
71 } else if (command == DevToolsRemoteServiceCommand::kListTabs) { | 74 } else if (command == DevToolsRemoteServiceCommand::kListTabs) { |
72 ListValue* data = new ListValue(); | 75 ListValue* data = new ListValue(); |
73 const InspectableTabProxy::ControllersMap& navcon_map = | 76 const InspectableTabProxy::ControllersMap& navcon_map = |
74 delegate_->inspectable_tab_proxy()->controllers_map(); | 77 delegate_->inspectable_tab_proxy()->controllers_map(); |
75 for (InspectableTabProxy::ControllersMap::const_iterator it = | 78 for (InspectableTabProxy::ControllersMap::const_iterator it = |
76 navcon_map.begin(), end = navcon_map.end(); it != end; ++it) { | 79 navcon_map.begin(), end = navcon_map.end(); it != end; ++it) { |
77 NavigationEntry* entry = it->second->GetActiveEntry(); | 80 NavigationEntry* entry = it->second->GetActiveEntry(); |
78 if (entry == NULL) { | 81 if (entry == NULL) { |
79 continue; | 82 continue; |
80 } | 83 } |
81 if (entry->url().is_valid()) { | 84 if (entry->url().is_valid()) { |
82 ListValue* tab = new ListValue(); | 85 ListValue* tab = new ListValue(); |
83 tab->Append(Value::CreateIntegerValue( | 86 tab->Append(Value::CreateIntegerValue( |
84 static_cast<int32>(it->second->session_id().id()))); | 87 static_cast<int32>(it->second->session_id().id()))); |
85 tab->Append(Value::CreateStringValue(entry->url().spec())); | 88 tab->Append(Value::CreateStringValue(entry->url().spec())); |
86 data->Append(tab); | 89 data->Append(tab); |
87 } | 90 } |
88 } | 91 } |
89 response.SetInteger(kResultWide, Result::kOk); | 92 response.SetInteger(kResultKey, Result::kOk); |
90 response.Set(kDataWide, data); | 93 response.Set(kDataKey, data); |
91 } else { | 94 } else { |
92 // Unknown protocol command. | 95 // Unknown protocol command. |
93 NOTREACHED(); | 96 NOTREACHED(); |
94 response.SetInteger(kResultWide, Result::kUnknownCommand); | 97 response.SetInteger(kResultKey, Result::kUnknownCommand); |
95 } | 98 } |
96 std::string response_json; | 99 std::string response_json; |
97 base::JSONWriter::Write(&response, false, &response_json); | 100 base::JSONWriter::Write(&response, false, &response_json); |
98 scoped_ptr<DevToolsRemoteMessage> response_message( | 101 scoped_ptr<DevToolsRemoteMessage> response_message( |
99 DevToolsRemoteMessageBuilder::instance().Create(message.tool(), | 102 DevToolsRemoteMessageBuilder::instance().Create(message.tool(), |
100 message.destination(), | 103 message.destination(), |
101 response_json)); | 104 response_json)); |
102 delegate_->Send(*response_message.get()); | 105 delegate_->Send(*response_message.get()); |
103 } | 106 } |
OLD | NEW |