From 07c9ac66f7c66d6e7dabff7d9afe81954e9ac545 Mon Sep 17 00:00:00 2001 From: itozyun Date: Wed, 20 May 2015 20:03:57 +0900 Subject: [PATCH] Version 0.5.151, fix X.Timer.remove & X.OAuth2 is working. --- 0.6.x/js/01_core/09_XPair.js | 3 +- 0.6.x/js/01_core/14_XTimer.js | 6 ++-- 0.6.x/js/06_net/00_XNet.js | 19 +++++++----- 0.6.x/js/06_net/05_XXHRGadget.js | 54 ++++++++++++++++++++++++++------- 0.6.x/js/06_net/10_XOAuth2.js | 64 ++++++++++++++++++++++------------------ 0.6.x/js/main.js | 2 -- 6 files changed, 96 insertions(+), 52 deletions(-) diff --git a/0.6.x/js/01_core/09_XPair.js b/0.6.x/js/01_core/09_XPair.js index 120258c..14cd864 100644 --- a/0.6.x/js/01_core/09_XPair.js +++ b/0.6.x/js/01_core/09_XPair.js @@ -9,6 +9,8 @@ X[ 'Pair' ] = { var X_Pair_SIZE = 1000, X_Pair_KEY_STORE_LIST = [[]], X_Pair_PAIR_STORE_LIST = [[]], + X_Pair_noChashe = false, + X_Pair_functionString = new Function( 's', 'p', 'k', (function(){ var ret = 'var i=0,l=s.length,a;' + 'for(;i gadget.io.makeRequset, flash + // TODO (xProtocol | method='update' | !cors) & canUse -> gadget.io.makeRequset, flash // force 'gadget', 'flash' switch( X_NET_currentData[ 'test' ] ){ case 'gadget' : @@ -277,6 +283,7 @@ function X_NET_shiftQueue(){ break; case 'flash' : break; + default : X_NET_currentWrapper = X_NET_XHRWrapper || X_TEMP.X_Net_XHR_init(); }; @@ -285,16 +292,17 @@ function X_NET_shiftQueue(){ // OAuth2 if( auth = X_NET_currentData[ 'auth' ] ){ authSettings = X_Pair_get( auth ); - switch( auth.state() ){ + switch( auth[ 'state' ]() ){ case 0 : case 1 : + case 2 : if( !( auth[ 'dispatch' ]( X_EVENT_NEED_AUTH ) & X_Callback_PREVENT_DEFAULT ) ){ authSettings.lazyReq = X_NET_currentQueue; }; X_NET_currentQueue = null; X_NET_shiftQueue(); break; - case 2 : // refresh token + case 3 : // refresh token X_NET_QUEUE_LIST.push( X_NET_currentQueue ); X_NET_currentQueue = null; X_NET_shiftQueue(); @@ -312,9 +320,6 @@ function X_NET_shiftQueue(){ case X_NET_TYPE_IMAGE : X_NET_currentWrapper = X_NET_ImageWrapper || X_TEMP.X_NET_Image_init(); break; - case 5 : - X_NET_currentWrapper = X_NET_GIMRWrapper; - break; }; X_NET_currentWrapper[ 'listen' ]( [ X_EVENT_PROGRESS, X_EVENT_SUCCESS, X_EVENT_ERROR, X_EVENT_TIMEOUT ], X_NET_currentQueue, X_NET_proxyDispatch ); diff --git a/0.6.x/js/06_net/05_XXHRGadget.js b/0.6.x/js/06_net/05_XXHRGadget.js index 0a04809..3d2cf68 100644 --- a/0.6.x/js/06_net/05_XXHRGadget.js +++ b/0.6.x/js/06_net/05_XXHRGadget.js @@ -39,12 +39,14 @@ var X_NET_GIMR_canUse = 5.5 <= X_UA[ 'IE' ] || !X_UA[ 'IE' ], X_NET_GIMR_phase = 0, - X_NET_GIMR_lastHashString; + X_NET_GIMR_lastHashString, + + X_NET_GIMR_isReceiveBatches, X_NET_GIMR_receivedString; function X_NET_GIMR_detectImageOverIframe(){ var raw = X_NET_GIMR_gadgetIframe[ '_rawObject' ], - iwin, ret, error, data = null; + iwin, ret, n, error, data = null; if( raw ){ iwin = raw.contentWindow || ( raw.contentDocument && raw.contentDocument.parentWindow ) || window.frames[ X_NET_GIMR_iframeName ]; @@ -53,7 +55,8 @@ function X_NET_GIMR_detectImageOverIframe(){ ret = X_NET_GIMR_detection( iwin.frames ); if( ret && ret !== X_NET_GIMR_lastHashString ){ X_NET_GIMR_lastHashString = ret; - console.log( ret ); + //console.log( ret.length ); + //console.log( '' + ret ); switch( X_NET_GIMR_phase ){ case 0 : // init @@ -66,7 +69,33 @@ function X_NET_GIMR_detectImageOverIframe(){ break; case 2 : // _waiting_ 通信結果待ち - ret = X_String_parseTrustedJsonString( decodeURIComponent( ret.substr( 1 ) ) ); + // 分割受信モードか? + ret = ret.substr( 1 ); + n = parseInt( ret ); + + if( X_NET_GIMR_isReceiveBatches ){ + X_NET_GIMR_receivedString += ret; + if( --X_NET_GIMR_isReceiveBatches ){ + iwin.location.href = X_NET_GIMR_GADGET_URL + '#_recived_' + X_NET_GIMR_isReceiveBatches; + return; + }; + } else + if( 1 < n ){ + X_NET_GIMR_receivedString = ret.substr( ( n + ':' ).length ); + X_NET_GIMR_isReceiveBatches = --n; + iwin.location.href = X_NET_GIMR_GADGET_URL + '#_recived_' + X_NET_GIMR_isReceiveBatches; + // TODO speedup + X_NET_GIMR_timerID = X_Timer_add( 16, 0, X_NET_GIMR_detectImageOverIframe ); + return X_Callback_UN_LISTEN; + } else { + X_NET_GIMR_receivedString = ret; + }; + + var zero = String.fromCharCode( 0 ); + console.log( _builtin_decodeURI( X_NET_GIMR_receivedString ).split( zero + '002' ).join( '--' ).split( zero + 'E0F' ).join( '--' ) ); + ret = X_String_parseTrustedJsonString( _builtin_decodeURI( X_NET_GIMR_receivedString ).split( zero + '002' ).join( '。' ).split( zero + 'F0E' ).join( '.' ) ); + + X_NET_GIMR_receivedString = ''; error = ret[ 'errors' ] && ret[ 'errors' ].length; @@ -83,14 +112,15 @@ function X_NET_GIMR_detectImageOverIframe(){ X_NET_GIMRWrapper [ 'asyncDispatch' ]({ type : error ? X_EVENT_ERROR : X_EVENT_SUCCESS, - state : ret[ 'rc' ] || ( error ? 400 : 200 ), + state : ret[ 'rc' ] || ( error ? ret[ 'code' ] || 400 : 200 ), data : data, 'headers' : ret[ 'headers' ], 'message' : error && ret[ 'errors' ].join( '\n' ) }); - iwin.location.href = X_NET_GIMR_GADGET_URL + '#_recived_'; + X_NET_GIMR_timerID = X_NET_GIMR_phase = 0; - X_NET_GIMR_lastHashString = ''; + X_NET_GIMR_lastHashString = ''; + iwin.location.href = X_NET_GIMR_GADGET_URL + '#_recived_'; return X_Callback_UN_LISTEN; }; @@ -145,9 +175,11 @@ X_NET_GIMRWrapper = X_Class_override( } ); }; - X_NET_GIMR_timerID = X.Timer.add( 100, 0, X_NET_GIMR_detectImageOverIframe ); + X_NET_GIMR_timerID = X_Timer_add( 333, 0, X_NET_GIMR_detectImageOverIframe ); X_NET_GIMR_requestOriginal = X_Object_deepCopy( obj ); + + console.dir( obj ); X_NET_GIMR_requestOptions = { 'CONTENT_TYPE' : 'TEXT', @@ -157,7 +189,7 @@ X_NET_GIMRWrapper = X_Class_override( }; for( k in obj ){ - if( v = obj[ k ] ){ + if( v = '' + obj[ k ] ){ switch( k ){ case 'postdata' : X_NET_GIMR_requestOptions[ 'POST_DATA' ] = v; @@ -167,7 +199,7 @@ X_NET_GIMRWrapper = X_Class_override( break; case 'dataType' : // TEXT 以外は無視される? - switch( v = ( '' + v ).toUpperCase() ){ + switch( v = v.toUpperCase() ){ case 'TEXT' : case 'JSON' : X_NET_GIMR_requestOptions[ 'CONTENT_TYPE' ] = v; @@ -182,7 +214,7 @@ X_NET_GIMRWrapper = X_Class_override( }; break; case 'headers' : - X_NET_GIMR_requestOptions[ 'HEADERS' ] = X_NET_GIMR_requestOriginal[ v ]; + X_NET_GIMR_requestOptions[ 'HEADERS' ] = X_NET_GIMR_requestOriginal[ k ]; break; case 'cashe' : X_NET_GIMR_requestOptions[ 'REFRESH_INTERVAL' ] = 3600; diff --git a/0.6.x/js/06_net/10_XOAuth2.js b/0.6.x/js/06_net/10_XOAuth2.js index e43d508..d8f3dd1 100644 --- a/0.6.x/js/06_net/10_XOAuth2.js +++ b/0.6.x/js/06_net/10_XOAuth2.js @@ -17,9 +17,9 @@ var X_NET_OAUTH2_detection = new Function( 'w', 'try{return w.location.sear *
*
0 :
disconnected *
1 :
now authentication ... - *
+ :
authorization_code - *
2 :
refresh_token - *
3 :
hasAccessToken + *
2 :
authorization_code + *
3 :
refresh_token + *
4 :
hasAccessToken *
*/ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ]( @@ -29,11 +29,13 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ]( /** @lends OAuth2.prototype */ { 'Constructor' : function( obj ){ + obj = X_Object_clone( obj ); - X_Pair_create( this, obj = X_Object_clone( obj ) ); + X_Pair_create( this, obj ); if( _getAccessToken( this ) ){ - obj.oauth2State = 3; + obj.oauth2State = 4; + this[ 'asyncDispatch' ]( X_EVENT_SUCCESS ); } else { this[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH ); } @@ -44,7 +46,7 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ]( // TODO kill の cancel }, - 'authState' : function(){ + 'state' : function(){ return X_Pair_get( this ).oauth2State || 0; }, @@ -62,7 +64,7 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ]( { 'response_type' : 'code', 'client_id' : pair[ 'clientID' ], - 'redirect_uri' : tpair[ 'redirectURI' ], + 'redirect_uri' : pair[ 'redirectURI' ], 'scope' : ( pair[ 'scopes' ] || []).join(' ') } ), 'oauthauthorize', @@ -80,14 +82,15 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ]( }, 'cancelAuth' : function(){ - pair = X_Pair_get( this ); + var pair = X_Pair_get( this ); if( pair.net ){ pair.net[ 'kill' ](); delete pair.net; }; - X_NET_OAUTH2_authorizationWindow && X_NET_OAUTH2_authorizationWindow.close(); + // http://kojikoji75.hatenablog.com/entry/2013/12/15/223839 + X_NET_OAUTH2_authorizationWindow && X_NET_OAUTH2_authorizationWindow.open( 'about:blank','_self' ).close(); X_NET_OAUTH2_authorizationWindow = null; X_NET_OAUTH2_authorizationTimerID && X_Timer_remove( X_NET_OAUTH2_authorizationTimerID ); @@ -97,7 +100,7 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ]( }, 'refreshToken' : function(){ - /* + /* TODO 自動リフレッシュ * var expires_at = this._getAccessTokenExpiry(); if (expires_at && Date.now() + millis > expires_at) this._refreshAccessToken({replay: false}); @@ -107,7 +110,7 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ]( if( pair.net ) return; - pair.oauth2State = 2; + pair.oauth2State = 3; pair.net = X.Net( { 'xhr' : pair[ 'tokenEndpoint' ], @@ -118,10 +121,11 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ]( 'refresh_token' : _getRefreshToken( this ) }), 'dataType' : 'json', - 'headers' : { - 'Accept' : 'application/json', - 'Content-Type' : 'application/x-www-form-urlencoded' - } + 'headers' : { + 'Accept' : 'application/json', + 'Content-Type' : 'application/x-www-form-urlencoded' + }, + 'test' : 'gadget' } ).listenOnce( [ X_EVENT_SUCCESS, X_EVENT_ERROR ], this, X_Net_OAuth2_responceHandler ); this[ 'asyncDispatch' ]( { type : X_EVENT_PROGRESS, message : 'Start to refresh token.' } ); @@ -130,11 +134,12 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ]( ); function X_Net_OAuth2_detectAuthPopup(){ - var closed, search, pair; + var closed, search, pair = X_Pair_get( this ); - if( window.frames[ 'oauthauthorize' ] !== X_NET_OAUTH2_authorizationWindow || X_NET_OAUTH2_authorizationWindow.closed ){ + if( X_NET_OAUTH2_authorizationWindow.closed ){ pair.oauth2State = 0; closed = true; + this[ 'asyncDispatch' ]( X_EVENT_CANCELED ); } else if( search = X_NET_OAUTH2_detection( X_NET_OAUTH2_authorizationWindow ) ){ @@ -146,6 +151,7 @@ function X_Net_OAuth2_detectAuthPopup(){ X_Net_OAuth2_authorizationCode( this, pair ); + pair.oauth2State = 2; this[ 'asyncDispatch' ]( { type : X_EVENT_PROGRESS, message : 'Get code success, then authorization code.' } ); }; @@ -168,17 +174,18 @@ function X_Net_OAuth2_authorizationCode( oauth2, pair ){ 'redirect_uri' : pair[ 'redirectURI' ] }), 'dataType' : 'json', - 'headers' : { + 'headers' : { 'Accept' : 'application/json', 'Content-Type' : 'application/x-www-form-urlencoded' - } + }, + 'test' : 'gadget' } ).listenOnce( [ X_EVENT_SUCCESS, X_EVENT_ERROR ], oauth2, X_Net_OAuth2_responceHandler ); }; function X_Net_OAuth2_responceHandler( e ){ var data = e.data, pair = X_Pair_get( this ), - isRefresh = pair.oauth2State === 2; + isRefresh = pair.oauth2State === 3; delete pair.net; @@ -208,7 +215,7 @@ function X_Net_OAuth2_responceHandler( e ){ _removeAccessTokenExpiry( this ); }; - pair.oauth2State = 3; + pair.oauth2State = 4; this[ 'asyncDispatch' ]( { type : X_EVENT_SUCCESS, message : isRefresh ? 'Refresh access token success.' : 'Get new access token success.' } ); break; @@ -225,7 +232,7 @@ function X_Net_OAuth2_responceHandler( e ){ this[ 'asyncDispatch' ]( { type : X_EVENT_ERROR, message : 'network-error' } ); } else { pair.oauth2State = 0; - _setAuthMechanism( 'param' ); + _setAuthMechanism( this, 'param' ); this[ 'asyncDispatch' ]( { type : X_EVENT_PROGRESS, message : 'Refresh access token failed. retry header -> param. ' } ); // retry X_Net_OAuth2_authorizationCode( this, pair ); @@ -241,7 +248,7 @@ function X_NET_OAUTH2_onXHR401Error( oauth2 ){ if( _getAuthMechanism( oauth2 ) !== 'param' ){ xhr = X_NET_currentWrapper[ '_rawObject' ]; bearerParams = xhr.getResponseHeader( 'WWW-Authenticate' ); - headersExposed = !X_Net_XHR_X_DOMAIN || !!xhr.getAllResponseHeaders(); // this is a hack for Firefox and IE + headersExposed = !X_Net_XHR_createXDR || !!xhr.getAllResponseHeaders(); // this is a hack for Firefox and IE }; // http://d.hatena.ne.jp/ritou/20110402/1301679908 @@ -251,7 +258,7 @@ function X_NET_OAUTH2_onXHR401Error( oauth2 ){ } else if ((( bearerParams && bearerParams.indexOf( ' error="invalid_token"' ) !== -1 ) || !headersExposed) && _getRefreshToken( oauth2 ) ) { _removeAccessToken( oauth2 ); // It doesn't work any more. - pair.oauth2State = 2; + pair.oauth2State = 3; oauth2[ 'refreshToken' ](); } else if (!headersExposed && !_getRefreshToken( oauth2 )) { @@ -266,12 +273,12 @@ function X_NET_OAUTH2_updateRequest( oauth2, request ){ url = request[ 'url' ], headers; - if( token && mechanism === 'param'){ + if( token && mechanism === 'param' ){ request[ 'url' ] = url + ((url.indexOf('?') !== -1) ? '&' : '?') + 'bearer_token=' + encodeURIComponent( token ); }; if( token && ( !mechanism || mechanism === 'header' ) ){ - request[ 'headers' ] || ( headers = request[ 'headers' ] = {} ); + headers = request[ 'headers' ] || ( request[ 'headers' ] = {} ); headers[ 'Authorization' ] = 'Bearer ' + token; }; }; @@ -280,8 +287,9 @@ function _getAccessToken( that ){ return updateLocalStorage( '', that, 'accessTo function _getRefreshToken( that){ return updateLocalStorage( '', that, 'refreshToken' ); } function _getAccessTokenExpiry( that ){ return updateLocalStorage( '', that, 'tokenExpiry' ); } function _getAuthMechanism( that ){ + // TODO use gadget | flash ... // IE's XDomainRequest doesn't support sending headers, so don't try. - return X_Net_XHR_X_DOMAIN ? 'param' : updateLocalStorage( '', that, 'AuthMechanism' ); + return X_Net_XHR_createXDR ? 'param' : updateLocalStorage( '', that, 'AuthMechanism' ); } function _setAccessToken( that, value ){ updateLocalStorage( '+', that, 'accessToken' , value); } function _setRefreshToken( that, value ){ updateLocalStorage( '+', that, 'refreshToken', value); } @@ -294,7 +302,7 @@ function _removeAccessTokenExpiry( that ){ updateLocalStorage( '-', that, 'token function _removeAuthMechanism( that ){ updateLocalStorage( '-', that, 'AuthMechanism' ); } function updateLocalStorage( cmd, that, name, value ){ - var action = cmd === '+' ? 'setItem' : '-' ? 'removeItem' : 'getItem', + var action = cmd === '+' ? 'setItem' : cmd === '-' ? 'removeItem' : 'getItem', pair; if( window.localStorage ){ diff --git a/0.6.x/js/main.js b/0.6.x/js/main.js index 73ef331..77956ee 100644 --- a/0.6.x/js/main.js +++ b/0.6.x/js/main.js @@ -64,8 +64,6 @@ root = X.UI.PageRoot( ) ) ); -_root = X.Class._getPrivate( root ); -_text = X.Class._getPrivate( text ); function _onClick( e ){ alert( e.type ); -- 2.11.0