Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: chrome/browser/extensions/api/debugger/debugger_api.cc

Issue 10803043: Refactor chrome.debugger api to use the JSON schema compiler. Also modified the (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/common/extensions/api/api.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Implements the Chrome Extensions Debugger API. 5 // Implements the Chrome Extensions Debugger API.
6 6
7 #include "chrome/browser/extensions/api/debugger/debugger_api.h" 7 #include "chrome/browser/extensions/api/debugger/debugger_api.h"
8 8
9 #include <map> 9 #include <map>
10 #include <set> 10 #include <set>
11 11
12 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
13 #include "base/json/json_writer.h" 13 #include "base/json/json_writer.h"
14 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/singleton.h" 15 #include "base/memory/singleton.h"
15 #include "base/string_number_conversions.h" 16 #include "base/string_number_conversions.h"
16 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
17 #include "base/values.h" 18 #include "base/values.h"
18 #include "chrome/browser/extensions/api/debugger/debugger_api_constants.h" 19 #include "chrome/browser/extensions/api/debugger/debugger_api_constants.h"
Matt Tytel 2012/07/20 00:41:15 After we use the json schema compiler for events,
19 #include "chrome/browser/extensions/extension_event_router.h" 20 #include "chrome/browser/extensions/extension_event_router.h"
20 #include "chrome/browser/extensions/extension_tab_util.h" 21 #include "chrome/browser/extensions/extension_tab_util.h"
21 #include "chrome/browser/infobars/infobar.h" 22 #include "chrome/browser/infobars/infobar.h"
22 #include "chrome/browser/infobars/infobar_tab_helper.h" 23 #include "chrome/browser/infobars/infobar_tab_helper.h"
23 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" 25 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h"
25 #include "chrome/browser/ui/tab_contents/tab_contents.h" 26 #include "chrome/browser/ui/tab_contents/tab_contents.h"
26 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" 27 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
27 #include "chrome/common/chrome_notification_types.h" 28 #include "chrome/common/chrome_notification_types.h"
29 #include "chrome/common/extensions/api/debugger.h"
28 #include "chrome/common/extensions/extension.h" 30 #include "chrome/common/extensions/extension.h"
29 #include "chrome/common/extensions/extension_error_utils.h" 31 #include "chrome/common/extensions/extension_error_utils.h"
30 #include "content/public/browser/devtools_agent_host_registry.h" 32 #include "content/public/browser/devtools_agent_host_registry.h"
31 #include "content/public/browser/devtools_client_host.h" 33 #include "content/public/browser/devtools_client_host.h"
32 #include "content/public/browser/devtools_manager.h" 34 #include "content/public/browser/devtools_manager.h"
33 #include "content/public/browser/notification_service.h" 35 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/notification_source.h" 36 #include "content/public/browser/notification_source.h"
35 #include "content/public/browser/render_view_host.h" 37 #include "content/public/browser/render_view_host.h"
36 #include "content/public/browser/web_contents.h" 38 #include "content/public/browser/web_contents.h"
37 #include "content/public/common/content_client.h" 39 #include "content/public/common/content_client.h"
38 #include "grit/generated_resources.h" 40 #include "grit/generated_resources.h"
39 #include "ui/base/l10n/l10n_util.h" 41 #include "ui/base/l10n/l10n_util.h"
40 #include "webkit/glue/webkit_glue.h" 42 #include "webkit/glue/webkit_glue.h"
41 43
42 using content::DevToolsAgentHost; 44 using content::DevToolsAgentHost;
43 using content::DevToolsAgentHostRegistry; 45 using content::DevToolsAgentHostRegistry;
44 using content::DevToolsClientHost; 46 using content::DevToolsClientHost;
45 using content::DevToolsManager; 47 using content::DevToolsManager;
46 using content::WebContents; 48 using content::WebContents;
47 49
48 namespace keys = debugger_api_constants; 50 namespace keys = debugger_api_constants;
51 namespace Attach = extensions::api::debugger::Attach;
52 namespace Detach = extensions::api::debugger::Detach;
53 namespace SendCommand = extensions::api::debugger::SendCommand;
49 54
50 class ExtensionDevToolsInfoBarDelegate : public ConfirmInfoBarDelegate { 55 class ExtensionDevToolsInfoBarDelegate : public ConfirmInfoBarDelegate {
51 public: 56 public:
52 ExtensionDevToolsInfoBarDelegate( 57 ExtensionDevToolsInfoBarDelegate(
53 InfoBarTabHelper* infobar_helper, 58 InfoBarTabHelper* infobar_helper,
54 const std::string& client_name); 59 const std::string& client_name);
55 virtual ~ExtensionDevToolsInfoBarDelegate(); 60 virtual ~ExtensionDevToolsInfoBarDelegate();
56 61
57 private: 62 private:
58 // ConfirmInfoBarDelegate: 63 // ConfirmInfoBarDelegate:
(...skipping 15 matching lines...) Expand all
74 const std::string& extension_name, 79 const std::string& extension_name,
75 int tab_id); 80 int tab_id);
76 81
77 ~ExtensionDevToolsClientHost(); 82 ~ExtensionDevToolsClientHost();
78 83
79 bool MatchesContentsAndExtensionId(WebContents* web_contents, 84 bool MatchesContentsAndExtensionId(WebContents* web_contents,
80 const std::string& extension_id); 85 const std::string& extension_id);
81 void Close(); 86 void Close();
82 void SendMessageToBackend(SendCommandDebuggerFunction* function, 87 void SendMessageToBackend(SendCommandDebuggerFunction* function,
83 const std::string& method, 88 const std::string& method,
84 Value* params); 89 SendCommand::Params::CommandParams* params);
85 90
86 // DevToolsClientHost interface 91 // DevToolsClientHost interface
87 virtual void InspectedContentsClosing() OVERRIDE; 92 virtual void InspectedContentsClosing() OVERRIDE;
88 virtual void DispatchOnInspectorFrontend(const std::string& message) OVERRIDE; 93 virtual void DispatchOnInspectorFrontend(const std::string& message) OVERRIDE;
89 virtual void ContentsReplaced(WebContents* web_contents) OVERRIDE; 94 virtual void ContentsReplaced(WebContents* web_contents) OVERRIDE;
90 virtual void FrameNavigating(const std::string& url) OVERRIDE {} 95 virtual void FrameNavigating(const std::string& url) OVERRIDE {}
91 96
92 private: 97 private:
93 void SendDetachedEvent(); 98 void SendDetachedEvent();
94 99
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 } 227 }
223 228
224 void ExtensionDevToolsClientHost::Close() { 229 void ExtensionDevToolsClientHost::Close() {
225 DevToolsManager::GetInstance()->ClientHostClosing(this); 230 DevToolsManager::GetInstance()->ClientHostClosing(this);
226 delete this; 231 delete this;
227 } 232 }
228 233
229 void ExtensionDevToolsClientHost::SendMessageToBackend( 234 void ExtensionDevToolsClientHost::SendMessageToBackend(
230 SendCommandDebuggerFunction* function, 235 SendCommandDebuggerFunction* function,
231 const std::string& method, 236 const std::string& method,
232 Value* params) { 237 SendCommand::Params::CommandParams* params) {
233 DictionaryValue protocol_request; 238 DictionaryValue protocol_request;
234 int request_id = ++last_request_id_; 239 int request_id = ++last_request_id_;
235 pending_requests_[request_id] = function; 240 pending_requests_[request_id] = function;
236 protocol_request.SetInteger("id", request_id); 241 protocol_request.SetInteger("id", request_id);
237 protocol_request.SetString("method", method); 242 protocol_request.SetString("method", method);
238 if (params) 243 if (params)
not at google - send to devlin 2012/07/19 23:53:47 params can't be NULL, it's validated to not be, so
mitchellwrosen 2012/07/20 19:05:17 Params is optional, so this pointer can be NULL
Matt Tytel 2012/07/20 19:09:57 Maybe rename params to command_params here and oth
not at google - send to devlin 2012/07/23 13:56:23 Yep, I misread that. Thanks.
239 protocol_request.Set("params", params->DeepCopy()); 244 protocol_request.Set("params", params->additional_properties.DeepCopy());
240 245
241 std::string json_args; 246 std::string json_args;
242 base::JSONWriter::Write(&protocol_request, &json_args); 247 base::JSONWriter::Write(&protocol_request, &json_args);
243 DevToolsManager::GetInstance()->DispatchOnInspectorBackend(this, json_args); 248 DevToolsManager::GetInstance()->DispatchOnInspectorBackend(this, json_args);
244 } 249 }
245 250
246 void ExtensionDevToolsClientHost::SendDetachedEvent() { 251 void ExtensionDevToolsClientHost::SendDetachedEvent() {
247 Profile* profile = 252 Profile* profile =
248 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 253 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
249 if (profile != NULL && profile->GetExtensionEventRouter()) { 254 if (profile != NULL && profile->GetExtensionEventRouter()) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 if (!result->IsType(Value::TYPE_DICTIONARY)) 295 if (!result->IsType(Value::TYPE_DICTIONARY))
291 return; 296 return;
292 DictionaryValue* dictionary = static_cast<DictionaryValue*>(result.get()); 297 DictionaryValue* dictionary = static_cast<DictionaryValue*>(result.get());
293 298
294 int id; 299 int id;
295 if (!dictionary->GetInteger("id", &id)) { 300 if (!dictionary->GetInteger("id", &id)) {
296 std::string method_name; 301 std::string method_name;
297 if (!dictionary->GetString("method", &method_name)) 302 if (!dictionary->GetString("method", &method_name))
298 return; 303 return;
299 304
300 ListValue args; 305 ListValue args;
Matt Tytel 2012/07/20 00:41:15 You can use the the json schema compiler here too
301 args.Append(CreateDebuggeeId(tab_id_)); 306 args.Append(CreateDebuggeeId(tab_id_));
302 args.Append(Value::CreateStringValue(method_name)); 307 args.Append(Value::CreateStringValue(method_name));
303 Value* params_value; 308 Value* params_value;
304 if (dictionary->Get("params", &params_value)) 309 if (dictionary->Get("params", &params_value))
305 args.Append(params_value->DeepCopy()); 310 args.Append(params_value->DeepCopy());
306 311
307 std::string json_args; 312 std::string json_args;
308 base::JSONWriter::Write(&args, &json_args); 313 base::JSONWriter::Write(&args, &json_args);
309 314
310 profile->GetExtensionEventRouter()->DispatchEventToExtension( 315 profile->GetExtensionEventRouter()->DispatchEventToExtension(
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 UTF8ToUTF16(client_name_)); 352 UTF8ToUTF16(client_name_));
348 } 353 }
349 354
350 DebuggerFunction::DebuggerFunction() 355 DebuggerFunction::DebuggerFunction()
351 : contents_(0), 356 : contents_(0),
352 tab_id_(0), 357 tab_id_(0),
353 client_host_(0) { 358 client_host_(0) {
354 } 359 }
355 360
356 bool DebuggerFunction::InitTabContents() { 361 bool DebuggerFunction::InitTabContents() {
357 Value* debuggee;
358 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &debuggee));
359
360 DictionaryValue* dict = static_cast<DictionaryValue*>(debuggee);
361 EXTENSION_FUNCTION_VALIDATE(dict->GetInteger(keys::kTabIdKey, &tab_id_));
362
363 // Find the TabContents that contains this tab id. 362 // Find the TabContents that contains this tab id.
364 contents_ = NULL; 363 contents_ = NULL;
365 TabContents* tab_contents = NULL; 364 TabContents* tab_contents = NULL;
366 bool result = ExtensionTabUtil::GetTabById( 365 bool result = ExtensionTabUtil::GetTabById(
367 tab_id_, profile(), include_incognito(), NULL, NULL, &tab_contents, NULL); 366 tab_id_, profile(), include_incognito(), NULL, NULL, &tab_contents, NULL);
368 if (!result || !tab_contents) { 367 if (!result || !tab_contents) {
369 error_ = ExtensionErrorUtils::FormatErrorMessage( 368 error_ = ExtensionErrorUtils::FormatErrorMessage(
370 keys::kNoTabError, 369 keys::kNoTabError,
371 base::IntToString(tab_id_)); 370 base::IntToString(tab_id_));
372 return false; 371 return false;
(...skipping 27 matching lines...) Expand all
400 return false; 399 return false;
401 } 400 }
402 return true; 401 return true;
403 } 402 }
404 403
405 AttachDebuggerFunction::AttachDebuggerFunction() {} 404 AttachDebuggerFunction::AttachDebuggerFunction() {}
406 405
407 AttachDebuggerFunction::~AttachDebuggerFunction() {} 406 AttachDebuggerFunction::~AttachDebuggerFunction() {}
408 407
409 bool AttachDebuggerFunction::RunImpl() { 408 bool AttachDebuggerFunction::RunImpl() {
409 scoped_ptr<Attach::Params> params(Attach::Params::Create(*args_));
410 EXTENSION_FUNCTION_VALIDATE(params.get());
411
412 tab_id_ = params->target.tab_id;
410 if (!InitTabContents()) 413 if (!InitTabContents())
411 return false; 414 return false;
412 415
413 std::string version; 416 if (!webkit_glue::IsInspectorProtocolVersionSupported(
414 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &version)); 417 params->required_version)) {
415
416 if (!webkit_glue::IsInspectorProtocolVersionSupported(version)) {
417 error_ = ExtensionErrorUtils::FormatErrorMessage( 418 error_ = ExtensionErrorUtils::FormatErrorMessage(
418 keys::kProtocolVersionNotSupportedError, 419 keys::kProtocolVersionNotSupportedError,
419 version); 420 params->required_version);
420 return false; 421 return false;
421 } 422 }
422 423
423 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost( 424 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost(
424 contents_->GetRenderViewHost()); 425 contents_->GetRenderViewHost());
425 DevToolsClientHost* client_host = DevToolsManager::GetInstance()-> 426 DevToolsClientHost* client_host = DevToolsManager::GetInstance()->
426 GetDevToolsClientHostFor(agent); 427 GetDevToolsClientHostFor(agent);
427 428
428 if (client_host != NULL) { 429 if (client_host != NULL) {
429 error_ = ExtensionErrorUtils::FormatErrorMessage( 430 error_ = ExtensionErrorUtils::FormatErrorMessage(
430 keys::kAlreadyAttachedError, 431 keys::kAlreadyAttachedError,
431 base::IntToString(tab_id_)); 432 base::IntToString(tab_id_));
432 return false; 433 return false;
433 } 434 }
434 435
435 new ExtensionDevToolsClientHost(contents_, 436 new ExtensionDevToolsClientHost(contents_,
436 GetExtension()->id(), 437 GetExtension()->id(),
437 GetExtension()->name(), 438 GetExtension()->name(),
438 tab_id_); 439 tab_id_);
439 SendResponse(true); 440 SendResponse(true);
440 return true; 441 return true;
441 } 442 }
442 443
443 DetachDebuggerFunction::DetachDebuggerFunction() {} 444 DetachDebuggerFunction::DetachDebuggerFunction() {}
444 445
445 DetachDebuggerFunction::~DetachDebuggerFunction() {} 446 DetachDebuggerFunction::~DetachDebuggerFunction() {}
446 447
447 bool DetachDebuggerFunction::RunImpl() { 448 bool DetachDebuggerFunction::RunImpl() {
449 scoped_ptr<Detach::Params> params(Detach::Params::Create(*args_));
450 EXTENSION_FUNCTION_VALIDATE(params.get());
451
452 tab_id_ = params->target.tab_id;
448 if (!InitClientHost()) 453 if (!InitClientHost())
449 return false; 454 return false;
450 455
451 client_host_->Close(); 456 client_host_->Close();
452 SendResponse(true); 457 SendResponse(true);
453 return true; 458 return true;
454 } 459 }
455 460
456 SendCommandDebuggerFunction::SendCommandDebuggerFunction() {} 461 SendCommandDebuggerFunction::SendCommandDebuggerFunction() {}
457 462
458 SendCommandDebuggerFunction::~SendCommandDebuggerFunction() {} 463 SendCommandDebuggerFunction::~SendCommandDebuggerFunction() {}
459 464
460 bool SendCommandDebuggerFunction::RunImpl() { 465 bool SendCommandDebuggerFunction::RunImpl() {
466 scoped_ptr<SendCommand::Params> params(SendCommand::Params::Create(*args_));
467 EXTENSION_FUNCTION_VALIDATE(params.get());
461 468
469 tab_id_ = params->target.tab_id;
462 if (!InitClientHost()) 470 if (!InitClientHost())
463 return false; 471 return false;
464 472
465 std::string method; 473 client_host_->SendMessageToBackend(this, params->method,
466 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &method)); 474 params->command_params.get());
467
468 Value *params;
469 if (!args_->Get(2, &params))
470 params = NULL;
471
472 client_host_->SendMessageToBackend(this, method, params);
473 return true; 475 return true;
474 } 476 }
475 477
476 void SendCommandDebuggerFunction::SendResponseBody( 478 void SendCommandDebuggerFunction::SendResponseBody(
not at google - send to devlin 2012/07/20 00:45:44 ah, matt is right. you can use the compiler here t
477 DictionaryValue* dictionary) { 479 DictionaryValue* dictionary) {
478 Value* error_body; 480 Value* error_body;
479 if (dictionary->Get("error", &error_body)) { 481 if (dictionary->Get("error", &error_body)) {
480 base::JSONWriter::Write(error_body, &error_); 482 base::JSONWriter::Write(error_body, &error_);
481 SendResponse(false); 483 SendResponse(false);
482 return; 484 return;
483 } 485 }
484 486
485 Value* result_body; 487 Value* result_body;
486 if (dictionary->Get("result", &result_body)) 488 if (dictionary->Get("result", &result_body))
487 SetResult(result_body->DeepCopy()); 489 SetResult(result_body->DeepCopy());
488 else 490 else
489 SetResult(new DictionaryValue()); 491 SetResult(new DictionaryValue());
490 SendResponse(true); 492 SendResponse(true);
491 } 493 }
OLDNEW
« no previous file with comments | « no previous file | chrome/common/extensions/api/api.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698