Roll src/third_party/WebKit bf18a82:a9cee16 (svn 185297:185304)
[chromium-blink-merge.git] / chrome / test / chromedriver / session.cc
blobdf7d67424a7699f819d88fe1f27a221188eb8439
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
3 // found in the LICENSE file.
5 #include "chrome/test/chromedriver/session.h"
7 #include <list>
9 #include "base/lazy_instance.h"
10 #include "base/threading/thread_local.h"
11 #include "base/values.h"
12 #include "chrome/test/chromedriver/chrome/chrome.h"
13 #include "chrome/test/chromedriver/chrome/status.h"
14 #include "chrome/test/chromedriver/chrome/web_view.h"
15 #include "chrome/test/chromedriver/logging.h"
17 namespace {
19 base::LazyInstance<base::ThreadLocalPointer<Session> >
20 lazy_tls_session = LAZY_INSTANCE_INITIALIZER;
22 } // namespace
24 FrameInfo::FrameInfo(const std::string& parent_frame_id,
25 const std::string& frame_id,
26 const std::string& chromedriver_frame_id)
27 : parent_frame_id(parent_frame_id),
28 frame_id(frame_id),
29 chromedriver_frame_id(chromedriver_frame_id) {}
31 const base::TimeDelta Session::kDefaultPageLoadTimeout =
32 base::TimeDelta::FromMinutes(5);
34 Session::Session(const std::string& id)
35 : id(id),
36 quit(false),
37 detach(false),
38 force_devtools_screenshot(false),
39 sticky_modifiers(0),
40 mouse_position(0, 0),
41 page_load_timeout(kDefaultPageLoadTimeout),
42 auto_reporting_enabled(false) {}
44 Session::Session(const std::string& id, scoped_ptr<Chrome> chrome)
45 : id(id),
46 quit(false),
47 detach(false),
48 force_devtools_screenshot(false),
49 chrome(chrome.Pass()),
50 sticky_modifiers(0),
51 mouse_position(0, 0),
52 page_load_timeout(kDefaultPageLoadTimeout),
53 auto_reporting_enabled(false) {}
55 Session::~Session() {}
57 Status Session::GetTargetWindow(WebView** web_view) {
58 if (!chrome)
59 return Status(kNoSuchWindow, "no chrome started in this session");
61 Status status = chrome->GetWebViewById(window, web_view);
62 if (status.IsError())
63 status = Status(kNoSuchWindow, "target window already closed", status);
64 return status;
67 void Session::SwitchToTopFrame() {
68 frames.clear();
71 void Session::SwitchToParentFrame() {
72 if (!frames.empty())
73 frames.pop_back();
76 void Session::SwitchToSubFrame(const std::string& frame_id,
77 const std::string& chromedriver_frame_id) {
78 std::string parent_frame_id;
79 if (!frames.empty())
80 parent_frame_id = frames.back().frame_id;
81 frames.push_back(FrameInfo(parent_frame_id, frame_id, chromedriver_frame_id));
84 std::string Session::GetCurrentFrameId() const {
85 if (frames.empty())
86 return std::string();
87 return frames.back().frame_id;
90 std::vector<WebDriverLog*> Session::GetAllLogs() const {
91 std::vector<WebDriverLog*> logs;
92 for (ScopedVector<WebDriverLog>::const_iterator log = devtools_logs.begin();
93 log != devtools_logs.end();
94 ++log) {
95 logs.push_back(*log);
97 if (driver_log)
98 logs.push_back(driver_log.get());
99 return logs;
102 std::string Session::GetFirstBrowserError() const {
103 for (ScopedVector<WebDriverLog>::const_iterator it = devtools_logs.begin();
104 it != devtools_logs.end();
105 ++it) {
106 if ((*it)->type() == WebDriverLog::kBrowserType) {
107 std::string message = (*it)->GetFirstErrorMessage();
108 if (!message.empty())
109 return message;
112 return std::string();
115 Session* GetThreadLocalSession() {
116 return lazy_tls_session.Pointer()->Get();
119 void SetThreadLocalSession(scoped_ptr<Session> session) {
120 lazy_tls_session.Pointer()->Set(session.release());