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

Side by Side Diff: content/shell/browser/layout_test/layout_test_devtools_frontend.cc

Issue 2756623002: DevTools: extract bindings from ShellDevToolsFrontend (Closed)
Patch Set: fixup Created 3 years, 9 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/shell/browser/layout_test/layout_test_devtools_frontend.h"
6
7 #include "base/command_line.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/path_service.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "build/build_config.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/shell/browser/layout_test/blink_test_controller.h"
17 #include "content/shell/browser/shell.h"
18 #include "content/shell/common/layout_test/layout_test_switches.h"
19 #include "net/base/filename_util.h"
20
21 namespace content {
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.
40 GURL LayoutTestDevToolsFrontend::GetDevToolsPathAsURL(
41 const std::string& frontend_url) {
42 if (!frontend_url.empty())
43 return GURL(frontend_url);
44 base::FilePath dir_exe;
45 if (!PathService::Get(base::DIR_EXE, &dir_exe)) {
46 NOTREACHED();
47 return GURL();
48 }
49 #if defined(OS_MACOSX)
50 // On Mac, the executable is in
51 // out/Release/Content Shell.app/Contents/MacOS/Content Shell.
52 // We need to go up 3 directories to get to out/Release.
53 dir_exe = dir_exe.AppendASCII("../../..");
54 #endif
55 base::FilePath dev_tools_path;
56 bool is_debug_dev_tools = base::CommandLine::ForCurrentProcess()->HasSwitch(
57 switches::kDebugDevTools);
58 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
59 switches::kCustomDevToolsFrontend)) {
60 dev_tools_path = base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
61 switches::kCustomDevToolsFrontend);
62 } else {
63 std::string folder = is_debug_dev_tools ? "debug/" : "";
64 dev_tools_path = dir_exe.AppendASCII("resources/inspector/" + folder);
65 }
66
67 GURL result =
68 net::FilePathToFileURL(dev_tools_path.AppendASCII("inspector.html"));
69 std::string url_string =
70 base::StringPrintf("%s?experiments=true", result.spec().c_str());
71 if (is_debug_dev_tools)
72 url_string += "&debugFrontend=true";
73 return GURL(url_string);
74 }
75
76 // static.
77 GURL LayoutTestDevToolsFrontend::MapJSTestURL(const GURL& test_url) {
78 std::string url_string = GetDevToolsPathAsURL(std::string()).spec();
79 std::string inspector_file_name = "inspector.html";
80 size_t start_position = url_string.find(inspector_file_name);
81 url_string.replace(start_position, inspector_file_name.length(),
82 "unit_test_runner.html");
83 url_string += "&test=" + test_url.spec();
84 return GURL(url_string);
85 }
86
87 void LayoutTestDevToolsFrontend::ReuseFrontend(const std::string& settings,
88 const std::string frontend_url) {
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_) {
100 pending_evaluations_.push_back(std::make_pair(call_id, script));
101 return;
102 }
103
104 std::string encoded_script;
105 base::JSONWriter::Write(base::Value(script), &encoded_script);
106 std::string source =
107 base::StringPrintf("DevToolsAPI.evaluateForTestInFrontend(%d, %s);",
108 call_id,
109 encoded_script.c_str());
110 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(
111 base::UTF8ToUTF16(source));
112 }
113
114 LayoutTestDevToolsFrontend::LayoutTestDevToolsFrontend(
115 Shell* frontend_shell,
116 WebContents* inspected_contents)
117 : ShellDevToolsFrontend(frontend_shell, inspected_contents),
118 ready_for_test_(false) {
119 }
120
121 LayoutTestDevToolsFrontend::~LayoutTestDevToolsFrontend() {
122 }
123
124 void LayoutTestDevToolsFrontend::AgentHostClosed(
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) {
131 std::string method;
132 base::DictionaryValue* dict = nullptr;
133 std::unique_ptr<base::Value> parsed_message = base::JSONReader::Read(message);
134 if (parsed_message &&
135 parsed_message->GetAsDictionary(&dict) &&
136 dict->GetString("method", &method) &&
137 method == "readyForTest") {
138 ready_for_test_ = true;
139 for (const auto& pair : pending_evaluations_)
140 EvaluateInFrontend(pair.first, pair.second);
141 pending_evaluations_.clear();
142 return;
143 }
144
145 ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend(message);
146 }
147
148 void LayoutTestDevToolsFrontend::RenderProcessGone(
149 base::TerminationStatus status) {
150 BlinkTestController::Get()->DevToolsProcessCrashed();
151 }
152
153 void LayoutTestDevToolsFrontend::RenderFrameCreated(
154 RenderFrameHost* render_frame_host) {
155 BlinkTestController::Get()->HandleNewRenderFrameHost(render_frame_host);
156 }
157
158 } // namespace content
OLDNEW
« no previous file with comments | « content/shell/browser/layout_test/layout_test_devtools_frontend.h ('k') | content/shell/browser/shell.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698