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.
4
5// Maps [requesting_origin, embedder] to content settings.  Written on the UI
6// thread and read on any thread.  One instance per profile. This is based on
7// HostContentSettingsMap but differs significantly in two aspects:
8// - It maps [requesting_origin.GetOrigin(), embedder.GetOrigin()] => setting
9//   rather than host => setting.
10// - It manages only Geolocation.
11
12#ifndef CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_MAP_H_
13#define CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_MAP_H_
14#pragma once
15
16#include <map>
17
18#include "base/basictypes.h"
19#include "base/memory/ref_counted.h"
20#include "chrome/browser/prefs/pref_change_registrar.h"
21#include "chrome/common/content_settings.h"
22#include "content/common/notification_observer.h"
23#include "content/common/notification_registrar.h"
24#include "googleurl/src/gurl.h"
25
26class ContentSettingsDetails;
27class DictionaryValue;
28class PrefService;
29class Profile;
30
31class GeolocationContentSettingsMap
32    : public base::RefCountedThreadSafe<GeolocationContentSettingsMap>,
33      public NotificationObserver {
34 public:
35  typedef std::map<GURL, ContentSetting> OneOriginSettings;
36  typedef std::map<GURL, OneOriginSettings> AllOriginsSettings;
37
38  explicit GeolocationContentSettingsMap(Profile* profile);
39
40  virtual ~GeolocationContentSettingsMap();
41
42  static void RegisterUserPrefs(PrefService* prefs);
43
44  // Returns the default setting.
45  //
46  // This should only be called on the UI thread.
47  ContentSetting GetDefaultContentSetting() const;
48
49  // Returns true if the content setting is managed (set by a policy).
50  bool IsDefaultContentSettingManaged() const;
51
52  // Returns a single ContentSetting which applies to the given |requesting_url|
53  // when embedded in a top-level page from |embedding_url|.  To determine the
54  // setting for a top-level page, as opposed to a frame embedded in a page,
55  // pass the page's URL for both arguments.
56  //
57  // This should only be called on the UI thread.
58  // Both arguments should be valid GURLs.
59  ContentSetting GetContentSetting(const GURL& requesting_url,
60                                   const GURL& embedding_url) const;
61
62  // Returns the settings for all origins with any non-default settings.
63  //
64  // This should only be called on the UI thread.
65  AllOriginsSettings GetAllOriginsSettings() const;
66
67  // Sets the default setting.
68  //
69  // This should only be called on the UI thread.
70  void SetDefaultContentSetting(ContentSetting setting);
71
72  // Sets the content setting for a particular (requesting origin, embedding
73  // origin) pair.  If the embedding origin is the same as the requesting
74  // origin, this represents the setting used when the requesting origin is
75  // itself the top-level page.  If |embedder| is the empty GURL, |setting|
76  // becomes the default setting for the requesting origin when embedded on any
77  // page that does not have an explicit setting.  Passing
78  // CONTENT_SETTING_DEFAULT for |setting| effectively removes that setting and
79  // allows future requests to return the all-embedders or global defaults (as
80  // applicable).
81  //
82  // This should only be called on the UI thread.  |requesting_url| should be
83  // a valid GURL, and |embedding_url| should be valid or empty.
84  void SetContentSetting(const GURL& requesting_url,
85                         const GURL& embedding_url,
86                         ContentSetting setting);
87
88  // Resets all settings.
89  //
90  // This should only be called on the UI thread.
91  void ResetToDefault();
92
93  // NotificationObserver implementation.
94  virtual void Observe(NotificationType type,
95                       const NotificationSource& source,
96                       const NotificationDetails& details);
97
98 private:
99  friend class base::RefCountedThreadSafe<GeolocationContentSettingsMap>;
100
101  // The default setting.
102  static const ContentSetting kDefaultSetting;
103
104  // Sends a CONTENT_SETTINGS_CHANGED notification.
105  void NotifyObservers(const ContentSettingsDetails& details);
106
107  void UnregisterObservers();
108
109  // Sets the fields of |one_origin_settings| based on the values in
110  // |dictionary|.
111  static void GetOneOriginSettingsFromDictionary(
112      const DictionaryValue* dictionary,
113      OneOriginSettings* one_origin_settings);
114
115  // The profile we're associated with.
116  Profile* profile_;
117
118  // Registrar to register for PREF_CHANGED notifications.
119  PrefChangeRegistrar prefs_registrar_;
120  NotificationRegistrar notification_registrar_;
121
122  DISALLOW_COPY_AND_ASSIGN(GeolocationContentSettingsMap);
123};
124
125#endif  // CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_MAP_H_
126