function ProcessSyncType(){UpdateSyncUI();var a=Xmarks.Settings.GetSyncInfo();if(a.toProcess.length==0)ResetSyncAction();else{var b=a.toProcess.shift();if(Xmarks.Settings.GetBool("sync-type-"+b.type,b.defaultEnabled))if(b.fn)b.fn(b,a);else{Xmarks.LogWrite("Skipping unused type (it has no fn attribute): "+b.type);b.status="success";a.processed.push(b);Xmarks.Settings.SetSyncInfo(a);ProcessSyncType()}else{Xmarks.LogWrite(b.type+" sync is currently disabled, skipping "+a.action);b.status="disabled";
a.processed.push(b);Xmarks.Settings.SetSyncInfo(a);ProcessSyncType()}}}function CheckLastErrorPop(){var a=localStorage.getItem("lasterrorpop"),b=(new Date).getTime();if(a!=null&&a>b-86400)return false;localStorage.setItem("lasterrorpop",b);return true}function ClearTryRepair(){localStorage.setItem("tryRepair","1")}function ShouldTryRepair(){if(localStorage.getItem("tryRepair")=="1")return false;localStorage.setItem("tryRepair","0");return true}
function ProcessSingleUpload(a,b){function c(){(new Request("POST",{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+"/download"},Xmarks.GetRequestArgs())).Start(function(d){if(ResponseOK(d)){var f=new a.ds,j=new Nodeset(f),i=new Commandset(d.commands);try{i.execute(j)}catch(h){Xmarks.LogWrite("Error processing download: "+JSON.stringify(h));a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_download_error");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType();
return}var g=new Nodeset(f);g.FetchFromNative(a.suffix,function(){j.Compare(g,function(k,l){function m(p){if(ResponseOK(p))CompleteTransaction(j,p,b,a,ProcessSyncType);else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_upload_profile_error");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}if(ResponseOK(k))if(l.set.length>0){var r=Xmarks.GetRequestArgs({baserev:d.toprev,commands:l.set});(new Request("POST",{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+
"/putchanges"},r)).Start(m)}else m({status:0,toprev:d.toprev});else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_compare_error");a+=" "+GetResponseDetails(k);b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}})})}else{a.status="skipped";a.statusMessage=chrome.i18n.getMessage("bg_upload_canceled");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}})}Xmarks.LogWrite("Starting upload: "+a.type);var e=Xmarks.Settings.GetUserHash();if(Xmarks.Settings.GetInt(e+
"profileid",0)!=0)c();else{Xmarks.LogWrite("Removing baseline so intermediate nodes aren't applied");Xmarks.Settings.DeleteUserBaselineData(a.suffix,function(d){function f(h,g){if(ResponseOK(h))(new Request("POST",{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+"/upload"},Xmarks.GetRequestArgs({commands:g.set}))).Start(j);else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_upload_commands_error");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}function j(h){if(ResponseOK(h))CompleteTransaction(i,
h,b,a,ProcessSyncType);else{if(h==2){a.status="skipped";a.statusMessage=chrome.i18n.getMessage("bg_upload_canceled")}else if(typeof h=="object"&&h.status==401){a.status="skipped";a.statusMessage=chrome.i18n.getMessage("bg_invalid_upload")}else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_upload_error")}b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}ResponseOK(d)||Xmarks.LogWrite("WARNING: Invalid response from Delete baseline data, uploaded data may get confused by baseline commands.  Attempting to continue.");
d=new a.ds;var i=new Nodeset(d);i.FetchFromNative(a.suffix,function(){Xmarks.LogWrite("Providing commands");i.ProvideCommandset(f)})})}}
function ProcessSingleDownload(a,b){Xmarks.LogWrite("Starting download: "+a.type);(new Request("POST",{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+"/download"},Xmarks.GetRequestArgs())).Start(function(c){function e(i){if(ResponseOK(i)){Xmarks.LogWrite("Local bookmarks cleared, flushing new bookmarks to native");f.FlushToNative(function(h){if(ResponseOK(h)){Xmarks.LogWrite("Bookmarks written to local, clearing baseline data");Xmarks.Settings.DeleteUserBaselineData(a.suffix,function(g){ResponseOK(g)||
Xmarks.LogWrite("WARNING: Unable to clear baseline, download may not finish correctly");Xmarks.LogWrite("Baseline data cleared, completeling transaction");CompleteTransaction(f,c,b,a,ProcessSyncType)})}else{a.status="error";a.statusMessage="Error writing local bookmarks";b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}})}else{a.status="error";a.statusMessage="Error clearing local bookmarks";b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}Xmarks.LogWrite("Handling download callback");
if(ResponseOK(c)){var d=new a.ds,f=new Nodeset(d);d=new Commandset(c.commands);try{d.execute(f);f.ClearNative(e)}catch(j){Xmarks.LogWrite("Error processing download: "+JSON.stringify(j));a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_error_download_response");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}else{if(c==2){a.status="skipped";a.statusMessage=chrome.i18n.getMessage("bg_download_canceled")}else if(typeof c=="object"&&c.status==401){a.status="skipped";
a.statusMessage=chrome.i18n.getMessage("bg_invalid_download")}else if(typeof c=="object"&&c.status==404){a.status="skipped";a.statusMessage=chrome.i18n.getMessage("bg_no_user_data")}else if(typeof c=="object"&&c.status==444){Xmarks.LogWrite("Server data does not exist, upload required");d=chrome.i18n.getMessage("bg_no_server_data",[a.type]);Xmarks.ShowDialog(d,{button1:chrome.i18n.getMessage("dialog_upload"),button2:chrome.i18n.getMessage("dialog_cancel")},function(i){Xmarks.LogWrite("MustUploadPromptCallback handling action: "+
i);if(i==chrome.i18n.getMessage("dialog_upload"))ProcessSingleUpload(a,b);else{Xmarks.LogWrite("Canceling download");a.status="skipped";a.statusMessage="No data on servers, you must do an upload";b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}});return}else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_error_download_response")}b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}})}
function ProcessSingleRepair(a,b){function c(i){if(ResponseOK(i)){Xmarks.LogWrite("Processing updates");var h=d.ProcessHashUpdates(i.updates),g=h[0];keep_cs=h[1];if(g.length){Xmarks.LogWrite("Resulting commandset: "+g.toJSONString());Xmarks.LogWrite("Reapply commandset: "+keep_cs.toJSONString());d.Execute(g);d.FlushToNative(function(l){if(self.ResponseOK(l)){Xmarks.LogWrite("Repair: Updates applied successfully");CompleteTransaction(d,i,b,a,k,true)}});var k=function(){if(keep_cs.length){Xmarks.LogWrite("Repair: Applying rebase operations");
var l=new a.ds;l=new Nodeset(l,d);l.Execute(keep_cs);l.FlushToNative(function(m){if(self.ResponseOK(m)){Xmarks.Settings.Set("last-change",(new Date).getTime());UpdateSyncUI()}})}a.status="success";ProcessSyncType()}}else{Xmarks.LogWrite("Nothing to see here; move along.");a.status="success";ProcessSyncType()}}else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_sync_stat");a.status="success";ProcessSyncType()}}Xmarks.LogWrite("Starting repair: "+a.type);var e=new a.ds,d=new Nodeset(e);
e.Repair();var f=["nid","url","name"];d.FetchFromNative(a.suffix,function(i){ResponseOK(i)&&d.HashTree(f,j)});var j=function(i){if(ResponseOK(i)){Xmarks.LogWrite("Repair: Fetch Diffs");d.ProvideHashes(function(h,g){ResponseOK(h)&&(new Request("POST",{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+"/getdiffs"},Xmarks.GetRequestArgs({attrs:f,nodeset:g}))).Start(c)})}}}
function ProcessSingleSync(a,b){function c(d){if(!ResponseOK(d)||!Xmarks.Settings.GetInt(Xmarks.Settings.GetUserHash()+"rev"+a.suffix,0))if(ResponseOK(d)){Xmarks.LogWrite("Starting first time sync");if(d.isreset)ProcessSingleUpload(a,b);else{a.mergeType="local";ProcessSingleMerge(a,b)}}else{if(d==2){a.status="skipped";a.statusMessage=chrome.i18n.getMessage("bg_sync_canceled")}else if(typeof d=="object"&&d.status==401){a.status="skipped";a.statusMessage=chrome.i18n.getMessage("bg_invalid_sync")}else{a.status=
"error";a.statusMessage=chrome.i18n.getMessage("bg_sync_stat")}b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}else(new Request("POST",{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+"/getchanges"},Xmarks.GetRequestArgs({baserev:Xmarks.Settings.Get(Xmarks.Settings.GetUserHash()+"rev"+a.suffix)}))).Start(e)}function e(d){function f(){if(Xmarks.Settings.GetBaseline(a.suffix))i();else{Xmarks.LogWrite("No baseline file found, pulling from server");var g=Xmarks.Settings.GetUserHash();
(new Request("POST",{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+"/download"},Xmarks.GetRequestArgs({rev:Xmarks.Settings.GetInt(g+"rev"+a.suffix)}))).Start(j)}}function j(g){if(ResponseOK(g)){var k=new a.ds;k=new Nodeset(k);g=new Commandset(g.commands);try{g.execute(k);Xmarks.Settings.SetBaseline(k,a.suffix,false,function(){Xmarks.LogWrite("Baseline updated, continuing sync");i()})}catch(l){Xmarks.LogWrite("Error processing download: "+JSON.stringify(l));a.status="error";a.statusMessage=
chrome.i18n.getMessage("bg_baseline_download_error");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_baseline_error");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}function i(){function g(m,r){function p(n,s){function t(){if(n.set.length>0){Xmarks.LogWrite("Sending local changes to server");this.request=new Request("POST",{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+"/putchanges"},
Xmarks.GetRequestArgs({baserev:d.toprev,commands:n.set}));this.request.Start(q)}else q({status:0,toprev:d.toprev})}function q(u){if(ResponseOK(u))CompleteTransaction(o,u,b,a,ProcessSyncType);else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_sync_changes_error");a.statusMessage+=GetResponseDetails(d);b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}if(s.set.length>0){Xmarks.LogWrite("Applying server changes to Chrome");s.execute(o);o.FlushToNative(t)}else t()}if(ResponseOK(m)){var o=
new Nodeset(k);o.FetchFromNative(a.suffix,function(n){if(ResponseOK(n)){Xmarks.LogWrite("Comparing baseline to local");(new Nodeset(k,Xmarks.Settings.GetBaseline(a.suffix))).Compare(o,function(s,t){if(ResponseOK(s))try{Xmarks.LogWrite("Synchronizing changes");Synchronize(Xmarks.Settings.GetBaseline(a.suffix),t,r,o,l,p);ClearTryRepair()}catch(q){console.error(q);if(typeof q=="number"){a.status="error";a.statusMessage="Error during sync ("+q+"); manually starting a download from the advanced tab should reset things."}else{a.status=
"error";a.statusMessage=chrome.i18n.getMessage("bg_unknown_sync_error")+": "+JSON.stringify(q)}b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType();var u=function(v){Xmarks.LogWrite("SyncErrorPromptCallback handling action: "+v);v==chrome.i18n.getMessage("dialog_download")&&HandleAction("download")};if(ShouldTryRepair())HandleAction("repair");else if(CheckLastErrorPop()){var w=chrome.i18n.getMessage("dialog_error_download");Xmarks.ShowDialog(w,{button1:chrome.i18n.getMessage("dialog_download"),
button2:chrome.i18n.getMessage("dialog_cancel")},u,null,290)}}else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_compare_error");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}})}else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_error_local_loaded");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}})}else{Xmarks.LogWrite("Error processing download: "+m);a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_error_build_mcs");
b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}Xmarks.LogWrite("Comparing baseline to server");var k=new a.ds,l=new Nodeset(k,d.continuous?Xmarks.Settings.GetBaseline(a.suffix):null);Xmarks.LogWrite("Created nodeset");if(d.commands.length>0){(new Commandset(d.commands)).execute(l);(new Nodeset(k,Xmarks.Settings.GetBaseline(a.suffix))).Compare(l,g)}else g(0,new Commandset)}if(ResponseOK(d))if(!d.toprev||!d.commands){Xmarks.LogWrite("Invalid sync response: "+JSON.stringify(d));
a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_invalid_sync_response");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}else if(!d.continuous&&!d.substituted){Xmarks.LogWrite("Response was not continuous, user needs to sync or download");h=chrome.i18n.getMessage("bg_not_continuous",[a.type]);Xmarks.ShowDialog(h,{button1:chrome.i18n.getMessage("dialog_sync"),button2:chrome.i18n.getMessage("dialog_download"),button3:chrome.i18n.getMessage("dialog_cancel")},function(g){Xmarks.LogWrite("DiscontinuityPromptCallback handling action: "+
g);if(g==chrome.i18n.getMessage("dialog_sync"))Xmarks.Settings.LoadLocalBaseline(a,f);else if(g==chrome.i18n.getMessage("dialog_download"))ProcessSingleDownload(a,b);else{Xmarks.LogWrite("Canceling sync");a.status="skipped";b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}})}else Xmarks.Settings.LoadLocalBaseline(a,f);else if(typeof d=="object"&&d.status==444){Xmarks.LogWrite("Server data does not exist, upload required");var h=chrome.i18n.getMessage("bg_no_server_data",[a.type]);
Xmarks.ShowDialog(h,{button1:chrome.i18n.getMessage("dialog_upload"),button2:chrome.i18n.getMessage("dialog_cancel")},function(g){Xmarks.LogWrite("MustUploadPromptCallback handling action: "+g);if(g==chrome.i18n.getMessage("dialog_upload"))ProcessSingleUpload(a,b);else{Xmarks.LogWrite("Canceling download");a.status="skipped";a.statusMessage="No data on servers, you must do an upload";downloadData.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}})}else if(typeof d=="object"&&d.status==
401){a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_invalid_sync");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType();UnauthenticateUser(function(){});AuthenticateUser(function(){HandleAction("sync")})}else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_sync_response");a.statusMessage+=GetResponseDetails(d);b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}Xmarks.Settings.GetInt(Xmarks.Settings.GetUserHash()+"rev"+a.suffix,0)?c(0):(new Request("POST",
{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+"/status"},Xmarks.GetRequestArgs())).Start(c)}
function ProcessSingleMerge(a,b){if(!a.mergeType)a.mergeType="local";Xmarks.LogWrite("Starting a merge: "+a.mergeType);(new Request("POST",{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+"/download"},Xmarks.GetRequestArgs())).Start(function(c){if(ResponseOK(c)){var e=new a.ds,d=new Nodeset(e),f=new Commandset(c.commands);try{f.execute(d)}catch(j){Xmarks.LogWrite("Error processing merge download: "+JSON.stringify(j));a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_download_error");
b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType();return}var i=new Nodeset(e);i.FetchFromNative(a.suffix,function(){function h(l){if(ResponseOK(l))d.Compare(k,g);else{a.status="error";a.statusMessage="Error merging node sets";b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}function g(l,m){function r(n){if(ResponseOK(n))p(n);else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_merge_putchanges");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);
ProcessSyncType()}}function p(n){k.FlushToNative(function(){CompleteTransaction(k,n,b,a,ProcessSyncType)})}if(ResponseOK(l))if(m.set.length>0){var o=Xmarks.GetRequestArgs({baserev:c.toprev,commands:m.set});(new Request("POST",{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+"/putchanges"},o)).Start(r)}else p(c);else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_error_build_mcs");b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}}var k;Xmarks.LogWrite("Merging nodesets");
if(a.mergeType=="local"){k=new Nodeset(e,i);k.Merge(d,h)}else{k=new Nodeset(e,d);k.Merge(i,h)}})}else{if(c==2){a.status="skipped";a.statusMessage=chrome.i18n.getMessage("bg_download_canceled")}else if(typeof c=="object"&&c.status==401){a.status="skipped";a.statusMessage=chrome.i18n.getMessage("bg_invalid_download")}else if(typeof c=="object"&&c.status==404){a.status="skipped";a.statusMessage=chrome.i18n.getMessage("bg_no_user_data")}else if(typeof c=="object"&&c.status==444){Xmarks.LogWrite("Server data does not exist, upload required");
f=chrome.i18n.getMessage("bg_no_server_data",[a.type]);Xmarks.ShowDialog(f,{button1:chrome.i18n.getMessage("dialog_upload"),button2:chrome.i18n.getMessage("dialog_cancel")},function(h){Xmarks.LogWrite("MustUploadPromptCallback handling action: "+h);if(h==chrome.i18n.getMessage("dialog_upload"))ProcessSingleUpload(a,b);else{Xmarks.LogWrite("Canceling download");a.status="skipped";a.statusMessage="No data on servers, you must do an upload";b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}});
return}else{a.status="error";a.statusMessage=chrome.i18n.getMessage("bg_error_download_response")}b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}})}
function ProcessSinglePurge(a,b){(new Request("POST",{host:Xmarks.Settings.Get("syncserver"),path:"/sync/"+a.type+"/purge"},Xmarks.GetRequestArgs({mode:"data"}))).Start(function(c){if(ResponseOK(c))CompleteTransaction(null,{toprev:0},b,a,ProcessSyncType);else{if(c==2){a.status="skipped";a.statusMessage="Purge canceled"}else if(typeof c=="object"&&c.status==401){a.status="skipped";a.statusMessage="Invalid purge response"}else{a.status="error";a.statusMessage="Purge error"}b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);
ProcessSyncType()}})}
function CompleteTransaction(a,b,c,e,d,f){if(Xmarks.Settings.Get("sync-error")){e.status="error";e.statusMessage=Xmarks.Settings.Get("sync-error");c.processed.push(e);d()}else{var j=Xmarks.Settings.GetUserHash();Xmarks.Settings.Get(j+"rev"+e.suffix,0)==0&&Xmarks.Settings.Set(j+"firstSync",true);Xmarks.Settings.Set(j+"rev"+e.suffix,b.toprev,true);Xmarks.Settings.SetBaseline(a,e.suffix,true,function(){Xmarks.Settings.Set(j+"lastsync",(new Date).getTime(),true);b.auth&&Xmarks.Settings.Set(j+"authtoken",
b.auth);Xmarks.LogWrite(e.type+" completed, marking successful");e.status="success";c.processed.push(e);Xmarks.Settings.SetSyncInfo(c);d()},f)}}
function HandleHeartbeat(){var a=Xmarks.Settings.GetInt(Xmarks.Settings.GetUserHash()+"lastsync",0);Xmarks.Settings.GetInt("last-change",0);var b=Xmarks.Settings.GetInt("autosync-freq",3600)*1E3,c=(new Date).getTime(),e=Xmarks.Settings.GetInt("app-start",0);if(e==0){Xmarks.Settings.Set("app-start",c);e=c}var d=0;d=a>0?c-a:c-e;a=Xmarks.Settings.Get("current-action",null);var f=Xmarks.Settings.GetInt("backoff-until",0);if(Xmarks.Settings.GetBool("enable-auto-sync","true")&&c>f&&!a)if(d>b){Xmarks.LogWrite("Triggering auto-sync");
Xmarks.Settings.Set("auto-sync",true);HandleAction("sync")}else{b=Xmarks.Settings.GetInt("last-tab-sync",0);d=Xmarks.Settings.GetInt("autotabsync-freq",600)*1E3;a=0;a=b>0?c-b:c-e;if(Xmarks.Settings.GetBool("sync-type-tabs","false")&&a>d){Xmarks.LogWrite("Triggering tab auto-sync");Xmarks.Settings.Set("auto-sync",true);WriteTabs({force:false},{toProcess:[],processed:[],action:"auto tab sync"})}}setTimeout(HandleHeartbeat,12E4)}
function UpdateSyncUI(){var a=GetStatusDetails();chrome.browserAction.setTitle({title:a.badgeTitle});if(a.badgeText&&a.badgeColor){chrome.browserAction.setBadgeBackgroundColor({color:a.badgeColor});chrome.browserAction.setBadgeText({text:a.badgeText})}else chrome.browserAction.setBadgeText({text:""});var b=chrome.self.getViews(),c;for(c in b)typeof b[c].HandleSyncStatusChange=="function"&&b[c].HandleSyncStatusChange(a)}
function SetNewAction(a){var b=Xmarks.Settings.Get("current-action");if(b){if(Xmarks.Settings.Get("auto-sync",false)==false){a=chrome.i18n.getMessage("bg_currently_busy",[b])+'<div style="font-size: 8pt; margin-top: 15px">'+chrome.i18n.getMessage("bg_possible_error",['<a href="http://helpdesk.xmarks.com/troubleshooting/chrome-troubleshooting/" target="_blank">',"</a>"])+"</div>";Xmarks.ShowDialog(a)}return false}Xmarks.Settings.Set("current-action",a);Xmarks.Settings.Remove("sync-error");return true}
function ResetSyncAction(){var a="",b=Xmarks.Settings.GetSyncInfo();if(b&&b.processed)for(var c=0;c<b.processed.length;c++)if(b.processed[c].status=="error"){Xmarks.Settings.Set("sync-error",b.processed[c].type+" error: "+b.processed[c].statusMessage);reportErrorServer(b.processed[c].type+" error: "+b.processed[c].statusMessage);a="error";break}else if(b.processed[c].status=="success")a="good";Xmarks.Settings.SetSyncInfo(null);if(!a)if(Xmarks.Settings.Get("saved-status",null)){a=Xmarks.Settings.Get("saved-status");
Xmarks.Settings.Remove("saved-status")}else a="good";if(a=="good"){Xmarks.Settings.Set("failcount",0);c=Xmarks.Settings.GetUserHash();if(Xmarks.Settings.GetBool(c+"firstSync","false")==true){Xmarks.Settings.Remove(c+"firstSync");if(!Xmarks.Settings.GetBool(c+"shownSuccess","true")){Xmarks.Settings.Set(c+"shownSuccess",true,true);ShowSuccessPage("success")}}}else{b=Xmarks.Settings.GetInt("backoff-until",0);var e=Xmarks.Settings.GetInt("failcount",0)+1;if(e>7)e=7;var d=4;for(c=1;c<e;c++)d+=d;c=(new Date).getTime()+
d*6E4;if(b>c)c=b;Xmarks.Settings.Set("failcount",e);Xmarks.Settings.Set("backoff-until",c)}Xmarks.Settings.Set("sync-status",a);Xmarks.Settings.Remove("current-action");Xmarks.Settings.Remove("auto-sync");CreateLocalChangeHandlers();if(a=Xmarks.Settings.GetPostSyncCallback()){a();Xmarks.Settings.SetPostSyncCallback(null)}}var lasterrortime=0;
function reportErrorServer(a){try{var b=(new Date).getTime(),c=parseInt(b/1E3);if(!(lasterrortime>c-5)){lasterrortime=c;var e=new XMLHttpRequest;e.onreadystatechange=function(){};e.onerror=function(){};e.open("POST","https://buy.xmarks.com/syncerr.php",true);e.setRequestHeader("Content-Type","application/x-www-form-urlencoded");e.send("msg="+encodeURIComponent(a)+"&username="+encodeURIComponent(Xmarks.Settings.Get("current-username"))+"&client=cr"+encodeURIComponent(Xmarks.Settings.Get("version")))}}catch(d){}}
function ResponseOK(a){if(typeof a=="number")if(a){Xmarks.LogWrite("Invalid response number: "+a);return false}else return true;else if(typeof a=="object")if(a.status){Xmarks.LogWrite("Invalid response object: "+JSON.stringify(a));var b=function(c){Xmarks.LogWrite("SyncErrorPromptCallback handling action: "+c);c==chrome.i18n.getMessage("dialog_download")&&HandleAction("download")};if(a.status!=-3&&CheckLastErrorPop()){a=chrome.i18n.getMessage("dialog_error_download");Xmarks.ShowDialog(a,{button1:chrome.i18n.getMessage("dialog_download"),
button2:chrome.i18n.getMessage("dialog_cancel")},b,null,290)}return false}else return true;else{Xmarks.LogWrite("Invalid unknown response: "+a);return false}}function GetResponseDetails(a){return typeof a=="number"?a?"Invalid response number: "+a:"ok":typeof a=="object"?a.status?"Invalid response object: "+JSON.stringify(a):"ok":"Invalid unknown response: "+a}
function WriteTabs(a,b){chrome.windows.getAll({populate:true},function(c){for(var e=[],d=0;d<c.length;d++){var f=c[d];if(!f.incognito){var j=[];if(f.tabs)for(var i=0;i<f.tabs.length;i++){var h=f.tabs[i];h.url.indexOf("http")==0&&j.push({url:h.url,title:h.title,icon:h.favIconUrl})}j.length&&e.push({tabs:j})}}if(a.force||Xmarks.Settings.WindowSetChanged(e)){Xmarks.LogWrite("Sending new tab state");(new Request("POST",{host:Xmarks.Settings.Get("cloudserver"),path:"/cloud/tabs/write"},{mymid:Xmarks.Settings.GetMachineId(),
data:{clientName:Xmarks.Settings.Get("sync-computer-name"),browserName:"chrome",windows:e}})).Start(function(g){if(g.status){Xmarks.LogWrite("Error sending tabs to server: "+g.status);a.result="error";a.statusMessage="Error sending tabs to server: "+g.status}else{Xmarks.Settings.SaveWindowSet(e);g=new Date;Xmarks.Settings.Set(Xmarks.Settings.GetUserHash()+"lastsync",g.getTime(),true);Xmarks.Settings.Set("last-tab-sync",g.getTime());a.status="success"}b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);
ProcessSyncType()})}else{Xmarks.LogWrite("Tab state is the same, not sending tab data");a.status="skipped";a.statusMessage="Open tabs unchanged";b.processed.push(a);Xmarks.Settings.SetSyncInfo(b);ProcessSyncType()}})}
function LoadRemoteTabData(a){(new Request("POST",{host:Xmarks.Settings.Get("cloudserver"),path:"/cloud/tabs/read"},{mymid:Xmarks.Settings.GetMachineId()})).Start(function(b){if(b.status||!b.mids)a(b.status||500);else{var c={};forEach(b.mids,function(e,d){if(d!=Xmarks.Settings.GetMachineId()){var f=e.clientName||"Unknown";f in c||(c[f]={mids:{}});c[f].mids[d]=e}});forEach(c,function(e){e.length=0;forEach(e.mids,function(d){d.length=0;forEach(d.windows,function(f){e.length+=f.tabs.length;d.length+=
f.tabs.length})})});a(0,c)}})}
function GetStatusDetails(){var a="good",b=Xmarks.Settings.GetSyncInfo();if(b)if(b.toProcess.length>0)a="active";else for(var c=0;c<b.processed.length;c++){if(b.processed[c].status=="error")a="error"}else if(Xmarks.Settings.Get("current-action"))a="active";else{a=Xmarks.Settings.GetInt("last-change",0);c=Xmarks.Settings.GetInt(Xmarks.Settings.GetUserHash()+"lastsync",0);a=a>c?"dirty":Xmarks.Settings.Get("sync-status","good")}c=chrome.i18n.getMessage("bg_syncing_successfully");var e=chrome.i18n.getMessage("browser_action_tooltip_default"),
d=null,f=null;if(a=="dirty"){e=c=chrome.i18n.getMessage("bg_changes_to_sync");d="!";f=[255,247,153,255]}else if(a=="error"){e=c=chrome.i18n.getMessage("bg_general_error");d="?";f=[207,0,22,255]}else if(a=="active"){e=c=chrome.i18n.getMessage("bg_sync_in_progress");d="!";f=[0,202,80,255]}return{syncStatus:a,syncText:c,badgeTitle:e,badgeText:d,badgeColor:f,syncInfo:b}};
