| 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 #include "content/shell/browser/layout_test/layout_test_devtools_frontend.h" | 5 #include "content/shell/browser/layout_test/layout_test_devtools_bindings.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
| 15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 16 #include "content/shell/browser/layout_test/blink_test_controller.h" | 16 #include "content/shell/browser/layout_test/blink_test_controller.h" |
| 17 #include "content/shell/browser/shell.h" | 17 #include "content/shell/browser/shell.h" |
| 18 #include "content/shell/common/layout_test/layout_test_switches.h" | 18 #include "content/shell/common/layout_test/layout_test_switches.h" |
| 19 #include "net/base/filename_util.h" | 19 #include "net/base/filename_util.h" |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 // static | |
| 24 LayoutTestDevToolsFrontend* LayoutTestDevToolsFrontend::Show( | |
| 25 WebContents* inspected_contents, | |
| 26 const std::string& settings, | |
| 27 const std::string& frontend_url) { | |
| 28 Shell* shell = Shell::CreateNewWindow(inspected_contents->GetBrowserContext(), | |
| 29 GURL(), | |
| 30 NULL, | |
| 31 gfx::Size()); | |
| 32 LayoutTestDevToolsFrontend* devtools_frontend = | |
| 33 new LayoutTestDevToolsFrontend(shell, inspected_contents); | |
| 34 devtools_frontend->SetPreferences(settings); | |
| 35 shell->LoadURL(GetDevToolsPathAsURL(frontend_url)); | |
| 36 return devtools_frontend; | |
| 37 } | |
| 38 | |
| 39 // static. | 23 // static. |
| 40 GURL LayoutTestDevToolsFrontend::GetDevToolsPathAsURL( | 24 GURL LayoutTestDevToolsBindings::GetDevToolsPathAsURL( |
| 41 const std::string& frontend_url) { | 25 const std::string& frontend_url) { |
| 42 if (!frontend_url.empty()) | 26 if (!frontend_url.empty()) |
| 43 return GURL(frontend_url); | 27 return GURL(frontend_url); |
| 44 base::FilePath dir_exe; | 28 base::FilePath dir_exe; |
| 45 if (!PathService::Get(base::DIR_EXE, &dir_exe)) { | 29 if (!PathService::Get(base::DIR_EXE, &dir_exe)) { |
| 46 NOTREACHED(); | 30 NOTREACHED(); |
| 47 return GURL(); | 31 return GURL(); |
| 48 } | 32 } |
| 49 #if defined(OS_MACOSX) | 33 #if defined(OS_MACOSX) |
| 50 // On Mac, the executable is in | 34 // On Mac, the executable is in |
| (...skipping 16 matching lines...) Expand all Loading... |
| 67 GURL result = | 51 GURL result = |
| 68 net::FilePathToFileURL(dev_tools_path.AppendASCII("inspector.html")); | 52 net::FilePathToFileURL(dev_tools_path.AppendASCII("inspector.html")); |
| 69 std::string url_string = | 53 std::string url_string = |
| 70 base::StringPrintf("%s?experiments=true", result.spec().c_str()); | 54 base::StringPrintf("%s?experiments=true", result.spec().c_str()); |
| 71 if (is_debug_dev_tools) | 55 if (is_debug_dev_tools) |
| 72 url_string += "&debugFrontend=true"; | 56 url_string += "&debugFrontend=true"; |
| 73 return GURL(url_string); | 57 return GURL(url_string); |
| 74 } | 58 } |
| 75 | 59 |
| 76 // static. | 60 // static. |
| 77 GURL LayoutTestDevToolsFrontend::MapJSTestURL(const GURL& test_url) { | 61 GURL LayoutTestDevToolsBindings::MapJSTestURL(const GURL& test_url) { |
| 78 std::string url_string = GetDevToolsPathAsURL(std::string()).spec(); | 62 std::string url_string = GetDevToolsPathAsURL(std::string()).spec(); |
| 79 std::string inspector_file_name = "inspector.html"; | 63 std::string inspector_file_name = "inspector.html"; |
| 80 size_t start_position = url_string.find(inspector_file_name); | 64 size_t start_position = url_string.find(inspector_file_name); |
| 81 url_string.replace(start_position, inspector_file_name.length(), | 65 url_string.replace(start_position, inspector_file_name.length(), |
| 82 "unit_test_runner.html"); | 66 "unit_test_runner.html"); |
| 83 url_string += "&test=" + test_url.spec(); | 67 url_string += "&test=" + test_url.spec(); |
| 84 return GURL(url_string); | 68 return GURL(url_string); |
| 85 } | 69 } |
| 86 | 70 |
| 87 void LayoutTestDevToolsFrontend::ReuseFrontend(const std::string& settings, | 71 void LayoutTestDevToolsBindings::EvaluateInFrontend(int call_id, |
| 88 const std::string frontend_url) { | 72 const std::string& script) { |
| 89 DisconnectFromTarget(); | |
| 90 SetPreferences(settings); | |
| 91 ready_for_test_ = false; | |
| 92 pending_evaluations_.clear(); | |
| 93 frontend_shell()->LoadURL(GetDevToolsPathAsURL(frontend_url)); | |
| 94 } | |
| 95 | |
| 96 void LayoutTestDevToolsFrontend::EvaluateInFrontend( | |
| 97 int call_id, | |
| 98 const std::string& script) { | |
| 99 if (!ready_for_test_) { | 73 if (!ready_for_test_) { |
| 100 pending_evaluations_.push_back(std::make_pair(call_id, script)); | 74 pending_evaluations_.push_back(std::make_pair(call_id, script)); |
| 101 return; | 75 return; |
| 102 } | 76 } |
| 103 | 77 |
| 104 std::string encoded_script; | 78 std::string encoded_script; |
| 105 base::JSONWriter::Write(base::Value(script), &encoded_script); | 79 base::JSONWriter::Write(base::Value(script), &encoded_script); |
| 106 std::string source = | 80 std::string source = |
| 107 base::StringPrintf("DevToolsAPI.evaluateForTestInFrontend(%d, %s);", | 81 base::StringPrintf("DevToolsAPI.evaluateForTestInFrontend(%d, %s);", |
| 108 call_id, | 82 call_id, encoded_script.c_str()); |
| 109 encoded_script.c_str()); | |
| 110 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( | 83 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( |
| 111 base::UTF8ToUTF16(source)); | 84 base::UTF8ToUTF16(source)); |
| 112 } | 85 } |
| 113 | 86 |
| 114 LayoutTestDevToolsFrontend::LayoutTestDevToolsFrontend( | 87 LayoutTestDevToolsBindings::LayoutTestDevToolsBindings( |
| 115 Shell* frontend_shell, | 88 WebContents* devtools_contents, |
| 116 WebContents* inspected_contents) | 89 WebContents* inspected_contents) |
| 117 : ShellDevToolsFrontend(frontend_shell, inspected_contents), | 90 : ShellDevToolsBindings(devtools_contents, inspected_contents), |
| 118 ready_for_test_(false) { | 91 ready_for_test_(false) {} |
| 119 } | |
| 120 | 92 |
| 121 LayoutTestDevToolsFrontend::~LayoutTestDevToolsFrontend() { | 93 LayoutTestDevToolsBindings::~LayoutTestDevToolsBindings() {} |
| 122 } | |
| 123 | 94 |
| 124 void LayoutTestDevToolsFrontend::AgentHostClosed( | 95 void LayoutTestDevToolsBindings::AgentHostClosed(DevToolsAgentHost* agent_host, |
| 125 DevToolsAgentHost* agent_host, bool replaced) { | 96 bool replaced) { |
| 126 // Do not close the front-end shell. | 97 // Do not close the front-end shell. |
| 127 } | 98 } |
| 128 | 99 |
| 129 void LayoutTestDevToolsFrontend::HandleMessageFromDevToolsFrontend( | 100 void LayoutTestDevToolsBindings::HandleMessageFromDevToolsFrontend( |
| 130 const std::string& message) { | 101 const std::string& message) { |
| 131 std::string method; | 102 std::string method; |
| 132 base::DictionaryValue* dict = nullptr; | 103 base::DictionaryValue* dict = nullptr; |
| 133 std::unique_ptr<base::Value> parsed_message = base::JSONReader::Read(message); | 104 std::unique_ptr<base::Value> parsed_message = base::JSONReader::Read(message); |
| 134 if (parsed_message && | 105 if (parsed_message && parsed_message->GetAsDictionary(&dict) && |
| 135 parsed_message->GetAsDictionary(&dict) && | 106 dict->GetString("method", &method) && method == "readyForTest") { |
| 136 dict->GetString("method", &method) && | |
| 137 method == "readyForTest") { | |
| 138 ready_for_test_ = true; | 107 ready_for_test_ = true; |
| 139 for (const auto& pair : pending_evaluations_) | 108 for (const auto& pair : pending_evaluations_) |
| 140 EvaluateInFrontend(pair.first, pair.second); | 109 EvaluateInFrontend(pair.first, pair.second); |
| 141 pending_evaluations_.clear(); | 110 pending_evaluations_.clear(); |
| 142 return; | 111 return; |
| 143 } | 112 } |
| 144 | 113 |
| 145 ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend(message); | 114 ShellDevToolsBindings::HandleMessageFromDevToolsFrontend(message); |
| 146 } | 115 } |
| 147 | 116 |
| 148 void LayoutTestDevToolsFrontend::RenderProcessGone( | 117 void LayoutTestDevToolsBindings::RenderProcessGone( |
| 149 base::TerminationStatus status) { | 118 base::TerminationStatus status) { |
| 150 BlinkTestController::Get()->DevToolsProcessCrashed(); | 119 BlinkTestController::Get()->DevToolsProcessCrashed(); |
| 151 } | 120 } |
| 152 | 121 |
| 153 void LayoutTestDevToolsFrontend::RenderFrameCreated( | 122 void LayoutTestDevToolsBindings::RenderFrameCreated( |
| 154 RenderFrameHost* render_frame_host) { | 123 RenderFrameHost* render_frame_host) { |
| 155 BlinkTestController::Get()->HandleNewRenderFrameHost(render_frame_host); | 124 BlinkTestController::Get()->HandleNewRenderFrameHost(render_frame_host); |
| 156 } | 125 } |
| 157 | 126 |
| 158 } // namespace content | 127 } // namespace content |
| OLD | NEW |