app_shell: Add version number in user agent
[chromium-blink-merge.git] / chrome / browser / ui / browser_window_state.cc
bloba27924109c284b43cc710c2106ae2beda0ee0a7b
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/browser/ui/browser_window_state.h"
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/defaults.h"
11 #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"
15 #include "chrome/browser/ui/window_sizer/window_sizer.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h"
19 namespace chrome {
20 namespace {
22 // Parse two comma-separated integers from str. Return true on success.
23 bool 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;
30 size_t num2_pos = num1_size + 1;
31 size_t num2_size = str.size() - num2_pos;
32 int num1 = 0;
33 int num2 = 0;
34 if (!base::StringToInt(str.substr(0, num1_size), &num1) ||
35 !base::StringToInt(str.substr(num2_pos, num2_size), &num2))
36 return false;
38 *ret_num1 = num1;
39 *ret_num2 = num2;
40 return true;
43 } // namespace
45 std::string GetWindowPlacementKey(const Browser* browser) {
46 if (browser->app_name().empty()) {
47 return browser->is_type_popup() ?
48 prefs::kBrowserWindowPlacementPopup : prefs::kBrowserWindowPlacement;
50 return std::string(prefs::kBrowserWindowPlacement) + "_" +
51 browser->app_name();
54 bool ShouldSaveWindowPlacement(const Browser* browser) {
55 // Only save the window placement of popups if the window is from a trusted
56 // source (v1 app, devtools, or system window).
57 return (browser->type() == Browser::TYPE_TABBED) ||
58 ((browser->type() == Browser::TYPE_POPUP) && browser->is_trusted_source());
61 void SaveWindowPlacement(const Browser* browser,
62 const gfx::Rect& bounds,
63 ui::WindowShowState show_state) {
64 // Save to the session storage service, used when reloading a past session.
65 // Note that we don't want to be the ones who cause lazy initialization of
66 // the session service. This function gets called during initial window
67 // showing, and we don't want to bring in the session service this early.
68 SessionService* session_service =
69 SessionServiceFactory::GetForProfileIfExisting(browser->profile());
70 if (session_service)
71 session_service->SetWindowBounds(browser->session_id(), bounds, show_state);
74 void GetSavedWindowBoundsAndShowState(const Browser* browser,
75 gfx::Rect* bounds,
76 ui::WindowShowState* show_state) {
77 DCHECK(browser);
78 DCHECK(bounds);
79 DCHECK(show_state);
80 *bounds = browser->override_bounds();
81 WindowSizer::GetBrowserWindowBoundsAndShowState(browser->app_name(),
82 *bounds,
83 browser,
84 bounds,
85 show_state);
87 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
88 bool record_mode = parsed_command_line.HasSwitch(switches::kRecordMode);
89 bool playback_mode = parsed_command_line.HasSwitch(switches::kPlaybackMode);
90 if (record_mode || playback_mode) {
91 // In playback/record mode we always fix the size of the browser and
92 // move it to (0,0). The reason for this is two reasons: First we want
93 // resize/moves in the playback to still work, and Second we want
94 // playbacks to work (as much as possible) on machines w/ different
95 // screen sizes.
96 *bounds = gfx::Rect(0, 0, 800, 600);
99 // The following options override playback/record.
100 if (parsed_command_line.HasSwitch(switches::kWindowSize)) {
101 std::string str =
102 parsed_command_line.GetSwitchValueASCII(switches::kWindowSize);
103 int width, height;
104 if (ParseCommaSeparatedIntegers(str, &width, &height))
105 bounds->set_size(gfx::Size(width, height));
107 if (parsed_command_line.HasSwitch(switches::kWindowPosition)) {
108 std::string str =
109 parsed_command_line.GetSwitchValueASCII(switches::kWindowPosition);
110 int x, y;
111 if (ParseCommaSeparatedIntegers(str, &x, &y))
112 bounds->set_origin(gfx::Point(x, y));
116 } // namespace chrome