Home | History | Annotate | Download | only in display
      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/chromeos/display/display_preferences.h"
      6 
      7 #include "ash/display/display_layout_store.h"
      8 #include "ash/display/display_manager.h"
      9 #include "ash/display/display_pref_util.h"
     10 #include "ash/shell.h"
     11 #include "base/prefs/pref_registry_simple.h"
     12 #include "base/prefs/pref_service.h"
     13 #include "base/prefs/scoped_user_pref_update.h"
     14 #include "base/strings/string16.h"
     15 #include "base/strings/string_number_conversions.h"
     16 #include "base/strings/string_split.h"
     17 #include "base/strings/string_util.h"
     18 #include "base/values.h"
     19 #include "chrome/browser/browser_process.h"
     20 #include "chrome/browser/chromeos/login/users/user_manager.h"
     21 #include "chrome/common/pref_names.h"
     22 #include "third_party/cros_system_api/dbus/service_constants.h"
     23 #include "ui/gfx/display.h"
     24 #include "ui/gfx/insets.h"
     25 #include "ui/gfx/screen.h"
     26 #include "url/url_canon.h"
     27 #include "url/url_util.h"
     28 
     29 namespace chromeos {
     30 namespace {
     31 
     32 const char kInsetsTopKey[] = "insets_top";
     33 const char kInsetsLeftKey[] = "insets_left";
     34 const char kInsetsBottomKey[] = "insets_bottom";
     35 const char kInsetsRightKey[] = "insets_right";
     36 
     37 // This kind of boilerplates should be done by base::JSONValueConverter but it
     38 // doesn't support classes like gfx::Insets for now.
     39 // TODO(mukai): fix base::JSONValueConverter and use it here.
     40 bool ValueToInsets(const base::DictionaryValue& value, gfx::Insets* insets) {
     41   DCHECK(insets);
     42   int top = 0;
     43   int left = 0;
     44   int bottom = 0;
     45   int right = 0;
     46   if (value.GetInteger(kInsetsTopKey, &top) &&
     47       value.GetInteger(kInsetsLeftKey, &left) &&
     48       value.GetInteger(kInsetsBottomKey, &bottom) &&
     49       value.GetInteger(kInsetsRightKey, &right)) {
     50     insets->Set(top, left, bottom, right);
     51     return true;
     52   }
     53   return false;
     54 }
     55 
     56 void InsetsToValue(const gfx::Insets& insets, base::DictionaryValue* value) {
     57   DCHECK(value);
     58   value->SetInteger(kInsetsTopKey, insets.top());
     59   value->SetInteger(kInsetsLeftKey, insets.left());
     60   value->SetInteger(kInsetsBottomKey, insets.bottom());
     61   value->SetInteger(kInsetsRightKey, insets.right());
     62 }
     63 
     64 std::string ColorProfileToString(ui::ColorCalibrationProfile profile) {
     65   switch (profile) {
     66     case ui::COLOR_PROFILE_STANDARD:
     67       return "standard";
     68     case ui::COLOR_PROFILE_DYNAMIC:
     69       return "dynamic";
     70     case ui::COLOR_PROFILE_MOVIE:
     71       return "movie";
     72     case ui::COLOR_PROFILE_READING:
     73       return "reading";
     74     case ui::NUM_COLOR_PROFILES:
     75       break;
     76   }
     77   NOTREACHED();
     78   return "";
     79 }
     80 
     81 ui::ColorCalibrationProfile StringToColorProfile(std::string value) {
     82   if (value == "standard")
     83     return ui::COLOR_PROFILE_STANDARD;
     84   else if (value == "dynamic")
     85     return ui::COLOR_PROFILE_DYNAMIC;
     86   else if (value == "movie")
     87     return ui::COLOR_PROFILE_MOVIE;
     88   else if (value == "reading")
     89     return ui::COLOR_PROFILE_READING;
     90   NOTREACHED();
     91   return ui::COLOR_PROFILE_STANDARD;
     92 }
     93 
     94 ash::DisplayManager* GetDisplayManager() {
     95   return ash::Shell::GetInstance()->display_manager();
     96 }
     97 
     98 // Returns true id the current user can write display preferences to
     99 // Local State.
    100 bool UserCanSaveDisplayPreference() {
    101   UserManager* user_manager = UserManager::Get();
    102   return user_manager->IsUserLoggedIn() &&
    103       (user_manager->IsLoggedInAsRegularUser() ||
    104        user_manager->IsLoggedInAsLocallyManagedUser() ||
    105        user_manager->IsLoggedInAsKioskApp());
    106 }
    107 
    108 void LoadDisplayLayouts() {
    109   PrefService* local_state = g_browser_process->local_state();
    110   ash::DisplayLayoutStore* layout_store = GetDisplayManager()->layout_store();
    111 
    112   const base::DictionaryValue* layouts = local_state->GetDictionary(
    113       prefs::kSecondaryDisplays);
    114   for (base::DictionaryValue::Iterator it(*layouts);
    115        !it.IsAtEnd(); it.Advance()) {
    116     ash::DisplayLayout layout;
    117     if (!ash::DisplayLayout::ConvertFromValue(it.value(), &layout)) {
    118       LOG(WARNING) << "Invalid preference value for " << it.key();
    119       continue;
    120     }
    121 
    122     if (it.key().find(",") != std::string::npos) {
    123       std::vector<std::string> ids;
    124       base::SplitString(it.key(), ',', &ids);
    125       int64 id1 = gfx::Display::kInvalidDisplayID;
    126       int64 id2 = gfx::Display::kInvalidDisplayID;
    127       if (!base::StringToInt64(ids[0], &id1) ||
    128           !base::StringToInt64(ids[1], &id2) ||
    129           id1 == gfx::Display::kInvalidDisplayID ||
    130           id2 == gfx::Display::kInvalidDisplayID) {
    131         continue;
    132       }
    133       layout_store->RegisterLayoutForDisplayIdPair(id1, id2, layout);
    134     }
    135   }
    136 }
    137 
    138 void LoadDisplayProperties() {
    139   PrefService* local_state = g_browser_process->local_state();
    140   const base::DictionaryValue* properties = local_state->GetDictionary(
    141       prefs::kDisplayProperties);
    142   for (base::DictionaryValue::Iterator it(*properties);
    143        !it.IsAtEnd(); it.Advance()) {
    144     const base::DictionaryValue* dict_value = NULL;
    145     if (!it.value().GetAsDictionary(&dict_value) || dict_value == NULL)
    146       continue;
    147     int64 id = gfx::Display::kInvalidDisplayID;
    148     if (!base::StringToInt64(it.key(), &id) ||
    149         id == gfx::Display::kInvalidDisplayID) {
    150       continue;
    151     }
    152     gfx::Display::Rotation rotation = gfx::Display::ROTATE_0;
    153     float ui_scale = 1.0f;
    154     const gfx::Insets* insets_to_set = NULL;
    155 
    156     int rotation_value = 0;
    157     if (dict_value->GetInteger("rotation", &rotation_value)) {
    158       rotation = static_cast<gfx::Display::Rotation>(rotation_value);
    159     }
    160     int ui_scale_value = 0;
    161     if (dict_value->GetInteger("ui-scale", &ui_scale_value))
    162       ui_scale = static_cast<float>(ui_scale_value) / 1000.0f;
    163 
    164     int width = 0, height = 0;
    165     dict_value->GetInteger("width", &width);
    166     dict_value->GetInteger("height", &height);
    167     gfx::Size resolution_in_pixels(width, height);
    168 
    169     gfx::Insets insets;
    170     if (ValueToInsets(*dict_value, &insets))
    171       insets_to_set = &insets;
    172 
    173     ui::ColorCalibrationProfile color_profile = ui::COLOR_PROFILE_STANDARD;
    174     std::string color_profile_name;
    175     if (dict_value->GetString("color_profile_name", &color_profile_name))
    176       color_profile = StringToColorProfile(color_profile_name);
    177     GetDisplayManager()->RegisterDisplayProperty(id,
    178                                                  rotation,
    179                                                  ui_scale,
    180                                                  insets_to_set,
    181                                                  resolution_in_pixels,
    182                                                  color_profile);
    183   }
    184 }
    185 
    186 void StoreDisplayLayoutPref(const ash::DisplayIdPair& pair,
    187                             const ash::DisplayLayout& display_layout) {
    188   std::string name =
    189       base::Int64ToString(pair.first) + "," + base::Int64ToString(pair.second);
    190 
    191   PrefService* local_state = g_browser_process->local_state();
    192   DictionaryPrefUpdate update(local_state, prefs::kSecondaryDisplays);
    193   base::DictionaryValue* pref_data = update.Get();
    194   scoped_ptr<base::Value> layout_value(new base::DictionaryValue());
    195   if (pref_data->HasKey(name)) {
    196     base::Value* value = NULL;
    197     if (pref_data->Get(name, &value) && value != NULL)
    198       layout_value.reset(value->DeepCopy());
    199   }
    200   if (ash::DisplayLayout::ConvertToValue(display_layout, layout_value.get()))
    201     pref_data->Set(name, layout_value.release());
    202 }
    203 
    204 void StoreCurrentDisplayLayoutPrefs() {
    205   if (!UserCanSaveDisplayPreference() ||
    206       GetDisplayManager()->num_connected_displays() < 2) {
    207     return;
    208   }
    209 
    210   ash::DisplayIdPair pair = GetDisplayManager()->GetCurrentDisplayIdPair();
    211   ash::DisplayLayout display_layout =
    212       GetDisplayManager()->layout_store()->GetRegisteredDisplayLayout(pair);
    213   StoreDisplayLayoutPref(pair, display_layout);
    214 }
    215 
    216 void StoreCurrentDisplayProperties() {
    217   ash::DisplayManager* display_manager = GetDisplayManager();
    218   PrefService* local_state = g_browser_process->local_state();
    219 
    220   DictionaryPrefUpdate update(local_state, prefs::kDisplayProperties);
    221   base::DictionaryValue* pref_data = update.Get();
    222 
    223   size_t num = display_manager->GetNumDisplays();
    224   for (size_t i = 0; i < num; ++i) {
    225     const gfx::Display& display = display_manager->GetDisplayAt(i);
    226     int64 id = display.id();
    227     ash::DisplayInfo info = display_manager->GetDisplayInfo(id);
    228 
    229     scoped_ptr<base::DictionaryValue> property_value(
    230         new base::DictionaryValue());
    231     property_value->SetInteger("rotation", static_cast<int>(info.rotation()));
    232     property_value->SetInteger(
    233         "ui-scale",
    234         static_cast<int>(info.configured_ui_scale() * 1000));
    235     ash::DisplayMode mode;
    236     if (!display.IsInternal() &&
    237         display_manager->GetSelectedModeForDisplayId(id, &mode) &&
    238         !mode.native) {
    239       property_value->SetInteger("width", mode.size.width());
    240       property_value->SetInteger("height", mode.size.height());
    241     }
    242     if (!info.overscan_insets_in_dip().empty())
    243       InsetsToValue(info.overscan_insets_in_dip(), property_value.get());
    244     if (info.color_profile() != ui::COLOR_PROFILE_STANDARD) {
    245       property_value->SetString(
    246           "color_profile_name", ColorProfileToString(info.color_profile()));
    247     }
    248     pref_data->Set(base::Int64ToString(id), property_value.release());
    249   }
    250 }
    251 
    252 typedef std::map<chromeos::DisplayPowerState, std::string>
    253     DisplayPowerStateToStringMap;
    254 
    255 const DisplayPowerStateToStringMap* GetDisplayPowerStateToStringMap() {
    256   // Don't save or retore ALL_OFF state. crbug.com/318456.
    257   static const DisplayPowerStateToStringMap* map = ash::CreateToStringMap(
    258       chromeos::DISPLAY_POWER_ALL_ON, "all_on",
    259       chromeos::DISPLAY_POWER_INTERNAL_OFF_EXTERNAL_ON,
    260       "internal_off_external_on",
    261       chromeos::DISPLAY_POWER_INTERNAL_ON_EXTERNAL_OFF,
    262       "internal_on_external_off");
    263   return map;
    264 }
    265 
    266 bool GetDisplayPowerStateFromString(const base::StringPiece& state,
    267                                     chromeos::DisplayPowerState* field) {
    268   if (ash::ReverseFind(GetDisplayPowerStateToStringMap(), state, field))
    269     return true;
    270   LOG(ERROR) << "Invalid display power state value:" << state;
    271   return false;
    272 }
    273 
    274 void StoreDisplayPowerState(DisplayPowerState power_state) {
    275   const DisplayPowerStateToStringMap* map = GetDisplayPowerStateToStringMap();
    276   DisplayPowerStateToStringMap::const_iterator iter = map->find(power_state);
    277   if (iter != map->end()) {
    278     PrefService* local_state = g_browser_process->local_state();
    279     local_state->SetString(prefs::kDisplayPowerState, iter->second);
    280   }
    281 }
    282 
    283 void StoreCurrentDisplayPowerState() {
    284   StoreDisplayPowerState(
    285       ash::Shell::GetInstance()->display_configurator()->power_state());
    286 }
    287 
    288 }  // namespace
    289 
    290 void RegisterDisplayLocalStatePrefs(PrefRegistrySimple* registry) {
    291   // Per-display preference.
    292   registry->RegisterDictionaryPref(prefs::kSecondaryDisplays);
    293   registry->RegisterDictionaryPref(prefs::kDisplayProperties);
    294   DisplayPowerStateToStringMap::const_iterator iter =
    295       GetDisplayPowerStateToStringMap()->find(chromeos::DISPLAY_POWER_ALL_ON);
    296   registry->RegisterStringPref(prefs::kDisplayPowerState, iter->second);
    297 }
    298 
    299 void StoreDisplayPrefs() {
    300   // Stores the power state regardless of the login status, because the power
    301   // state respects to the current status (close/open) of the lid which can be
    302   // changed in any situation. See crbug.com/285360
    303   StoreCurrentDisplayPowerState();
    304 
    305   // Do not store prefs when the confirmation dialog is shown.
    306   if (!UserCanSaveDisplayPreference() ||
    307       !ash::Shell::GetInstance()->ShouldSaveDisplaySettings()) {
    308     return;
    309   }
    310 
    311   StoreCurrentDisplayLayoutPrefs();
    312   StoreCurrentDisplayProperties();
    313 }
    314 
    315 void SetCurrentDisplayLayout(const ash::DisplayLayout& layout) {
    316   GetDisplayManager()->SetLayoutForCurrentDisplays(layout);
    317 }
    318 
    319 void LoadDisplayPreferences(bool first_run_after_boot) {
    320   LoadDisplayLayouts();
    321   LoadDisplayProperties();
    322   if (!first_run_after_boot) {
    323     PrefService* local_state = g_browser_process->local_state();
    324     // Restore DisplayPowerState:
    325     std::string value = local_state->GetString(prefs::kDisplayPowerState);
    326     chromeos::DisplayPowerState power_state;
    327     if (GetDisplayPowerStateFromString(value, &power_state)) {
    328       ash::Shell::GetInstance()->display_configurator()->SetInitialDisplayPower(
    329           power_state);
    330     }
    331   }
    332 }
    333 
    334 // Stores the display layout for given display pairs.
    335 void StoreDisplayLayoutPrefForTest(int64 id1,
    336                                    int64 id2,
    337                                    const ash::DisplayLayout& layout) {
    338   StoreDisplayLayoutPref(std::make_pair(id1, id2), layout);
    339 }
    340 
    341 // Stores the given |power_state|.
    342 void StoreDisplayPowerStateForTest(DisplayPowerState power_state) {
    343   StoreDisplayPowerState(power_state);
    344 }
    345 
    346 }  // namespace chromeos
    347