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