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

Side by Side Diff: chrome/test/chromedriver/chrome_impl.cc

Issue 11415205: [chromedriver] Implement connecting to devtools and loading a page. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 8 years 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 | Annotate | Revision Log
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 #include "chrome/test/chromedriver/chrome_impl.h" 5 #include "chrome/test/chromedriver/chrome_impl.h"
6 6
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
7 #include "base/logging.h" 9 #include "base/logging.h"
8 #include "base/process_util.h" 10 #include "base/process_util.h"
11 #include "base/stringprintf.h"
12 #include "base/threading/platform_thread.h"
13 #include "base/time.h"
14 #include "base/values.h"
15 #include "chrome/test/chromedriver/devtools_client.h"
16 #include "chrome/test/chromedriver/net/net_util.h"
17 #include "chrome/test/chromedriver/net/url_request_context_getter.h"
9 #include "chrome/test/chromedriver/status.h" 18 #include "chrome/test/chromedriver/status.h"
19 #include "googleurl/src/gurl.h"
20
21 namespace {
22
23 Status FetchPagesInfo(URLRequestContextGetter* context_getter,
24 int port,
25 std::list<std::string>* debugger_urls) {
26 std::string url = base::StringPrintf(
27 "http://127.0.0.1:%d/json", port);
28 std::string data;
29 if (!FetchUrl(GURL(url), context_getter, &data))
30 return Status(kChromeNotReachable);
31 return internal::ParsePagesInfo(data, debugger_urls);
32 }
33
34 } // namespace
10 35
11 ChromeImpl::ChromeImpl(base::ProcessHandle process, 36 ChromeImpl::ChromeImpl(base::ProcessHandle process,
12 base::ScopedTempDir* user_data_dir) 37 URLRequestContextGetter* context_getter,
13 : process_(process) { 38 base::ScopedTempDir* user_data_dir,
39 int port)
40 : process_(process),
41 context_getter_(context_getter),
42 port_(port) {
14 if (user_data_dir->IsValid()) { 43 if (user_data_dir->IsValid()) {
15 CHECK(user_data_dir_.Set(user_data_dir->Take())); 44 CHECK(user_data_dir_.Set(user_data_dir->Take()));
16 } 45 }
17 } 46 }
18 47
19 ChromeImpl::~ChromeImpl() { 48 ChromeImpl::~ChromeImpl() {
20 base::CloseProcessHandle(process_); 49 base::CloseProcessHandle(process_);
21 } 50 }
22 51
52 Status ChromeImpl::Init() {
53 base::Time deadline = base::Time::Now() + base::TimeDelta::FromSeconds(20);
54 std::list<std::string> debugger_urls;
55 while (base::Time::Now() < deadline) {
56 FetchPagesInfo(context_getter_, port_, &debugger_urls);
57 if (debugger_urls.empty())
58 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
59 else
60 break;
61 }
62 if (debugger_urls.empty())
63 return Status(kUnknownError, "unable to discover open pages");
64 client_.reset(new DevToolsClient(context_getter_, debugger_urls.front()));
65 return Status(kOk);
66 }
67
68 Status ChromeImpl::Load(const std::string& url) {
69 base::DictionaryValue params;
70 params.SetString("url", url);
71 return client_->SendCommand("Page.navigate", params);
72 }
73
23 Status ChromeImpl::Quit() { 74 Status ChromeImpl::Quit() {
24 if (!base::KillProcess(process_, 0, true)) 75 if (!base::KillProcess(process_, 0, true))
25 return Status(kUnknownError, "cannot kill Chrome"); 76 return Status(kUnknownError, "cannot kill Chrome");
26 return Status(kOk); 77 return Status(kOk);
27 } 78 }
79
80 namespace internal {
81
82 Status ParsePagesInfo(const std::string& data,
83 std::list<std::string>* debugger_urls) {
84 scoped_ptr<base::Value> value(base::JSONReader::Read(data));
85 if (!value.get())
86 return Status(kUnknownError, "DevTools returned invalid JSON");
87 base::ListValue* list;
88 if (!value->GetAsList(&list))
89 return Status(kUnknownError, "DevTools did not return list");
90
91 std::list<std::string> internal_urls;
92 for (size_t i = 0; i < list->GetSize(); ++i) {
93 base::DictionaryValue* info;
94 if (!list->GetDictionary(i, &info))
95 return Status(kUnknownError, "DevTools contains non-dictionary item");
96 std::string debugger_url;
97 if (!info->GetString("webSocketDebuggerUrl", &debugger_url))
98 return Status(kUnknownError, "DevTools did not include debugger URL");
99 internal_urls.push_back(debugger_url);
100 }
101 debugger_urls->swap(internal_urls);
102 return Status(kOk);
103 }
104
105 } // namespace internal
OLDNEW
« no previous file with comments | « chrome/test/chromedriver/chrome_impl.h ('k') | chrome/test/chromedriver/chrome_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698