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::SwitchToParentFrame() {
77 void Session::SwitchToSubFrame(const std::string
& frame_id
,
78 const std::string
& chromedriver_frame_id
) {
79 std::string parent_frame_id
;
81 parent_frame_id
= frames
.back().frame_id
;
82 frames
.push_back(FrameInfo(parent_frame_id
, frame_id
, chromedriver_frame_id
));
85 std::string
Session::GetCurrentFrameId() const {
88 return frames
.back().frame_id
;
91 std::vector
<WebDriverLog
*> Session::GetAllLogs() const {
92 std::vector
<WebDriverLog
*> logs
;
93 for (ScopedVector
<WebDriverLog
>::const_iterator log
= devtools_logs
.begin();
94 log
!= devtools_logs
.end();
99 logs
.push_back(driver_log
.get());
103 std::string
Session::GetFirstBrowserError() const {
104 for (ScopedVector
<WebDriverLog
>::const_iterator it
= devtools_logs
.begin();
105 it
!= devtools_logs
.end();
107 if ((*it
)->type() == WebDriverLog::kBrowserType
) {
108 std::string message
= (*it
)->GetFirstErrorMessage();
109 if (!message
.empty())
113 return std::string();
116 Session
* GetThreadLocalSession() {
117 return lazy_tls_session
.Pointer()->Get();
120 void SetThreadLocalSession(scoped_ptr
<Session
> session
) {
121 lazy_tls_session
.Pointer()->Set(session
.release());