Home | History | Annotate | Download | only in chromedriver
      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 
     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 
     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       auto_reporting_enabled(false) {}
     44 
     45 Session::Session(const std::string& id, scoped_ptr<Chrome> chrome)
     46     : id(id),
     47       quit(false),
     48       detach(false),
     49       force_devtools_screenshot(false),
     50       chrome(chrome.Pass()),
     51       sticky_modifiers(0),
     52       mouse_position(0, 0),
     53       page_load_timeout(kDefaultPageLoadTimeout),
     54       auto_reporting_enabled(false) {}
     55 
     56 Session::~Session() {}
     57 
     58 Status Session::GetTargetWindow(WebView** web_view) {
     59   if (!chrome)
     60     return Status(kNoSuchWindow, "no chrome started in this session");
     61 
     62   Status status = chrome->GetWebViewById(window, web_view);
     63   if (status.IsError())
     64     status = Status(kNoSuchWindow, "target window already closed", status);
     65   return status;
     66 }
     67 
     68 void Session::SwitchToTopFrame() {
     69   frames.clear();
     70 }
     71 
     72 void Session::SwitchToParentFrame() {
     73   if (!frames.empty())
     74     frames.pop_back();
     75 }
     76 
     77 void Session::SwitchToSubFrame(const std::string& frame_id,
     78                                const std::string& chromedriver_frame_id) {
     79   std::string parent_frame_id;
     80   if (!frames.empty())
     81     parent_frame_id = frames.back().frame_id;
     82   frames.push_back(FrameInfo(parent_frame_id, frame_id, chromedriver_frame_id));
     83 }
     84 
     85 std::string Session::GetCurrentFrameId() const {
     86   if (frames.empty())
     87     return std::string();
     88   return frames.back().frame_id;
     89 }
     90 
     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();
     95        ++log) {
     96     logs.push_back(*log);
     97   }
     98   if (driver_log)
     99     logs.push_back(driver_log.get());
    100   return logs;
    101 }
    102 
    103 std::string Session::GetFirstBrowserError() const {
    104   for (ScopedVector<WebDriverLog>::const_iterator it = devtools_logs.begin();
    105        it != devtools_logs.end();
    106        ++it) {
    107     if ((*it)->type() == WebDriverLog::kBrowserType) {
    108       std::string message = (*it)->GetFirstErrorMessage();
    109       if (!message.empty())
    110         return message;
    111     }
    112   }
    113   return std::string();
    114 }
    115 
    116 Session* GetThreadLocalSession() {
    117   return lazy_tls_session.Pointer()->Get();
    118 }
    119 
    120 void SetThreadLocalSession(scoped_ptr<Session> session) {
    121   lazy_tls_session.Pointer()->Set(session.release());
    122 }
    123