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