Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2010 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/content_setting_combo_model.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/string16.h"
      9 #include "chrome/common/chrome_switches.h"
     10 #include "grit/generated_resources.h"
     11 #include "ui/base/l10n/l10n_util.h"
     12 
     13 namespace {
     14 
     15 // The settings shown in the combobox if show_session_ is false;
     16 const ContentSetting kNoSessionSettings[] = { CONTENT_SETTING_ALLOW,
     17                                               CONTENT_SETTING_BLOCK };
     18 
     19 // The settings shown in the combobox if show_session_ is true;
     20 const ContentSetting kSessionSettings[] = { CONTENT_SETTING_ALLOW,
     21                                             CONTENT_SETTING_SESSION_ONLY,
     22                                             CONTENT_SETTING_BLOCK };
     23 
     24 // The settings shown in the combobox for plug-ins;
     25 const ContentSetting kAskSettings[] = { CONTENT_SETTING_ALLOW,
     26                                         CONTENT_SETTING_ASK,
     27                                         CONTENT_SETTING_BLOCK };
     28 
     29 }  // namespace
     30 
     31 ContentSettingComboModel::ContentSettingComboModel(ContentSettingsType type)
     32     : content_type_(type) {
     33 }
     34 
     35 ContentSettingComboModel::~ContentSettingComboModel() {
     36 }
     37 
     38 int ContentSettingComboModel::GetItemCount() {
     39   if (content_type_ == CONTENT_SETTINGS_TYPE_PLUGINS &&
     40       CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableClickToPlay))
     41     return arraysize(kAskSettings);
     42   if (content_type_ == CONTENT_SETTINGS_TYPE_COOKIES)
     43     return arraysize(kSessionSettings);
     44   return arraysize(kNoSessionSettings);
     45 }
     46 
     47 string16 ContentSettingComboModel::GetItemAt(int index) {
     48   switch (SettingForIndex(index)) {
     49     case CONTENT_SETTING_ALLOW:
     50       return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ALLOW_BUTTON);
     51     case CONTENT_SETTING_BLOCK:
     52       return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON);
     53     case CONTENT_SETTING_ASK:
     54       return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ASK_BUTTON);
     55     case CONTENT_SETTING_SESSION_ONLY:
     56       return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_SESSION_ONLY_BUTTON);
     57     default:
     58       NOTREACHED();
     59   }
     60   return string16();
     61 }
     62 
     63 ContentSetting ContentSettingComboModel::SettingForIndex(int index) {
     64   if (content_type_ == CONTENT_SETTINGS_TYPE_PLUGINS &&
     65       CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableClickToPlay))
     66     return kAskSettings[index];
     67   if (content_type_ == CONTENT_SETTINGS_TYPE_COOKIES)
     68     return kSessionSettings[index];
     69   return kNoSessionSettings[index];
     70 }
     71 
     72 int ContentSettingComboModel::IndexForSetting(ContentSetting setting) {
     73   for (int i = 0; i < GetItemCount(); ++i)
     74     if (SettingForIndex(i) == setting)
     75       return i;
     76   NOTREACHED();
     77   return 0;
     78 }
     79 
     80