@@ -33,28 +33,91 @@ define(function (require, exports, module) {
3333 "use strict" ;
3434
3535 var PreferenceStorage = require ( "preferences/PreferenceStorage" ) . PreferenceStorage ,
36- CollectionUtils = require ( "utils/CollectionUtils" ) ;
36+ FileUtils = require ( "file/FileUtils" ) ,
37+ ExtensionLoader = require ( "utils/ExtensionLoader" ) ,
38+ CollectionUtils = require ( "utils/CollectionUtils" ) ;
3739
40+ /**
41+ * The local storage ID
42+ * @const
43+ * @type {string }
44+ */
3845 var PREFERENCES_CLIENT_ID = "com.adobe.brackets.preferences" ;
39-
46+
47+ /**
48+ * The prefix used in the generated client ID
49+ * @const
50+ * @type {string }
51+ */
52+ var CLIENT_ID_PREFIX = "com.adobe.brackets." ;
53+
54+
4055 // Private Properties
4156 var preferencesKey ,
4257 prefStorage ,
4358 persistentStorage ,
59+ extensionPaths ,
4460 doLoadPreferences = false ;
45-
61+
62+
63+ /**
64+ * @private
65+ * Returns an array with the extension paths used in Brackets. The result is stored on a
66+ * private variable on the first call and used to return the value on the next calls.
67+ * @return {Array.<string> }
68+ */
69+ function _getExtensionPaths ( ) {
70+ if ( ! extensionPaths ) {
71+ var dirPath = FileUtils . getNativeBracketsDirectoryPath ( ) ;
72+
73+ extensionPaths = [
74+ dirPath + "/extensions/default/" ,
75+ dirPath + "/extensions/dev/" ,
76+ ExtensionLoader . getUserExtensionPath ( ) + "/"
77+ ] ;
78+ }
79+ return extensionPaths ;
80+ }
81+
82+ /**
83+ * This method returns a standardized ClientID for a given requireJS module object
84+ * @param {!{id: string, uri: string} } module - A requireJS module object
85+ * @return {string } The ClientID
86+ */
87+ function getClientID ( module ) {
88+ var paths = _getExtensionPaths ( ) ;
89+ var pathExp , pathUrl , clientID ;
90+
91+ paths . some ( function ( path ) {
92+ pathExp = new RegExp ( "^" + path ) ;
93+ if ( module . uri . match ( pathExp ) ) {
94+ pathUrl = path ;
95+ return true ;
96+ }
97+ } ) ;
98+
99+ if ( pathUrl ) {
100+ clientID = CLIENT_ID_PREFIX + module . uri . replace ( pathUrl , "" ) ;
101+ } else {
102+ clientID = CLIENT_ID_PREFIX + module . id ;
103+ }
104+ return clientID ;
105+ }
106+
46107 /**
47- * Retreive preferences data for the given clientID.
48- *
49- * @param {string } clientID Unique identifier
50- * @param {string } defaults Default preferences stored as JSON
108+ * Retreive the preferences data for the given clientID.
109+ * @param {string|{id: string, uri: string} } clientID - A unique identifier or a requireJS module object
110+ * @param {string } defaults - Default preferences stored as JSON
51111 * @return {PreferenceStorage }
52112 */
53113 function getPreferenceStorage ( clientID , defaults ) {
54- if ( ( clientID === undefined ) || ( clientID === null ) ) {
114+ if ( ! clientID || ( typeof clientID === "object" && ( ! clientID . id || ! clientID . uri ) ) ) {
55115 console . error ( "Invalid clientID" ) ;
56116 return ;
57117 }
118+ if ( typeof clientID === "object" ) {
119+ clientID = getClientID ( clientID ) ;
120+ }
58121
59122 var prefs = prefStorage [ clientID ] ;
60123
@@ -117,34 +180,21 @@ define(function (require, exports, module) {
117180 *
118181 * @param {!PreferenceStorage } newPrefs The new PreferenceStorage
119182 * @param {!string } oldID The id of the old PreferenceStorage
120- * @param {?obj } defaults The defaults to add
121183 */
122- function handleClientIdChange ( newPrefs , oldID , defaults ) {
123- var oldPrefs = getPreferenceStorage ( oldID ) ;
124-
125- defaults = defaults || { } ;
126-
127- if ( ! newPrefs . getValue ( "newClientID" ) ) {
128- var data = oldPrefs . getAllValues ( ) ;
129-
130- if ( $ . isEmptyObject ( data ) ) {
131- data = defaults ;
132- }
184+ function handleClientIdChange ( newPrefs , oldID ) {
185+ if ( prefStorage [ oldID ] ) {
186+ var oldPrefs = getPreferenceStorage ( oldID ) ;
133187
134- newPrefs . setAllValues ( data , false ) ;
135- newPrefs . setValue ( "newClientID" , true ) ;
188+ if ( ! newPrefs . getValue ( "newClientID" ) ) {
189+ var data = oldPrefs . getAllValues ( ) ;
190+
191+ if ( ! $ . isEmptyObject ( data ) ) {
192+ newPrefs . setAllValues ( data , false ) ;
193+ }
194+ newPrefs . setValue ( "newClientID" , true ) ;
195+ }
196+ delete prefStorage [ oldID ] ;
136197 }
137- delete prefStorage [ oldID ] ;
138- }
139-
140- /**
141- * This method returns a standardized ClientId for a given moduleId
142- *
143- * @param {!string } moduleId a given moduleId
144- * @return {string } the ClientId
145- */
146- function getClientId ( moduleId ) {
147- return "com.adobe.brackets." + moduleId ;
148198 }
149199
150200 // Check localStorage for a preferencesKey. Production and unit test keys
@@ -167,7 +217,7 @@ define(function (require, exports, module) {
167217 exports . getPreferenceStorage = getPreferenceStorage ;
168218 exports . savePreferences = savePreferences ;
169219 exports . handleClientIdChange = handleClientIdChange ;
170- exports . getClientId = getClientId ;
220+ exports . getClientID = getClientID ;
171221
172222 // Unit test use only
173223 exports . _reset = _reset ;
0 commit comments