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