jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 268f998 | 2009-11-06 21:34:25 +0000 | [diff] [blame] | 30 | #define GWT_DEV_PREFS_PREFIX "gwt-dev-plugin." |
| 31 | #define GWT_DEV_ACCESS_LIST "accessList" |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 32 | |
| 33 | NS_IMPL_ADDREF(Preferences) |
| 34 | NS_IMPL_RELEASE(Preferences) |
| 35 | NS_IMPL_QUERY_INTERFACE1(Preferences, nsIObserver) |
| 36 | |
| 37 | Preferences::Preferences() { |
| 38 | nsCOMPtr<nsIPrefService> prefService = do_GetService( |
| 39 | NS_PREFSERVICE_CONTRACTID); |
| 40 | if (!prefService) { |
jat@google.com | 268f998 | 2009-11-06 21:34:25 +0000 | [diff] [blame] | 41 | Debug::log(Debug::Error) << "Unable to get preference service" |
| 42 | << Debug::flush; |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 43 | return; |
| 44 | } |
| 45 | nsCOMPtr<nsIPrefBranch> branch; |
jat@google.com | 268f998 | 2009-11-06 21:34:25 +0000 | [diff] [blame] | 46 | prefService->GetBranch(GWT_DEV_PREFS_PREFIX, getter_AddRefs(branch)); |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 47 | if (!branch) { |
jat@google.com | 268f998 | 2009-11-06 21:34:25 +0000 | [diff] [blame] | 48 | Debug::log(Debug::Error) << "Unable to get " GWT_DEV_PREFS_PREFIX |
| 49 | " preference branch" << Debug::flush; |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 50 | 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.com | 268f998 | 2009-11-06 21:34:25 +0000 | [diff] [blame] | 57 | prefs->AddObserver(GWT_DEV_ACCESS_LIST, this, PR_FALSE); |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 58 | nsCString prefValue; |
jat@google.com | 268f998 | 2009-11-06 21:34:25 +0000 | [diff] [blame] | 59 | if (branch->GetCharPref(GWT_DEV_ACCESS_LIST, getter_Copies(prefValue)) |
| 60 | == NS_OK) { |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 61 | loadAccessList(prefValue.get()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // implements nsIObserver |
| 66 | NS_IMETHODIMP |
| 67 | Preferences::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.com | 268f998 | 2009-11-06 21:34:25 +0000 | [diff] [blame] | 79 | if (prefs->GetCharPref(GWT_DEV_ACCESS_LIST, getter_Copies(prefValue)) |
| 80 | == NS_OK) { |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 81 | loadAccessList(prefValue.get()); |
| 82 | } |
| 83 | return NS_OK; |
| 84 | } |
| 85 | |
| 86 | void Preferences::addNewRule(const std::string& pattern, bool exclude) { |
| 87 | nsCString prefValue; |
jat@google.com | 268f998 | 2009-11-06 21:34:25 +0000 | [diff] [blame] | 88 | if (prefs->GetCharPref(GWT_DEV_ACCESS_LIST, getter_Copies(prefValue)) |
| 89 | != NS_OK) { |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 90 | 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.com | 268f998 | 2009-11-06 21:34:25 +0000 | [diff] [blame] | 103 | if (prefs->SetCharPref(GWT_DEV_ACCESS_LIST, pref.c_str()) != NS_OK) { |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 104 | Debug::log(Debug::Error) << "Unable to save modified access list preference" |
| 105 | << Debug::flush; |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void 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 | |
| 119 | Preferences::~Preferences() { |
| 120 | if (prefs) { |
jat@google.com | 268f998 | 2009-11-06 21:34:25 +0000 | [diff] [blame] | 121 | prefs->RemoveObserver(GWT_DEV_ACCESS_LIST, this); |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 122 | } |
| 123 | } |