blob: bc2232562c14dc50bb5771d6e21debbc35f43792 [file] [log] [blame]
[email protected]1f0b50b2012-06-22 20:37:161// 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/browser/ui/browser_window_state.h"
6
7#include "base/command_line.h"
[email protected]3853a4c2013-02-11 17:15:578#include "base/prefs/pref_service.h"
[email protected]3ea1b182013-02-08 22:38:419#include "base/strings/string_number_conversions.h"
[email protected]1f0b50b2012-06-22 20:37:1610#include "chrome/browser/defaults.h"
[email protected]1f0b50b2012-06-22 20:37:1611#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/sessions/session_service.h"
13#include "chrome/browser/sessions/session_service_factory.h"
14#include "chrome/browser/ui/browser.h"
[email protected]03639422012-06-28 19:03:3715#include "chrome/browser/ui/window_sizer/window_sizer.h"
[email protected]1f0b50b2012-06-22 20:37:1616#include "chrome/common/chrome_switches.h"
17#include "chrome/common/pref_names.h"
18
19namespace chrome {
20namespace {
21
22// Parse two comma-separated integers from str. Return true on success.
23bool ParseCommaSeparatedIntegers(const std::string& str,
24 int* ret_num1,
25 int* ret_num2) {
26 size_t num1_size = str.find_first_of(',');
27 if (num1_size == std::string::npos)
28 return false;
29
30 size_t num2_pos = num1_size + 1;
31 size_t num2_size = str.size() - num2_pos;
32 int num1, num2;
33 if (!base::StringToInt(str.substr(0, num1_size), &num1) ||
34 !base::StringToInt(str.substr(num2_pos, num2_size), &num2))
35 return false;
36
37 *ret_num1 = num1;
38 *ret_num2 = num2;
39 return true;
40}
41
42} // namespace
43
44std::string GetWindowPlacementKey(const Browser* browser) {
45 std::string name(prefs::kBrowserWindowPlacement);
46 if (!browser->app_name().empty()) {
47 name.append("_");
48 name.append(browser->app_name());
49 }
50 return name;
51}
52
53bool ShouldSaveWindowPlacement(const Browser* browser) {
54 switch (browser->type()) {
55 case Browser::TYPE_TABBED:
56 return true;
57 case Browser::TYPE_POPUP:
58 // Only save the window placement of popups if they are restored,
59 // or the window belongs to DevTools or an App.
60 return browser_defaults::kRestorePopups || browser->is_devtools() ||
61 browser->is_app();
[email protected]1f0b50b2012-06-22 20:37:1662 default:
63 return false;
64 }
65}
66
67void SaveWindowPlacement(const Browser* browser,
68 const gfx::Rect& bounds,
69 ui::WindowShowState show_state) {
70 // Save to the session storage service, used when reloading a past session.
71 // Note that we don't want to be the ones who cause lazy initialization of
72 // the session service. This function gets called during initial window
73 // showing, and we don't want to bring in the session service this early.
74 SessionService* session_service =
75 SessionServiceFactory::GetForProfileIfExisting(browser->profile());
76 if (session_service)
77 session_service->SetWindowBounds(browser->session_id(), bounds, show_state);
78}
79
[email protected]6a9b53a22012-10-09 02:43:3480void GetSavedWindowBoundsAndShowState(const Browser* browser,
81 gfx::Rect* bounds,
82 ui::WindowShowState* show_state) {
83 DCHECK(browser);
84 DCHECK(bounds);
85 DCHECK(show_state);
86 *bounds = browser->override_bounds();
87 WindowSizer::GetBrowserWindowBoundsAndShowState(browser->app_name(),
88 *bounds,
89 browser,
90 bounds,
91 show_state);
[email protected]1f0b50b2012-06-22 20:37:1692
93 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
94 bool record_mode = parsed_command_line.HasSwitch(switches::kRecordMode);
95 bool playback_mode = parsed_command_line.HasSwitch(switches::kPlaybackMode);
96 if (record_mode || playback_mode) {
97 // In playback/record mode we always fix the size of the browser and
98 // move it to (0,0). The reason for this is two reasons: First we want
99 // resize/moves in the playback to still work, and Second we want
100 // playbacks to work (as much as possible) on machines w/ different
101 // screen sizes.
[email protected]6a9b53a22012-10-09 02:43:34102 *bounds = gfx::Rect(0, 0, 800, 600);
[email protected]1f0b50b2012-06-22 20:37:16103 }
104
105 // The following options override playback/record.
106 if (parsed_command_line.HasSwitch(switches::kWindowSize)) {
107 std::string str =
108 parsed_command_line.GetSwitchValueASCII(switches::kWindowSize);
109 int width, height;
110 if (ParseCommaSeparatedIntegers(str, &width, &height))
[email protected]6a9b53a22012-10-09 02:43:34111 bounds->set_size(gfx::Size(width, height));
[email protected]1f0b50b2012-06-22 20:37:16112 }
113 if (parsed_command_line.HasSwitch(switches::kWindowPosition)) {
114 std::string str =
115 parsed_command_line.GetSwitchValueASCII(switches::kWindowPosition);
116 int x, y;
117 if (ParseCommaSeparatedIntegers(str, &x, &y))
[email protected]6a9b53a22012-10-09 02:43:34118 bounds->set_origin(gfx::Point(x, y));
[email protected]1f0b50b2012-06-22 20:37:16119 }
[email protected]1f0b50b2012-06-22 20:37:16120}
121
122} // namespace chrome