blob: 055200ca3a3ec2abd021e33fa1345e3a9056c067 [file] [log] [blame]
jat@google.com23181962009-09-03 22:22:56 +00001/*
2 * Copyright 2009 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17#include "Preferences.h"
18#include "Debug.h"
19#include "AllowedConnections.h"
20
21#include "nsCOMPtr.h"
22#include "nsStringAPI.h"
23#include "nsISupportsImpl.h"
24#include "nsIObserver.h"
25#include "nsIPrefService.h"
26#include "nsIPrefBranch.h"
27#include "nsIPrefBranch2.h"
28#include "nsServiceManagerUtils.h"
29
jat@google.com268f9982009-11-06 21:34:25 +000030#define GWT_DEV_PREFS_PREFIX "gwt-dev-plugin."
31#define GWT_DEV_ACCESS_LIST "accessList"
jat@google.com23181962009-09-03 22:22:56 +000032
33NS_IMPL_ADDREF(Preferences)
34NS_IMPL_RELEASE(Preferences)
35NS_IMPL_QUERY_INTERFACE1(Preferences, nsIObserver)
36
37Preferences::Preferences() {
38 nsCOMPtr<nsIPrefService> prefService = do_GetService(
39 NS_PREFSERVICE_CONTRACTID);
40 if (!prefService) {
jat@google.com268f9982009-11-06 21:34:25 +000041 Debug::log(Debug::Error) << "Unable to get preference service"
42 << Debug::flush;
jat@google.com23181962009-09-03 22:22:56 +000043 return;
44 }
45 nsCOMPtr<nsIPrefBranch> branch;
jat@google.com268f9982009-11-06 21:34:25 +000046 prefService->GetBranch(GWT_DEV_PREFS_PREFIX, getter_AddRefs(branch));
jat@google.com23181962009-09-03 22:22:56 +000047 if (!branch) {
jat@google.com268f9982009-11-06 21:34:25 +000048 Debug::log(Debug::Error) << "Unable to get " GWT_DEV_PREFS_PREFIX
49 " preference branch" << Debug::flush;
jat@google.com23181962009-09-03 22:22:56 +000050 return;
51 }
52 prefs = do_QueryInterface(branch);
53 if (!prefs) {
54 Debug::log(Debug::Error) << "Unable to get nsIPrefBranch2" << Debug::flush;
55 return;
56 }
jat@google.com268f9982009-11-06 21:34:25 +000057 prefs->AddObserver(GWT_DEV_ACCESS_LIST, this, PR_FALSE);
jat@google.com23181962009-09-03 22:22:56 +000058 nsCString prefValue;
jat@google.com268f9982009-11-06 21:34:25 +000059 if (branch->GetCharPref(GWT_DEV_ACCESS_LIST, getter_Copies(prefValue))
60 == NS_OK) {
jat@google.com23181962009-09-03 22:22:56 +000061 loadAccessList(prefValue.get());
62 }
63}
64
65// implements nsIObserver
66NS_IMETHODIMP
67Preferences::Observe(nsISupports *aSubject, const char* aTopic,
68 const PRUnichar *aData) {
69 nsresult rv = NS_OK;
70 Debug::log(Debug::Spam) << "Preferences::Observe(subject="
71 << aSubject << ", topic=" << aTopic << ", data=" << aData << Debug::flush;
72 if (strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
73 return NS_ERROR_UNEXPECTED;
74 }
75 // TODO(jat): check preference name in aData if we ever add another one
76 nsCOMPtr<nsIPrefBranch> prefs(do_QueryInterface(aSubject, &rv));
77 NS_ENSURE_SUCCESS(rv, rv);
78 nsCString prefValue;
jat@google.com268f9982009-11-06 21:34:25 +000079 if (prefs->GetCharPref(GWT_DEV_ACCESS_LIST, getter_Copies(prefValue))
80 == NS_OK) {
jat@google.com23181962009-09-03 22:22:56 +000081 loadAccessList(prefValue.get());
82 }
83 return NS_OK;
84}
85
86void Preferences::addNewRule(const std::string& pattern, bool exclude) {
87 nsCString prefValue;
jat@google.com268f9982009-11-06 21:34:25 +000088 if (prefs->GetCharPref(GWT_DEV_ACCESS_LIST, getter_Copies(prefValue))
89 != NS_OK) {
jat@google.com23181962009-09-03 22:22:56 +000090 Debug::log(Debug::Error) << "Unable to retrieve access list preference"
91 << Debug::flush;
92 return;
93 }
94 // TODO(jat): see if the same rule already exists
95 std::string pref(prefValue.get());
96 if (pref.length() > 0) {
97 pref += ',';
98 }
99 if (exclude) {
100 pref += '!';
101 }
102 pref += pattern;
jat@google.com268f9982009-11-06 21:34:25 +0000103 if (prefs->SetCharPref(GWT_DEV_ACCESS_LIST, pref.c_str()) != NS_OK) {
jat@google.com23181962009-09-03 22:22:56 +0000104 Debug::log(Debug::Error) << "Unable to save modified access list preference"
105 << Debug::flush;
106 return;
107 }
108}
109
110void Preferences::loadAccessList(const char* prefValue) {
111 if (!prefValue) {
112 return;
113 }
114 Debug::log(Debug::Spam) << "loadFromAccessList(prefValue=" << prefValue << ")"
115 << Debug::flush;
116 AllowedConnections::initFromAccessList(prefValue);
117}
118
119Preferences::~Preferences() {
120 if (prefs) {
jat@google.com268f9982009-11-06 21:34:25 +0000121 prefs->RemoveObserver(GWT_DEV_ACCESS_LIST, this);
jat@google.com23181962009-09-03 22:22:56 +0000122 }
123}