var debugtop=Error().lineNumber-5;
var DEBUG=false;
var Log=function(data){if(DEBUG)console.log(data);};
Log(" >> --- message-init_file.jss ---");
function isFunction(obj){
return (typeof obj=="function");};
function isBoolean(obj){
return (typeof obj=="boolean");};
function perror(e){
console.log("[JS error] message/gadget/init_file.js:"+(e.lineNumber-debugtop)+": "+e.name+": "+e.message);};
var NAME_UA_MANIFEST="ua.manifest";
var NAME_UA_PAGE_HEAD="NOTIFY_";
var NAME_SURFER="surfer";
var PRODUCT_ID_HOME="home";
var PRODUCT_ID_MESSAGE="0387878700140065";
var PATH_NVRAM_FILE="/apps_nv/data/"+PRODUCT_ID_MESSAGE+"/";
var APP_ID_MESSAGE="MESSAGE";
var ADT_ID_MESSAGE=APP_ID_MESSAGE+"-MSG";
var RETRY_NW_ERROR_SEC=30*1000;var RETRY_REQ_ERROR_SEC=1*60*60*1000;var REGIST_SUCCESS=0;
var REGIST_AUTHORIZED_ERROR=1;
var REGIST_FAILED=2;
var REGIST_NOT_INITIALIZED=3;
var REGIST_EXIT=-1;
var TIMEOUT_S=2*60*1000;var ERRTEXT_TOKEN_EXPIRED="EXPIRATIONEXPIREEXPIRATIONEXPIRE";
var UNREGIST_SEQUENCE_ID_START=100;
var gadget_util_path=APPLI_PATH+PRODUCT_ID_MESSAGE+"/gadget/gadget_util.js";
add_package_load_path("home_gadget_util",gadget_util_path);
var cloudGadget=null;
function _prepareCloudGadget(){
if(!cloudGadget){
require("home_gadget_util");
provide("home_gadget_util");
cloudGadget=new home_gadget_util.CloudGadget(APP_ID_MESSAGE);}
};
var nvram_file_mgr={};
nvram_file_mgr.readSync=function(arg){
arg=arg||{};
arg.async=false;
var ret={result:false,data:""};
try{
ret.data=apps_nvram_file.read(arg);
ret.ret=true;}catch(e){
console.log("nvram_file read unsuccessfull");}
return ret;};
nvram_file_mgr.writeSync=function(arg){
arg=arg||{};
arg.async=false;
var ret={result:false};
try{
apps_nvram_file.write(arg);
ret.result=true;}catch(e){
console.log("nvram_file write unsuccessfull");}
return ret;};
nvram_file_mgr.removeSync=function(arg){
arg=arg||{};
arg.async=false;
var ret={result:false,data:""};
try{
apps_nvram_file.remove(arg);
ret.result=true;}catch(e){
console.log("nvram_file remove unsuccessfull");}
return ret;};
nvram_file_mgr.getList=function(arg){
arg=arg||{};
var ret={result:false,list:[]};
try{
ret.list=apps_nvram_file.list(arg);
ret.result=true;}catch(e){
console.log("nvram_file list unsuccessfull");}
return ret;};
var user_profile_mgr={};
user_profile_mgr.getUserProfileList=function(){
var uprofile_list=[];
if(system.user_profile&&system.user_profile.supported){
try{
var uprofile=system.user_profile.get({mhc:{}});
if(uprofile&&uprofile.mhc&&uprofile.mhc.all){
uprofile_list=uprofile.mhc.all;}
}catch(e){
perror(e);}
}
return uprofile_list;};
user_profile_mgr.getUserProfile=function(id){
var uprofile={};
if(!system.user_profile||!system.user_profile.supported)
return uprofile;
try{
var _uprofile=system.user_profile.get({mhc:{id:id}});
if(_uprofile&&_uprofile.mhc){
uprofile=_uprofile;}
}catch(e){
perror(e);}
return uprofile;};
user_profile_mgr.getCurrentUserMHCUserkey=function(){
var profile=this.getUserProfile(null);
if(profile&&profile.mhc&&profile.mhc.mhc_userkey)
return profile.mhc.mhc_userkey;
return null;};
var user_agent_mgr={
user_list:[],
state_list:[],
unregist_trg_list:[],
pnt:0,
unregist_pnt:0,
ua_manifest_path:"",
register_endpoint_req_id:null,
regist_retry_timer:{},
badge_timer:{},
un_read_req_id:null,
un_read_checked:false
};
user_agent_mgr.init=function(){
addUserAgentHook("register",system.user_agent.REGISTER,user_agent_mgr.registerFunc);
addUserAgentHook("unregister",system.user_agent.UNREGISTER,user_agent_mgr.unregisterFunc);
addUserAgentHook("notification",system.user_agent.NOTIFICATION,user_agent_mgr.notificationFunc);
addUpdateUsrProfHook("message",ebus.app_upd.EV_USER_PROFILE_UPDATE,user_agent_mgr.updateUserFunc);};
user_agent_mgr.prepareUserList=function(){
var uprof_list=user_profile_mgr.getUserProfileList();
if(uprof_list&&uprof_list.length>0){
var uprof_list_len=uprof_list.indexOf(null);
if(uprof_list_len==-1)
uprof_list_len=uprof_list.length;
for(var i=0;i<uprof_list_len;i++){
var uprof=user_profile_mgr.getUserProfile(uprof_list[i]);
if(!uprof||!uprof.mhc)continue;
if(isString(uprof.mhc.mhc_userkey)&&uprof.mhc.mhc_userkey.length>0)
this.user_list.push(uprof);}
}
};
user_agent_mgr.checkNetworkEnable=function(cb){
cb=isFunction(cb)?cb:function(){};
if(isDisclaimerNeeded()){
cb(null);
return;}
if(!isNetworkAvailable()){
cb(false);
return;}
var check_cb=function(need_update){
switch(need_update){
case true:cb(null);
break;
case false:cb(true);
break;
case null:
default:cb(false);}
};
apputil.latestCheck(check_cb);};
user_agent_mgr.startRetryTimer=function(sec,cb){
var self=this;
this.stopRetryTimer();
sec=isNumber(sec)?sec:RETRY_NW_ERROR_SEC;
cb=isFunction(cb)?cb:function(){};
append_timer(this.regist_retry_timer,sec,function(obj,count){
self.stopRetryTimer();
cb();});};
user_agent_mgr.stopRetryTimer=function(){
delete_timer(this.regist_retry_timer);};
user_agent_mgr.startRegist=function(){
var self=this;
this.pnt=0;
var ret=this.createUAManifest();
if(!ret.result){
console.log(" create UA Manifest failed...");
return;}
this.ua_manifest_path=ret.file_path;
if(this.user_list.length<=0)
this.prepareUserList();
var nw_cb=function(res){
switch(res){
case true:self.registEndpoint();
break;
case false:self.retryRegist(RETRY_NW_ERROR_SEC);
break;
case null:
default:self.abortRegist();}
};
this.checkNetworkEnable(function(res){nw_cb(res);});};
user_agent_mgr.retryRegist=function(retry_sec){
var self=this;
this.startRetryTimer(
retry_sec,
function(){
self.startRegist();}
);};
user_agent_mgr.interruptRegist=function(){
this.user_list=[];
this.state_list.splice(0,this.state_list.length);
this.un_read_checked=false;
_prepareCloudGadget();
if(this.register_endpoint_req_id){
cloudGadget.cancelRequest(this.register_endpoint_req_id);
this.register_endpoint_req_id=null;}
if(this.un_read_req_id){
cloudGadget.cancelRequest(this.un_read_req_id);
this.un_read_req_id=null;}
this.stopRetryTimer();
this.startRegist();};
user_agent_mgr.finishRegist=function(){
this.startCheckUnRead();
this.startUnregist();};
user_agent_mgr.abortRegist=function(){
};
user_agent_mgr.reset=function(){
this.user_list=[];
this.state_list=[];
this.unregist_trg_list=[];
this.pnt=0;
this.unregist_pnt=0;
this.ua_manifest_path="";
_prepareCloudGadget();
if(this.register_endpoint_req_id){
cloudGadget.cancelRequest(this.register_endpoint_req_id);
this.register_endpoint_req_id=null;}
delete_timer(this.regist_retry_timer);
delete_timer(this.badge_timer);
if(this.un_read_req_id){
cloudGadget.cancelRequest(this.un_read_req_id);
this.un_read_req_id=null;}
this.un_read_checked=false;
delUserAgentHook("register");
delUserAgentHook("unregister");
delUserAgentHook("notification");
delUpdateUsrProfHook("message");};
user_agent_mgr.createUAManifest=function(){
var res={result:false};
var ret={result:false,file_path:""};
var filename=NAME_UA_MANIFEST;
var encoded_filename=encodeURIComponent(filename);
res=nvram_file_mgr.readSync({
filename:encoded_filename,finished:function(){},product_id:PRODUCT_ID_MESSAGE
});
if(res.result){ret.result=res.result;
ret.file_path=PATH_NVRAM_FILE+encoded_filename;
return ret;}
if(!isString(res.data)||res.data!=NAME_SURFER){
res=nvram_file_mgr.writeSync({
filename:encoded_filename,finished:function(){},product_id:PRODUCT_ID_MESSAGE,data:NAME_SURFER
});
if(res.result){
ret.result=res.result;
ret.file_path=PATH_NVRAM_FILE+encoded_filename;}
}
return ret;};
user_agent_mgr.createUAPage=function(application_id,mhc_userkey){
var res={result:false};
var ret={result:false,file_path:""};
if(!isString(application_id)||!isString(mhc_userkey))
return ret;
var filename=[NAME_UA_PAGE_HEAD,mhc_userkey].join("");
var encoded_filename=encodeURIComponent(filename);
Log(" >> filename : "+filename);
res=nvram_file_mgr.readSync({
filename:encoded_filename,finished:function(){},product_id:PRODUCT_ID_MESSAGE
});
if(res.result){ret.result=res.result;
ret.file_path=PATH_NVRAM_FILE+encoded_filename;
return ret;}
var data=[application_id,mhc_userkey].join("_");
var encoded_data=encodeURIComponent(data);
res=nvram_file_mgr.writeSync({
filename:encoded_filename,finished:function(){},product_id:PRODUCT_ID_MESSAGE,data:encoded_data
});
if(res.result){
ret.result=res.result;
ret.file_path=PATH_NVRAM_FILE+encoded_filename;}
return ret;};
user_agent_mgr.registEndpoint=function(){
var trg_user=this.user_list[this.pnt];
if(!trg_user){
this.finishRegist();
return;}
if(isBoolean(this.state_list[this.pnt])&&this.state_list[this.pnt]){
this.nextRegistEndpoint();return;}
var userkey=trg_user.mhc.mhc_userkey||"";
var res=this.createUAPage(PRODUCT_ID_MESSAGE,userkey);
if(res.result){
var page_path=res.file_path;
var ret=system.user_agent.register(this.pnt,this.ua_manifest_path,page_path);}else {this.nextRegistEndpoint();}
};
user_agent_mgr.nextRegistEndpoint=function(){
this.pnt++;
if(this.pnt<this.user_list.length){
this.registEndpoint();}else {
Log(" >> registEndpoint finish!!!");
this.finishRegist();}
};
user_agent_mgr.getRegistingUser=function(){
return this.user_list[this.pnt]||{};};
user_agent_mgr.saveRegistingUserState=function(bool){
this.state_list[this.pnt]=isBoolean(bool)?bool:null;};
user_agent_mgr.setToolbarMessagesBadge=function(){
var badge_list=[null,null,null,1,null,null];
var set_func=function(){
if(!this.HOME||!this.HOME.toolbar)
return false;
this.HOME.toolbar.setBadge(badge_list);
force_redraw();
return true;};
delete_timer(this.badge_timer);
if(!set_func()){
append_timer(this.badge_timer,100,function(obj,count){
if(set_func()||count>10){
delete_timer(obj);}
});}
};
user_agent_mgr.registerFunc=function(stg,obj,type,sequence_id,ret,str){
Log(" >> user_agent_mgr.registerFunc");
Log(" >>   sequence_id : "+sequence_id);
Log(" >>   ret : "+ret);
Log(" >>   str : "+str);
if(sequence_id>=UNREGIST_SEQUENCE_ID_START){
user_agent_mgr.unregistRegisterFunc(stg,obj,type,sequence_id,ret,str);
return;}
user_agent_mgr.register_endpoint_req_id=null;
var regist_state_cb=function(res){
switch(res){
case REGIST_SUCCESS:user_agent_mgr.saveRegistingUserState(true);
user_agent_mgr.nextRegistEndpoint();
break;
case REGIST_AUTHORIZED_ERROR:user_agent_mgr.saveRegistingUserState(false);
user_agent_mgr.nextRegistEndpoint();
break;
case REGIST_FAILED:user_agent_mgr.saveRegistingUserState(false);
user_agent_mgr.retryRegist(RETRY_REQ_ERROR_SEC);
break;
case REGIST_NOT_INITIALIZED:user_agent_mgr.saveRegistingUserState(false);
user_agent_mgr.retryRegist(RETRY_NW_ERROR_SEC);
break;
case REGIST_EXIT:
default:user_agent_mgr.abortRegist();}
};
var endpoint_cb=function(errcode){
user_agent_mgr.register_endpoint_req_id=null;
switch(errcode){
case 200:regist_state_cb(REGIST_SUCCESS);
break;
case 401:case 461:regist_state_cb(REGIST_AUTHORIZED_ERROR);
break;
case -28:case 503:regist_state_cb(REGIST_FAILED);
break;
default:
regist_state_cb(REGIST_EXIT);
break;}
};
var endpoint=str;
switch(ret){
case system.user_agent.E_OK:
case system.user_agent.E_DUPLICATE:
var trg_user=user_agent_mgr.getRegistingUser();
_prepareCloudGadget();
if(!trg_user.mhc.mhc_authtoken
||trg_user.mhc.mhc_authtoken==ERRTEXT_TOKEN_EXPIRED){
endpoint_cb(401);
return;}
cloudGadget.setAuthorizedToken(trg_user.mhc.mhc_authtoken);
Log(" >> trg_user : "+trg_user.toSource());
Log(" >> cloudGadget token : "+cloudGadget.getAuthorizedToken());
Log(" >> endpoint : "+endpoint);
user_agent_mgr.register_endpoint_req_id=cloudGadget.registerEndpoint(
APP_ID_MESSAGE,endpoint,TIMEOUT_S,endpoint_cb
);
if(isNumber(user_agent_mgr.register_endpoint_req_id)&&user_agent_mgr.register_endpoint_req_id<0)endpoint_cb(REGIST_EXIT);
break;
case system.user_agent.E_NOT_INITIALIZED:
regist_state_cb(REGIST_NOT_INITIALIZED);
break;
default:
console.log(" >> register failed......");
regist_state_cb(REGIST_EXIT);}
};
user_agent_mgr.notificationFunc=function(stg,obj,type,str){
Log(" >> notification");
Log(" >> str : "+str);
if(!isString(str)){
console.log(" >> notification str nothing");
return;}
user_agent_mgr.setToolbarMessagesBadge();};
user_agent_mgr.updateUserFunc=function(stg,obj,ev){
Log(" >> user_agent_mgr.updateUserFunc");
user_agent_mgr.interruptRegist();};
user_agent_mgr.startCheckUnRead=function(){
this.pnt=0;
this.un_read_checked=false;
this.un_read_req_id=null;
this.checkUnRead();};
user_agent_mgr.checkUnRead=function(){
var self=this;
var trg_user=this.user_list[this.pnt];
if(!trg_user){this.un_read_checked=true;
this.finishCheckUnRead();
return;}
if(this.un_read_checked){this.finishCheckUnRead();
return;}
var get_cb=function(data_count,errcode){
self.un_read_req_id=null;
switch(errcode){
case 200:if(data_count>0){
self.un_read_checked=true;
self.setToolbarMessagesBadge();}
break;
case 401:case 461:case -28:case 503:break;
default:
self.finishCheckUnRead();
return;}
self.pnt++;
self.checkUnRead();};
_prepareCloudGadget();
if(!trg_user.mhc.mhc_authtoken
||trg_user.mhc.mhc_authtoken==ERRTEXT_TOKEN_EXPIRED){
get_cb(0,401);
return;}
cloudGadget.setAuthorizedToken(trg_user.mhc.mhc_authtoken);
this.un_read_req_id=cloudGadget.getDataCount(
ADT_ID_MESSAGE,0,0,TIMEOUT_S,get_cb
);};
user_agent_mgr.finishCheckUnRead=function(){
Log(" >> user_agent_mgr.finishCheckUnRead");
if(this.state_list.some(function(element,index,array){return element==false;})){
Log(" >>   #1 failed... retry");
this.retryRegist(RETRY_REQ_ERROR_SEC);
return;};
if(!this.un_read_checked){Log(" >>   #2 failed... retry");
this.retryRegist(RETRY_REQ_ERROR_SEC);
return;}
Log(" >>   all ok finish!");};
user_agent_mgr.startUnregist=function(){
this.unregist_pnt=0;
this.prepareUnregist();
this.unregist();};
user_agent_mgr.prepareUnregist=function(){
var nv_list=[];
var res=nvram_file_mgr.getList({product_id:PRODUCT_ID_MESSAGE});
if(res.result){
nv_list=res.list.slice();}
var page_file_list=nv_list.filter(function(element){
return (isString(element)&&element.indexOf(NAME_UA_PAGE_HEAD)==0);});
if(page_file_list.length<=0){return;}
var user_list=[];
this.unregist_trg_list=[];
var prof_list=user_profile_mgr.getUserProfileList();
prof_list.forEach(function(v){
if(!isNumber(v))return;
var prof=user_profile_mgr.getUserProfile(v);
if(prof&&prof.mhc&&prof.mhc.mhc_userkey)
user_list.push(encodeURIComponent(prof.mhc.mhc_userkey));});
if(user_list.length<=0){this.unregist_trg_list=page_file_list.slice();}else {
var self=this;
page_file_list.forEach(function(v){for(var i=0;i<user_list.length;i++){
if(v.indexOf(user_list[i])!=-1){
return;}
}
self.unregist_trg_list.push(v);});}
Log(" >> this.unregist_trg_list : "+this.unregist_trg_list);};
user_agent_mgr.unregist=function(){
var trg_ua_file=this.unregist_trg_list[this.unregist_pnt];
if(!isString(trg_ua_file)){
this.finishUnregist();
return;}
var page_path=PATH_NVRAM_FILE+trg_ua_file;
var sequence_id=this.unregist_pnt+UNREGIST_SEQUENCE_ID_START;
var ret=system.user_agent.register(sequence_id,this.ua_manifest_path,page_path);};
user_agent_mgr.unregistRegisterFunc=function(stg,obj,type,sequence_id,ret,str){
var endpoint=str;
switch(ret){
case system.user_agent.E_OK:
case system.user_agent.E_DUPLICATE:
system.user_agent.unregister(sequence_id,endpoint);
break;
default:
console.log(" >> unregistRegister failed......");
user_agent_mgr.nextUnregist();}
};
user_agent_mgr.unregisterFunc=function(stg,obj,type,sequence_id,ret){
Log(" >> user_agent_mgr.unregisterFunc");
Log(" >>   ret : "+ret);
switch(ret){
case system.user_agent.E_OK:
user_agent_mgr.removeUnregistedUAPage();
break;
default:
console.log(" >> unregist failed......");
user_agent_mgr.nextUnregist();}
};
user_agent_mgr.removeUnregistedUAPage=function(){
var trg_ua_file=this.unregist_trg_list[this.unregist_pnt];
nvram_file_mgr.removeSync({filename:trg_ua_file,product_id:PRODUCT_ID_MESSAGE});
this.nextUnregist();};
user_agent_mgr.nextUnregist=function(){
this.unregist_pnt++;
this.unregist();};
user_agent_mgr.finishUnregist=function(){
Log(" >> finishUnregist!!!!!");};
function setDisclaimerUpdatedBadge(){
if(isDisclaimerNeeded()&&getMarketInfo()!=false){
user_agent_mgr.setToolbarMessagesBadge();}
};
append_timer({},1,function (obj){
delete_timer(obj);
setDisclaimerUpdatedBadge();
if(is_china){return;}
user_agent_mgr.init();
user_agent_mgr.startRegist();});
