| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "content/browser/devtools/renderer_overrides_handler.h" | 5 #include "content/browser/devtools/renderer_overrides_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/string16.h" | 12 #include "base/string16.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "content/browser/child_process_security_policy_impl.h" | 15 #include "content/browser/child_process_security_policy_impl.h" |
| 16 #include "content/browser/devtools/devtools_protocol_constants.h" |
| 16 #include "content/browser/renderer_host/render_view_host_delegate.h" | 17 #include "content/browser/renderer_host/render_view_host_delegate.h" |
| 17 #include "content/public/browser/devtools_agent_host.h" | 18 #include "content/public/browser/devtools_agent_host.h" |
| 18 #include "content/public/browser/javascript_dialog_manager.h" | 19 #include "content/public/browser/javascript_dialog_manager.h" |
| 19 #include "content/public/browser/navigation_controller.h" | 20 #include "content/public/browser/navigation_controller.h" |
| 20 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
| 22 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/browser/web_contents_delegate.h" | 24 #include "content/public/browser/web_contents_delegate.h" |
| 24 #include "content/public/common/page_transition_types.h" | 25 #include "content/public/common/page_transition_types.h" |
| 25 #include "content/public/common/referrer.h" | 26 #include "content/public/common/referrer.h" |
| 26 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
| 27 | 28 |
| 28 namespace content { | 29 namespace content { |
| 29 | 30 |
| 30 namespace { | |
| 31 | |
| 32 const char kDOMFileInputCommand[] = "DOM.setFileInputFiles"; | |
| 33 const char kDOMFileInputFilesParam[] = "files"; | |
| 34 const char kPageHandleDialogCommand[] = "Page.handleJavaScriptDialog"; | |
| 35 const char kPageHandleDialogAcceptParam[] = "accept"; | |
| 36 const char kPageHandleDialogPromptTextParam[] = "promptText"; | |
| 37 const char kPageNavigateCommand[] = "Page.navigate"; | |
| 38 const char kPageNavigateUrlParam[] = "url"; | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 RendererOverridesHandler::RendererOverridesHandler(DevToolsAgentHost* agent) | 31 RendererOverridesHandler::RendererOverridesHandler(DevToolsAgentHost* agent) |
| 43 : agent_(agent) { | 32 : agent_(agent) { |
| 44 RegisterCommandHandler( | 33 RegisterCommandHandler( |
| 45 kDOMFileInputCommand, | 34 devtools::DOM::setFileInputFiles::kName, |
| 46 base::Bind( | 35 base::Bind( |
| 47 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles, | 36 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles, |
| 48 base::Unretained(this))); | 37 base::Unretained(this))); |
| 49 RegisterCommandHandler( | 38 RegisterCommandHandler( |
| 50 kPageHandleDialogCommand, | 39 devtools::Page::handleJavaScriptDialog::kName, |
| 51 base::Bind( | 40 base::Bind( |
| 52 &RendererOverridesHandler::PageHandleJavaScriptDialog, | 41 &RendererOverridesHandler::PageHandleJavaScriptDialog, |
| 53 base::Unretained(this))); | 42 base::Unretained(this))); |
| 54 RegisterCommandHandler( | 43 RegisterCommandHandler( |
| 55 kPageNavigateCommand, | 44 devtools::Page::navigate::kName, |
| 56 base::Bind( | 45 base::Bind( |
| 57 &RendererOverridesHandler::PageNavigate, | 46 &RendererOverridesHandler::PageNavigate, |
| 58 base::Unretained(this))); | 47 base::Unretained(this))); |
| 59 } | 48 } |
| 60 | 49 |
| 61 RendererOverridesHandler::~RendererOverridesHandler() {} | 50 RendererOverridesHandler::~RendererOverridesHandler() {} |
| 62 | 51 |
| 63 scoped_ptr<DevToolsProtocol::Response> | 52 scoped_ptr<DevToolsProtocol::Response> |
| 64 RendererOverridesHandler::GrantPermissionsForSetFileInputFiles( | 53 RendererOverridesHandler::GrantPermissionsForSetFileInputFiles( |
| 65 DevToolsProtocol::Command* command) { | 54 DevToolsProtocol::Command* command) { |
| 66 base::DictionaryValue* params = command->params(); | 55 base::DictionaryValue* params = command->params(); |
| 67 base::ListValue* file_list = NULL; | 56 base::ListValue* file_list = NULL; |
| 68 if (!params || !params->GetList(kDOMFileInputFilesParam, &file_list)) { | 57 const char* param = |
| 58 devtools::DOM::setFileInputFiles::kParamFiles; |
| 59 if (!params || !params->GetList(param, &file_list)) { |
| 69 return command->ErrorResponse( | 60 return command->ErrorResponse( |
| 70 DevToolsProtocol::kErrorInvalidParams, | 61 DevToolsProtocol::kErrorInvalidParams, |
| 71 base::StringPrintf("Missing or invalid '%s' parameter", | 62 base::StringPrintf("Missing or invalid '%s' parameter", param)); |
| 72 kDOMFileInputFilesParam)); | |
| 73 } | 63 } |
| 74 RenderViewHost* host = agent_->GetRenderViewHost(); | 64 RenderViewHost* host = agent_->GetRenderViewHost(); |
| 75 if (!host) | 65 if (!host) |
| 76 return scoped_ptr<DevToolsProtocol::Response>(); | 66 return scoped_ptr<DevToolsProtocol::Response>(); |
| 77 | 67 |
| 78 for (size_t i = 0; i < file_list->GetSize(); ++i) { | 68 for (size_t i = 0; i < file_list->GetSize(); ++i) { |
| 79 base::FilePath::StringType file; | 69 base::FilePath::StringType file; |
| 80 if (!file_list->GetString(i, &file)) { | 70 if (!file_list->GetString(i, &file)) { |
| 81 return command->ErrorResponse( | 71 return command->ErrorResponse( |
| 82 DevToolsProtocol::kErrorInvalidParams, | 72 DevToolsProtocol::kErrorInvalidParams, |
| 83 base::StringPrintf("'%s' must be a list of strings", | 73 base::StringPrintf("'%s' must be a list of strings", param)); |
| 84 kDOMFileInputFilesParam)); | |
| 85 } | 74 } |
| 86 ChildProcessSecurityPolicyImpl::GetInstance()->GrantReadFile( | 75 ChildProcessSecurityPolicyImpl::GetInstance()->GrantReadFile( |
| 87 host->GetProcess()->GetID(), base::FilePath(file)); | 76 host->GetProcess()->GetID(), base::FilePath(file)); |
| 88 } | 77 } |
| 89 return scoped_ptr<DevToolsProtocol::Response>(); | 78 return scoped_ptr<DevToolsProtocol::Response>(); |
| 90 } | 79 } |
| 91 | 80 |
| 92 scoped_ptr<DevToolsProtocol::Response> | 81 scoped_ptr<DevToolsProtocol::Response> |
| 93 RendererOverridesHandler::PageHandleJavaScriptDialog( | 82 RendererOverridesHandler::PageHandleJavaScriptDialog( |
| 94 DevToolsProtocol::Command* command) { | 83 DevToolsProtocol::Command* command) { |
| 95 base::DictionaryValue* params = command->params(); | 84 base::DictionaryValue* params = command->params(); |
| 85 const char* paramAccept = |
| 86 devtools::Page::handleJavaScriptDialog::kParamAccept; |
| 96 bool accept; | 87 bool accept; |
| 97 if (!params || !params->GetBoolean(kPageHandleDialogAcceptParam, &accept)) { | 88 if (!params || !params->GetBoolean(paramAccept, &accept)) { |
| 98 return command->ErrorResponse( | 89 return command->ErrorResponse( |
| 99 DevToolsProtocol::kErrorInvalidParams, | 90 DevToolsProtocol::kErrorInvalidParams, |
| 100 base::StringPrintf("Missing or invalid '%s' parameter", | 91 base::StringPrintf("Missing or invalid '%s' parameter", paramAccept)); |
| 101 kPageHandleDialogAcceptParam)); | |
| 102 } | 92 } |
| 103 string16 prompt_override; | 93 string16 prompt_override; |
| 104 string16* prompt_override_ptr = &prompt_override; | 94 string16* prompt_override_ptr = &prompt_override; |
| 105 if (!params || !params->GetString(kPageHandleDialogPromptTextParam, | 95 if (!params || !params->GetString( |
| 106 prompt_override_ptr)) { | 96 devtools::Page::handleJavaScriptDialog::kParamPromptText, |
| 97 prompt_override_ptr)) { |
| 107 prompt_override_ptr = NULL; | 98 prompt_override_ptr = NULL; |
| 108 } | 99 } |
| 109 | 100 |
| 110 RenderViewHost* host = agent_->GetRenderViewHost(); | 101 RenderViewHost* host = agent_->GetRenderViewHost(); |
| 111 if (host) { | 102 if (host) { |
| 112 WebContents* web_contents = host->GetDelegate()->GetAsWebContents(); | 103 WebContents* web_contents = host->GetDelegate()->GetAsWebContents(); |
| 113 if (web_contents) { | 104 if (web_contents) { |
| 114 JavaScriptDialogManager* manager = | 105 JavaScriptDialogManager* manager = |
| 115 web_contents->GetDelegate()->GetJavaScriptDialogManager(); | 106 web_contents->GetDelegate()->GetJavaScriptDialogManager(); |
| 116 if (manager && manager->HandleJavaScriptDialog( | 107 if (manager && manager->HandleJavaScriptDialog( |
| 117 web_contents, accept, prompt_override_ptr)) { | 108 web_contents, accept, prompt_override_ptr)) { |
| 118 return scoped_ptr<DevToolsProtocol::Response>(); | 109 return scoped_ptr<DevToolsProtocol::Response>(); |
| 119 } | 110 } |
| 120 } | 111 } |
| 121 } | 112 } |
| 122 return command->ErrorResponse( | 113 return command->ErrorResponse( |
| 123 DevToolsProtocol::kErrorInternalError, | 114 DevToolsProtocol::kErrorInternalError, |
| 124 "No JavaScript dialog to handle"); | 115 "No JavaScript dialog to handle"); |
| 125 } | 116 } |
| 126 | 117 |
| 127 scoped_ptr<DevToolsProtocol::Response> | 118 scoped_ptr<DevToolsProtocol::Response> |
| 128 RendererOverridesHandler::PageNavigate( | 119 RendererOverridesHandler::PageNavigate( |
| 129 DevToolsProtocol::Command* command) { | 120 DevToolsProtocol::Command* command) { |
| 130 base::DictionaryValue* params = command->params(); | 121 base::DictionaryValue* params = command->params(); |
| 131 std::string url; | 122 std::string url; |
| 132 if (!params || !params->GetString(kPageNavigateUrlParam, &url)) { | 123 const char* param = devtools::Page::navigate::kParamUrl; |
| 124 if (!params || !params->GetString(param, &url)) { |
| 133 return command->ErrorResponse( | 125 return command->ErrorResponse( |
| 134 DevToolsProtocol::kErrorInvalidParams, | 126 DevToolsProtocol::kErrorInvalidParams, |
| 135 base::StringPrintf("Missing or invalid '%s' parameter", | 127 base::StringPrintf("Missing or invalid '%s' parameter", |
| 136 kPageNavigateUrlParam)); | 128 param)); |
| 137 } | 129 } |
| 138 GURL gurl(url); | 130 GURL gurl(url); |
| 139 if (!gurl.is_valid()) { | 131 if (!gurl.is_valid()) { |
| 140 return command->ErrorResponse( | 132 return command->ErrorResponse( |
| 141 DevToolsProtocol::kErrorInternalError, | 133 DevToolsProtocol::kErrorInternalError, |
| 142 "Cannot navigate to invalid URL"); | 134 "Cannot navigate to invalid URL"); |
| 143 } | 135 } |
| 144 RenderViewHost* host = agent_->GetRenderViewHost(); | 136 RenderViewHost* host = agent_->GetRenderViewHost(); |
| 145 if (host) { | 137 if (host) { |
| 146 WebContents* web_contents = host->GetDelegate()->GetAsWebContents(); | 138 WebContents* web_contents = host->GetDelegate()->GetAsWebContents(); |
| 147 if (web_contents) { | 139 if (web_contents) { |
| 148 web_contents->GetController().LoadURL( | 140 web_contents->GetController().LoadURL( |
| 149 gurl, Referrer(), PAGE_TRANSITION_TYPED, ""); | 141 gurl, Referrer(), PAGE_TRANSITION_TYPED, ""); |
| 150 return command->SuccessResponse(new base::DictionaryValue()); | 142 return command->SuccessResponse(new base::DictionaryValue()); |
| 151 } | 143 } |
| 152 } | 144 } |
| 153 return command->ErrorResponse( | 145 return command->ErrorResponse( |
| 154 DevToolsProtocol::kErrorInternalError, | 146 DevToolsProtocol::kErrorInternalError, |
| 155 "No WebContents to navigate"); | 147 "No WebContents to navigate"); |
| 156 } | 148 } |
| 157 | 149 |
| 158 } // namespace content | 150 } // namespace content |
| OLD | NEW |