• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/test/chromedriver/session.h"
6 
7 #include <list>
8 
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"
17 
18 namespace {
19 
20 base::LazyInstance<base::ThreadLocalPointer<Session> >
21     lazy_tls_session = LAZY_INSTANCE_INITIALIZER;
22 
23 }  // namespace
24 
FrameInfo(const std::string & parent_frame_id,const std::string & frame_id,const std::string & chromedriver_frame_id)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),
29       frame_id(frame_id),
30       chromedriver_frame_id(chromedriver_frame_id) {}
31 
32 const base::TimeDelta Session::kDefaultPageLoadTimeout =
33     base::TimeDelta::FromMinutes(5);
34 
Session(const std::string & id)35 Session::Session(const std::string& id)
36     : id(id),
37       quit(false),
38       detach(false),
39       force_devtools_screenshot(false),
40       sticky_modifiers(0),
41       mouse_position(0, 0),
42       page_load_timeout(kDefaultPageLoadTimeout) {}
43 
Session(const std::string & id,scoped_ptr<Chrome> chrome)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 
~Session()54 Session::~Session() {}
55 
GetTargetWindow(WebView ** web_view)56 Status Session::GetTargetWindow(WebView** web_view) {
57   if (!chrome)
58     return Status(kNoSuchWindow, "no chrome started in this session");
59 
60   Status status = chrome->GetWebViewById(window, web_view);
61   if (status.IsError())
62     status = Status(kNoSuchWindow, "target window already closed", status);
63   return status;
64 }
65 
SwitchToTopFrame()66 void Session::SwitchToTopFrame() {
67   frames.clear();
68 }
69 
SwitchToSubFrame(const std::string & frame_id,const std::string & chromedriver_frame_id)70 void Session::SwitchToSubFrame(const std::string& frame_id,
71                                const std::string& chromedriver_frame_id) {
72   std::string parent_frame_id;
73   if (!frames.empty())
74     parent_frame_id = frames.back().frame_id;
75   frames.push_back(FrameInfo(parent_frame_id, frame_id, chromedriver_frame_id));
76 }
77 
GetCurrentFrameId() const78 std::string Session::GetCurrentFrameId() const {
79   if (frames.empty())
80     return std::string();
81   return frames.back().frame_id;
82 }
83 
GetAllLogs() const84 std::vector<WebDriverLog*> Session::GetAllLogs() const {
85   std::vector<WebDriverLog*> logs;
86   for (ScopedVector<WebDriverLog>::const_iterator log = devtools_logs.begin();
87        log != devtools_logs.end();
88        ++log) {
89     logs.push_back(*log);
90   }
91   if (driver_log)
92     logs.push_back(driver_log.get());
93   return logs;
94 }
95 
GetThreadLocalSession()96 Session* GetThreadLocalSession() {
97   return lazy_tls_session.Pointer()->Get();
98 }
99 
SetThreadLocalSession(scoped_ptr<Session> session)100 void SetThreadLocalSession(scoped_ptr<Session> session) {
101   lazy_tls_session.Pointer()->Set(session.release());
102 }
103