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/version.h"
15 #include "chrome/test/chromedriver/chrome/web_view.h"
16 #include "chrome/test/chromedriver/logging.h"
20 base::LazyInstance
<base::ThreadLocalPointer
<Session
> >
21 lazy_tls_session
= LAZY_INSTANCE_INITIALIZER
;
25 FrameInfo::FrameInfo(const std::string
& parent_frame_id
,
26 const std::string
& frame_id
,
27 const std::string
& chromedriver_frame_id
)
28 : parent_frame_id(parent_frame_id
),
30 chromedriver_frame_id(chromedriver_frame_id
) {}
32 const base::TimeDelta
Session::kDefaultPageLoadTimeout
=
33 base::TimeDelta::FromMinutes(5);
35 Session::Session(const std::string
& id
)
39 force_devtools_screenshot(false),
42 page_load_timeout(kDefaultPageLoadTimeout
),
43 auto_reporting_enabled(false) {}
45 Session::Session(const std::string
& id
, scoped_ptr
<Chrome
> chrome
)
49 force_devtools_screenshot(false),
50 chrome(chrome
.Pass()),
53 page_load_timeout(kDefaultPageLoadTimeout
),
54 auto_reporting_enabled(false) {}
56 Session::~Session() {}
58 Status
Session::GetTargetWindow(WebView
** web_view
) {
60 return Status(kNoSuchWindow
, "no chrome started in this session");
62 Status status
= chrome
->GetWebViewById(window
, web_view
);
64 status
= Status(kNoSuchWindow
, "target window already closed", status
);
68 void Session::SwitchToTopFrame() {
72 void Session::SwitchToSubFrame(const std::string
& frame_id
,
73 const std::string
& chromedriver_frame_id
) {
74 std::string parent_frame_id
;
76 parent_frame_id
= frames
.back().frame_id
;
77 frames
.push_back(FrameInfo(parent_frame_id
, frame_id
, chromedriver_frame_id
));
80 std::string
Session::GetCurrentFrameId() const {
83 return frames
.back().frame_id
;
86 std::vector
<WebDriverLog
*> Session::GetAllLogs() const {
87 std::vector
<WebDriverLog
*> logs
;
88 for (ScopedVector
<WebDriverLog
>::const_iterator log
= devtools_logs
.begin();
89 log
!= devtools_logs
.end();
94 logs
.push_back(driver_log
.get());
98 std::string
Session::GetFirstBrowserError() const {
99 for (ScopedVector
<WebDriverLog
>::const_iterator it
= devtools_logs
.begin();
100 it
!= devtools_logs
.end();
102 if ((*it
)->type() == WebDriverLog::kBrowserType
) {
103 std::string message
= (*it
)->GetFirstErrorMessage();
104 if (!message
.empty())
108 return std::string();
111 Session
* GetThreadLocalSession() {
112 return lazy_tls_session
.Pointer()->Get();
115 void SetThreadLocalSession(scoped_ptr
<Session
> session
) {
116 lazy_tls_session
.Pointer()->Set(session
.release());