| 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::LoadDevTools(const std::string& settings, |
| 88 const std::string frontend_url) { | 72 const std::string& frontend_url) { |
| 89 DisconnectFromTarget(); | |
| 90 SetPreferences(settings); | 73 SetPreferences(settings); |
| 91 ready_for_test_ = false; | 74 GURL devtools_url = |
| 92 pending_evaluations_.clear(); | 75 LayoutTestDevToolsBindings::GetDevToolsPathAsURL(frontend_url); |
| 93 frontend_shell()->LoadURL(GetDevToolsPathAsURL(frontend_url)); | 76 NavigationController::LoadURLParams params(devtools_url); |
| 77 params.transition_type = ui::PageTransitionFromInt( |
| 78 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
| 79 web_contents()->GetController().LoadURLWithParams(params); |
| 80 web_contents()->Focus(); |
| 81 CreateFrontendHost(); |
| 94 } | 82 } |
| 95 | 83 |
| 96 void LayoutTestDevToolsFrontend::EvaluateInFrontend( | 84 void LayoutTestDevToolsBindings::EvaluateInFrontend(int call_id, |
| 97 int call_id, | 85 const std::string& script) { |
| 98 const std::string& script) { | |
| 99 if (!ready_for_test_) { | 86 if (!ready_for_test_) { |
| 100 pending_evaluations_.push_back(std::make_pair(call_id, script)); | 87 pending_evaluations_.push_back(std::make_pair(call_id, script)); |
| 101 return; | 88 return; |
| 102 } | 89 } |
| 103 | 90 |
| 104 std::string encoded_script; | 91 std::string encoded_script; |
| 105 base::JSONWriter::Write(base::Value(script), &encoded_script); | 92 base::JSONWriter::Write(base::Value(script), &encoded_script); |
| 106 std::string source = | 93 std::string source = |
| 107 base::StringPrintf("DevToolsAPI.evaluateForTestInFrontend(%d, %s);", | 94 base::StringPrintf("DevToolsAPI.evaluateForTestInFrontend(%d, %s);", |
| 108 call_id, | 95 call_id, encoded_script.c_str()); |
| 109 encoded_script.c_str()); | |
| 110 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( | 96 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( |
| 111 base::UTF8ToUTF16(source)); | 97 base::UTF8ToUTF16(source)); |
| 112 } | 98 } |
| 113 | 99 |
| 114 LayoutTestDevToolsFrontend::LayoutTestDevToolsFrontend( | 100 LayoutTestDevToolsBindings::LayoutTestDevToolsBindings( |
| 115 Shell* frontend_shell, | 101 WebContents* devtools_contents, |
| 116 WebContents* inspected_contents) | 102 WebContents* inspected_contents) |
| 117 : ShellDevToolsFrontend(frontend_shell, inspected_contents), | 103 : ShellDevToolsBindings(devtools_contents, inspected_contents, nullptr), |
| 118 ready_for_test_(false) { | 104 ready_for_test_(false) {} |
| 119 } | |
| 120 | 105 |
| 121 LayoutTestDevToolsFrontend::~LayoutTestDevToolsFrontend() { | 106 LayoutTestDevToolsBindings::~LayoutTestDevToolsBindings() {} |
| 122 } | |
| 123 | 107 |
| 124 void LayoutTestDevToolsFrontend::AgentHostClosed( | 108 void LayoutTestDevToolsBindings::HandleMessageFromDevToolsFrontend( |
| 125 DevToolsAgentHost* agent_host, bool replaced) { | |
| 126 // Do not close the front-end shell. | |
| 127 } | |
| 128 | |
| 129 void LayoutTestDevToolsFrontend::HandleMessageFromDevToolsFrontend( | |
| 130 const std::string& message) { | 109 const std::string& message) { |
| 131 std::string method; | 110 std::string method; |
| 132 base::DictionaryValue* dict = nullptr; | 111 base::DictionaryValue* dict = nullptr; |
| 133 std::unique_ptr<base::Value> parsed_message = base::JSONReader::Read(message); | 112 std::unique_ptr<base::Value> parsed_message = base::JSONReader::Read(message); |
| 134 if (parsed_message && | 113 if (parsed_message && parsed_message->GetAsDictionary(&dict) && |
| 135 parsed_message->GetAsDictionary(&dict) && | 114 dict->GetString("method", &method) && method == "readyForTest") { |
| 136 dict->GetString("method", &method) && | |
| 137 method == "readyForTest") { | |
| 138 ready_for_test_ = true; | 115 ready_for_test_ = true; |
| 139 for (const auto& pair : pending_evaluations_) | 116 for (const auto& pair : pending_evaluations_) |
| 140 EvaluateInFrontend(pair.first, pair.second); | 117 EvaluateInFrontend(pair.first, pair.second); |
| 141 pending_evaluations_.clear(); | 118 pending_evaluations_.clear(); |
| 142 return; | 119 return; |
| 143 } | 120 } |
| 144 | 121 |
| 145 ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend(message); | 122 ShellDevToolsBindings::HandleMessageFromDevToolsFrontend(message); |
| 146 } | 123 } |
| 147 | 124 |
| 148 void LayoutTestDevToolsFrontend::RenderProcessGone( | 125 void LayoutTestDevToolsBindings::RenderProcessGone( |
| 149 base::TerminationStatus status) { | 126 base::TerminationStatus status) { |
| 150 BlinkTestController::Get()->DevToolsProcessCrashed(); | 127 BlinkTestController::Get()->DevToolsProcessCrashed(); |
| 151 } | 128 } |
| 152 | 129 |
| 153 void LayoutTestDevToolsFrontend::RenderFrameCreated( | 130 void LayoutTestDevToolsBindings::RenderFrameCreated( |
| 154 RenderFrameHost* render_frame_host) { | 131 RenderFrameHost* render_frame_host) { |
| 155 BlinkTestController::Get()->HandleNewRenderFrameHost(render_frame_host); | 132 BlinkTestController::Get()->HandleNewRenderFrameHost(render_frame_host); |
| 156 } | 133 } |
| 157 | 134 |
| 158 } // namespace content | 135 } // namespace content |
| OLD | NEW |