Home | History | Annotate | Download | only in ui
      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/browser/ui/browser_window_state.h"
      6 
      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"
     18 
     19 namespace chrome {
     20 namespace {
     21 
     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;
     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 
     44 std::string GetWindowPlacementKey(const Browser* browser) {
     45   std::string name;
     46   if (!browser->app_name().empty()) {
     47     name = prefs::kBrowserWindowPlacement;
     48     name.append("_");
     49     name.append(browser->app_name());
     50   } else if (browser->is_type_popup()) {
     51     name = prefs::kBrowserWindowPlacementPopup;
     52   } else {
     53     name = prefs::kBrowserWindowPlacement;
     54   }
     55   return name;
     56 }
     57 
     58 bool ShouldSaveWindowPlacement(const Browser* browser) {
     59   switch (browser->type()) {
     60   case Browser::TYPE_TABBED:
     61     return true;
     62   case Browser::TYPE_POPUP:
     63     // Only save the window placement of popups if the window is from a
     64     // trusted source (v1 app, devtools, or system window).
     65     return browser->is_trusted_source();
     66   default:
     67     return false;
     68   }
     69 }
     70 
     71 void SaveWindowPlacement(const Browser* browser,
     72                          const gfx::Rect& bounds,
     73                          ui::WindowShowState show_state) {
     74   // Save to the session storage service, used when reloading a past session.
     75   // Note that we don't want to be the ones who cause lazy initialization of
     76   // the session service. This function gets called during initial window
     77   // showing, and we don't want to bring in the session service this early.
     78   SessionService* session_service =
     79       SessionServiceFactory::GetForProfileIfExisting(browser->profile());
     80   if (session_service)
     81     session_service->SetWindowBounds(browser->session_id(), bounds, show_state);
     82 }
     83 
     84 void GetSavedWindowBoundsAndShowState(const Browser* browser,
     85                                       gfx::Rect* bounds,
     86                                       ui::WindowShowState* show_state) {
     87   DCHECK(browser);
     88   DCHECK(bounds);
     89   DCHECK(show_state);
     90   *bounds = browser->override_bounds();
     91   WindowSizer::GetBrowserWindowBoundsAndShowState(browser->app_name(),
     92                                                   *bounds,
     93                                                   browser,
     94                                                   bounds,
     95                                                   show_state);
     96 
     97   const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
     98   bool record_mode = parsed_command_line.HasSwitch(switches::kRecordMode);
     99   bool playback_mode = parsed_command_line.HasSwitch(switches::kPlaybackMode);
    100   if (record_mode || playback_mode) {
    101     // In playback/record mode we always fix the size of the browser and
    102     // move it to (0,0).  The reason for this is two reasons:  First we want
    103     // resize/moves in the playback to still work, and Second we want
    104     // playbacks to work (as much as possible) on machines w/ different
    105     // screen sizes.
    106     *bounds = gfx::Rect(0, 0, 800, 600);
    107   }
    108 
    109   // The following options override playback/record.
    110   if (parsed_command_line.HasSwitch(switches::kWindowSize)) {
    111     std::string str =
    112         parsed_command_line.GetSwitchValueASCII(switches::kWindowSize);
    113     int width, height;
    114     if (ParseCommaSeparatedIntegers(str, &width, &height))
    115       bounds->set_size(gfx::Size(width, height));
    116   }
    117   if (parsed_command_line.HasSwitch(switches::kWindowPosition)) {
    118     std::string str =
    119         parsed_command_line.GetSwitchValueASCII(switches::kWindowPosition);
    120     int x, y;
    121     if (ParseCommaSeparatedIntegers(str, &x, &y))
    122       bounds->set_origin(gfx::Point(x, y));
    123   }
    124 }
    125 
    126 }  // namespace chrome
    127