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"
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"
19 base::LazyInstance
<base::ThreadLocalPointer
<Session
> >
20 lazy_tls_session
= LAZY_INSTANCE_INITIALIZER
;
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
),
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
)
38 force_devtools_screenshot(false),
41 page_load_timeout(kDefaultPageLoadTimeout
),
42 auto_reporting_enabled(false) {}
44 Session::Session(const std::string
& id
, scoped_ptr
<Chrome
> chrome
)
48 force_devtools_screenshot(false),
49 chrome(chrome
.Pass()),
52 page_load_timeout(kDefaultPageLoadTimeout
),
53 auto_reporting_enabled(false) {}
55 Session::~Session() {}
57 Status
Session::GetTargetWindow(WebView
** web_view
) {
59 return Status(kNoSuchWindow
, "no chrome started in this session");
61 Status status
= chrome
->GetWebViewById(window
, web_view
);
63 status
= Status(kNoSuchWindow
, "target window already closed", status
);
67 void Session::SwitchToTopFrame() {
71 void Session::SwitchToParentFrame() {
76 void Session::SwitchToSubFrame(const std::string
& frame_id
,
77 const std::string
& chromedriver_frame_id
) {
78 std::string parent_frame_id
;
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 {
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();
98 logs
.push_back(driver_log
.get());
102 std::string
Session::GetFirstBrowserError() const {
103 for (ScopedVector
<WebDriverLog
>::const_iterator it
= devtools_logs
.begin();
104 it
!= devtools_logs
.end();
106 if ((*it
)->type() == WebDriverLog::kBrowserType
) {
107 std::string message
= (*it
)->GetFirstErrorMessage();
108 if (!message
.empty())
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());