Files.app: Stop to use file system URLs having externalfile:// scheme origin internally.
[chromium-blink-merge.git] / chrome / test / chromedriver / session.cc
blobd46918e0d0f0d6d6b0529ad87f6edf8aab11012d
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"
7 #include <list>
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"
18 namespace {
20 base::LazyInstance<base::ThreadLocalPointer<Session> >
21 lazy_tls_session = LAZY_INSTANCE_INITIALIZER;
23 } // namespace
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) {}
32 const base::TimeDelta Session::kDefaultPageLoadTimeout =
33 base::TimeDelta::FromMinutes(5);
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) {}
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) {}
56 Session::~Session() {}
58 Status Session::GetTargetWindow(WebView** web_view) {
59 if (!chrome)
60 return Status(kNoSuchWindow, "no chrome started in this session");
62 Status status = chrome->GetWebViewById(window, web_view);
63 if (status.IsError())
64 status = Status(kNoSuchWindow, "target window already closed", status);
65 return status;
68 void Session::SwitchToTopFrame() {
69 frames.clear();
72 void Session::SwitchToParentFrame() {
73 if (!frames.empty())
74 frames.pop_back();
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));
85 std::string Session::GetCurrentFrameId() const {
86 if (frames.empty())
87 return std::string();
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();
95 ++log) {
96 logs.push_back(*log);
98 if (driver_log)
99 logs.push_back(driver_log.get());
100 return logs;
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;
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());