Home | History | Annotate | Download | only in content_settings
      1 // Copyright 2013 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_settings/content_settings_usages_state.h"
      6 
      7 #include <string>
      8 
      9 #include "base/prefs/pref_service.h"
     10 #include "base/strings/string_piece.h"
     11 #include "base/strings/utf_string_conversions.h"
     12 #include "chrome/browser/content_settings/host_content_settings_map.h"
     13 #include "chrome/browser/profiles/profile.h"
     14 #include "chrome/common/pref_names.h"
     15 #include "content/public/browser/navigation_details.h"
     16 #include "content/public/browser/navigation_entry.h"
     17 #include "net/base/net_util.h"
     18 
     19 ContentSettingsUsagesState::ContentSettingsUsagesState(Profile* profile,
     20                                                        ContentSettingsType type)
     21     : profile_(profile),
     22       type_(type) {
     23 }
     24 
     25 ContentSettingsUsagesState::~ContentSettingsUsagesState() {
     26 }
     27 
     28 void ContentSettingsUsagesState::OnPermissionSet(
     29     const GURL& requesting_origin, bool allowed) {
     30   state_map_[requesting_origin] =
     31       allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
     32 }
     33 
     34 void ContentSettingsUsagesState::DidNavigate(
     35     const content::LoadCommittedDetails& details) {
     36   if (details.entry)
     37     embedder_url_ = details.entry->GetURL();
     38   if (state_map_.empty())
     39     return;
     40   if (!details.entry ||
     41       details.previous_url.GetOrigin() != details.entry->GetURL().GetOrigin()) {
     42     state_map_.clear();
     43     return;
     44   }
     45   // We're in the same origin, check if there's any icon to be displayed.
     46   unsigned int tab_state_flags = 0;
     47   GetDetailedInfo(NULL, &tab_state_flags);
     48   if (!(tab_state_flags & TABSTATE_HAS_ANY_ICON))
     49     state_map_.clear();
     50 }
     51 
     52 void ContentSettingsUsagesState::ClearStateMap() {
     53   state_map_.clear();
     54 }
     55 
     56 void ContentSettingsUsagesState::GetDetailedInfo(
     57     FormattedHostsPerState* formatted_hosts_per_state,
     58     unsigned int* tab_state_flags) const {
     59   DCHECK(tab_state_flags);
     60   DCHECK(embedder_url_.is_valid());
     61   ContentSetting default_setting =
     62       profile_->GetHostContentSettingsMap()->GetDefaultContentSetting(
     63           type_, NULL);
     64   std::set<std::string> formatted_hosts;
     65   std::set<std::string> repeated_formatted_hosts;
     66 
     67   // Build a set of repeated formatted hosts
     68   for (StateMap::const_iterator i(state_map_.begin());
     69        i != state_map_.end(); ++i) {
     70     std::string formatted_host = GURLToFormattedHost(i->first);
     71     if (!formatted_hosts.insert(formatted_host).second) {
     72       repeated_formatted_hosts.insert(formatted_host);
     73     }
     74   }
     75 
     76   for (StateMap::const_iterator i(state_map_.begin());
     77        i != state_map_.end(); ++i) {
     78     if (i->second == CONTENT_SETTING_ALLOW)
     79       *tab_state_flags |= TABSTATE_HAS_ANY_ALLOWED;
     80     if (formatted_hosts_per_state) {
     81       std::string formatted_host = GURLToFormattedHost(i->first);
     82       std::string final_formatted_host =
     83           repeated_formatted_hosts.find(formatted_host) ==
     84           repeated_formatted_hosts.end() ?
     85           formatted_host :
     86           i->first.spec();
     87       (*formatted_hosts_per_state)[i->second].insert(final_formatted_host);
     88     }
     89 
     90     const ContentSetting saved_setting =
     91         profile_->GetHostContentSettingsMap()->GetContentSetting(
     92             i->first, embedder_url_, type_, std::string());
     93     if (saved_setting != default_setting)
     94       *tab_state_flags |= TABSTATE_HAS_EXCEPTION;
     95     if (saved_setting != i->second)
     96       *tab_state_flags |= TABSTATE_HAS_CHANGED;
     97     if (saved_setting != CONTENT_SETTING_ASK)
     98       *tab_state_flags |= TABSTATE_HAS_ANY_ICON;
     99   }
    100 }
    101 
    102 std::string ContentSettingsUsagesState::GURLToFormattedHost(
    103     const GURL& url) const {
    104   base::string16 display_host;
    105   net::AppendFormattedHost(url,
    106       profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), &display_host);
    107   return base::UTF16ToUTF8(display_host);
    108 }
    109