1 // Copyright (c) 2011 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.
5 #include "chrome/browser/geolocation/geolocation_settings_state.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_piece.h"
11 #include "base/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"
19 GeolocationSettingsState::GeolocationSettingsState(Profile
* profile
)
23 GeolocationSettingsState::~GeolocationSettingsState() {
26 void GeolocationSettingsState::OnGeolocationPermissionSet(
27 const GURL
& requesting_origin
, bool allowed
) {
28 state_map_
[requesting_origin
] =
29 allowed
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
;
32 void GeolocationSettingsState::DidNavigate(
33 const content::LoadCommittedDetails
& details
) {
35 embedder_url_
= details
.entry
->GetURL();
36 if (state_map_
.empty())
39 details
.previous_url
.GetOrigin() != details
.entry
->GetURL().GetOrigin()) {
43 // We're in the same origin, check if there's any icon to be displayed.
44 unsigned int tab_state_flags
= 0;
45 GetDetailedInfo(NULL
, &tab_state_flags
);
46 if (!(tab_state_flags
& TABSTATE_HAS_ANY_ICON
))
50 void GeolocationSettingsState::ClearStateMap() {
54 void GeolocationSettingsState::GetDetailedInfo(
55 FormattedHostsPerState
* formatted_hosts_per_state
,
56 unsigned int* tab_state_flags
) const {
57 DCHECK(tab_state_flags
);
58 DCHECK(embedder_url_
.is_valid());
59 ContentSetting default_setting
=
60 profile_
->GetHostContentSettingsMap()->GetDefaultContentSetting(
61 CONTENT_SETTINGS_TYPE_GEOLOCATION
, NULL
);
62 std::set
<std::string
> formatted_hosts
;
63 std::set
<std::string
> repeated_formatted_hosts
;
65 // Build a set of repeated formatted hosts
66 for (StateMap::const_iterator
i(state_map_
.begin());
67 i
!= state_map_
.end(); ++i
) {
68 std::string formatted_host
= GURLToFormattedHost(i
->first
);
69 if (!formatted_hosts
.insert(formatted_host
).second
) {
70 repeated_formatted_hosts
.insert(formatted_host
);
74 for (StateMap::const_iterator
i(state_map_
.begin());
75 i
!= state_map_
.end(); ++i
) {
76 if (i
->second
== CONTENT_SETTING_ALLOW
)
77 *tab_state_flags
|= TABSTATE_HAS_ANY_ALLOWED
;
78 if (formatted_hosts_per_state
) {
79 std::string formatted_host
= GURLToFormattedHost(i
->first
);
80 std::string final_formatted_host
=
81 repeated_formatted_hosts
.find(formatted_host
) ==
82 repeated_formatted_hosts
.end() ?
85 (*formatted_hosts_per_state
)[i
->second
].insert(final_formatted_host
);
88 const ContentSetting saved_setting
=
89 profile_
->GetHostContentSettingsMap()->GetContentSetting(
92 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
94 if (saved_setting
!= default_setting
)
95 *tab_state_flags
|= TABSTATE_HAS_EXCEPTION
;
96 if (saved_setting
!= i
->second
)
97 *tab_state_flags
|= TABSTATE_HAS_CHANGED
;
98 if (saved_setting
!= CONTENT_SETTING_ASK
)
99 *tab_state_flags
|= TABSTATE_HAS_ANY_ICON
;
103 std::string
GeolocationSettingsState::GURLToFormattedHost(
104 const GURL
& url
) const {
105 string16 display_host
;
106 net::AppendFormattedHost(url
,
107 profile_
->GetPrefs()->GetString(prefs::kAcceptLanguages
), &display_host
);
108 return UTF16ToUTF8(display_host
);