diff options
author | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-02 05:49:10 +0000 |
---|---|---|
committer | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-02 05:49:10 +0000 |
commit | 5ad5ce7429291b39b4019433326e77f2600bab75 (patch) | |
tree | ea5f870cad00cfe1ec7254804d2d77d691fb332c | |
parent | db2f0dd078ccca67cd746d195dee5b68c7a27ea7 (diff) | |
download | chromium_src-5ad5ce7429291b39b4019433326e77f2600bab75.zip chromium_src-5ad5ce7429291b39b4019433326e77f2600bab75.tar.gz chromium_src-5ad5ce7429291b39b4019433326e77f2600bab75.tar.bz2 |
Add TableModel for geolocation settings.
BUG=39817,39820
TEST=manual
Review URL: http://codereview.chromium.org/1534013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43454 0039d316-1c4b-4281-b951-d872f2087c98
5 files changed, 215 insertions, 0 deletions
diff --git a/chrome/browser/geolocation/geolocation_content_settings_map.cc b/chrome/browser/geolocation/geolocation_content_settings_map.cc index 3f88fb2..9a07651 100755 --- a/chrome/browser/geolocation/geolocation_content_settings_map.cc +++ b/chrome/browser/geolocation/geolocation_content_settings_map.cc @@ -56,6 +56,15 @@ void GeolocationContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings); } +// static +std::string GeolocationContentSettingsMap::OriginToString(const GURL& origin) { + std::string port_component((origin.IntPort() != url_parse::PORT_UNSPECIFIED) ? + ":" + origin.port() : ""); + std::string scheme_component(!origin.SchemeIs(chrome::kHttpScheme) ? + origin.scheme() + chrome::kStandardSchemeSeparator : ""); + return scheme_component + origin.host() + port_component; +} + ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const { AutoLock auto_lock(lock_); return default_content_setting_; diff --git a/chrome/browser/geolocation/geolocation_content_settings_map.h b/chrome/browser/geolocation/geolocation_content_settings_map.h index df9d953..653771a 100755 --- a/chrome/browser/geolocation/geolocation_content_settings_map.h +++ b/chrome/browser/geolocation/geolocation_content_settings_map.h @@ -37,6 +37,10 @@ class GeolocationContentSettingsMap static void RegisterUserPrefs(PrefService* prefs); + // Return simplified string representing origin. If origin is using http or + // the standard port, those parts are not included in the output. + static std::string OriginToString(const GURL& origin); + // Returns the default setting. // // This may be called on any thread. diff --git a/chrome/browser/geolocation/geolocation_content_settings_table_model.cc b/chrome/browser/geolocation/geolocation_content_settings_table_model.cc new file mode 100644 index 0000000..16245f3 --- /dev/null +++ b/chrome/browser/geolocation/geolocation_content_settings_table_model.cc @@ -0,0 +1,137 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/geolocation/geolocation_content_settings_table_model.h" + +#include "app/l10n_util.h" +#include "app/table_model_observer.h" +#include "base/utf_string_conversions.h" +#include "grit/generated_resources.h" + +GeolocationContentSettingsTableModel::GeolocationContentSettingsTableModel( + GeolocationContentSettingsMap* map) + : map_(map), + observer_(NULL) { + GeolocationContentSettingsMap::AllOriginsSettings settings( + map_->GetAllOriginsSettings()); + GeolocationContentSettingsMap::AllOriginsSettings::const_iterator i; + for (i = settings.begin(); i != settings.end(); ++i) + AddEntriesForOrigin(i->first, i->second); +} + +bool GeolocationContentSettingsTableModel::CanRemoveException(int row) const { + const Entry& entry = entries_[row]; + return !(entry.origin == entry.embedding_origin && + static_cast<size_t>(row + 1) < entries_.size() && + entries_[row + 1].origin == entry.origin && + entry.setting == CONTENT_SETTING_DEFAULT); +} + +void GeolocationContentSettingsTableModel::RemoveException(int row) { + Entry& entry = entries_[row]; + bool next_has_same_origin = static_cast<size_t>(row + 1) < entries_.size() && + entries_[row + 1].origin == entry.origin; + bool has_children = entry.origin == entry.embedding_origin && + next_has_same_origin; + map_->SetContentSetting(entry.origin, entry.embedding_origin, + CONTENT_SETTING_DEFAULT); + if (has_children) { + entry.setting = CONTENT_SETTING_DEFAULT; + if (observer_) + observer_->OnItemsChanged(row, 1); + } else if (!next_has_same_origin && + row > 0 && + entries_[row - 1].origin == entry.origin && + entries_[row - 1].setting == CONTENT_SETTING_DEFAULT) { + // If we remove the last non-default child of a default parent, we should + // remove the parent too. + entries_.erase(entries_.begin() + row - 1, entries_.begin() + row + 1); + if (observer_) + observer_->OnItemsRemoved(row - 1, 2); + } else { + entries_.erase(entries_.begin() + row); + if (observer_) + observer_->OnItemsRemoved(row, 1); + } +} + +void GeolocationContentSettingsTableModel::RemoveAll() { + int old_row_count = RowCount(); + entries_.clear(); + map_->ResetToDefault(); + if (observer_) + observer_->OnItemsRemoved(0, old_row_count); +} + +int GeolocationContentSettingsTableModel::RowCount() { + return entries_.size(); +} + +std::wstring GeolocationContentSettingsTableModel::GetText(int row, + int column_id) { + const Entry& entry = entries_[row]; + if (column_id == IDS_EXCEPTIONS_HOSTNAME_HEADER) { + if (entry.origin == entry.embedding_origin) + return UTF8ToWide( + GeolocationContentSettingsMap::OriginToString(entry.origin)); + if (entry.embedding_origin.is_empty()) + return ASCIIToWide(" ") + + l10n_util::GetString(IDS_EXCEPTIONS_GEOLOCATION_EMBEDDED_ANY_OTHER); + return ASCIIToWide(" ") + + l10n_util::GetStringF(IDS_EXCEPTIONS_GEOLOCATION_EMBEDDED_ON_HOST, + UTF8ToWide(GeolocationContentSettingsMap::OriginToString( + entry.embedding_origin))); + } else if (column_id == IDS_EXCEPTIONS_ACTION_HEADER) { + switch (entry.setting) { + case CONTENT_SETTING_ALLOW: + return l10n_util::GetString(IDS_EXCEPTIONS_ALLOW_BUTTON); + case CONTENT_SETTING_BLOCK: + return l10n_util::GetString(IDS_EXCEPTIONS_BLOCK_BUTTON); + case CONTENT_SETTING_ASK: + return l10n_util::GetString(IDS_EXCEPTIONS_ASK_BUTTON); + case CONTENT_SETTING_DEFAULT: + return l10n_util::GetString(IDS_EXCEPTIONS_NOT_SET_BUTTON); + default: + NOTREACHED(); + } + } else { + NOTREACHED(); + } + return std::wstring(); +} + +void GeolocationContentSettingsTableModel::SetObserver( + TableModelObserver* observer) { + observer_ = observer; +} + +void GeolocationContentSettingsTableModel::AddEntriesForOrigin( + const GURL& origin, + const GeolocationContentSettingsMap::OneOriginSettings& settings) { + GeolocationContentSettingsMap::OneOriginSettings::const_iterator parent = + settings.find(origin); + + // Add the "parent" entry for the non-embedded setting. + entries_.push_back(Entry(origin, origin, + (parent == settings.end()) ? CONTENT_SETTING_DEFAULT : parent->second)); + + // Add the "children" for any embedded settings. + GeolocationContentSettingsMap::OneOriginSettings::const_iterator i; + for (i = settings.begin(); i != settings.end(); ++i) { + // Skip the non-embedded setting which we already added above. + if (i == parent) + continue; + + entries_.push_back(Entry(origin, i->first, i->second)); + } +} + +// static +GeolocationContentSettingsTableModel::Entry::Entry( + const GURL& in_origin, const GURL& in_embedding_origin, + ContentSetting in_setting) + : origin(in_origin), + embedding_origin(in_embedding_origin), + setting(in_setting) { +} diff --git a/chrome/browser/geolocation/geolocation_content_settings_table_model.h b/chrome/browser/geolocation/geolocation_content_settings_table_model.h new file mode 100644 index 0000000..e4d9d36 --- /dev/null +++ b/chrome/browser/geolocation/geolocation_content_settings_table_model.h @@ -0,0 +1,63 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_TABLE_MODEL_H_ +#define CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_TABLE_MODEL_H_ + +#include <vector> + +#include "app/table_model.h" +#include "chrome/browser/geolocation/geolocation_content_settings_map.h" +#include "chrome/common/content_settings.h" +#include "chrome/common/content_settings_types.h" + +class GeolocationContentSettingsTableModel : public TableModel { + public: + explicit GeolocationContentSettingsTableModel( + GeolocationContentSettingsMap* map); + + // Return whether the given row can be removed. A parent with setting of + // CONTENT_SETTING_DEFAULT can't be removed. + bool CanRemoveException(int row) const; + + // Removes the exception at the specified index from the map. If it is a + // parent, the row in model will be updated to have CONTENT_SETTING_DEFAULT. + // If it is the only child of a CONTENT_SETTING_DEFAULT parent, the parent + // will be removed from the model too. + void RemoveException(int row); + + // Removes all the exceptions from both the map and model. + void RemoveAll(); + + // TableModel overrides: + virtual int RowCount(); + virtual std::wstring GetText(int row, int column_id); + virtual void SetObserver(TableModelObserver* observer); + + private: + struct Entry { + Entry(const GURL& origin, + const GURL& embedding_origin, + ContentSetting setting); + + GURL origin; + GURL embedding_origin; + ContentSetting setting; + }; + + void AddEntriesForOrigin( + const GURL& origin, + const GeolocationContentSettingsMap::OneOriginSettings& settings); + + GeolocationContentSettingsMap* map_; + + typedef std::vector<Entry> EntriesVector; + EntriesVector entries_; + + TableModelObserver* observer_; + + DISALLOW_COPY_AND_ASSIGN(GeolocationContentSettingsTableModel); +}; + +#endif // CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_TABLE_MODEL_H_ diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 553c57b..e15ed73 100755 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1093,6 +1093,8 @@ 'browser/geolocation/geolocation_prefs.h', 'browser/geolocation/geolocation_content_settings_map.cc', 'browser/geolocation/geolocation_content_settings_map.h', + 'browser/geolocation/geolocation_content_settings_table_model.cc', + 'browser/geolocation/geolocation_content_settings_table_model.h', 'browser/geolocation/location_arbitrator.cc', 'browser/geolocation/location_arbitrator.h', 'browser/geolocation/location_provider.cc', |