본문 바로가기

C/C++

NPAPI로 Browser Cookie 설정하기

NPAPI로 Browser Cookie 설정하기

 

파이어폭스와 사파리에서 동작 (오페라, IE는 비정상 동작, 크롬은 안해봤음)

 

Gecko 1.9 이하 버전 :

GetCookie()

if( NPN_GetValue(npInstance, NPNVWindowNPObject, &pWndObject) == NPERR_NO_ERROR)
{
    if(NPN_GetProperty(npInstance, pWndObject,NPN_GetStringIdentifier("document"), &npDocument) == true)
    {
          if(NPN_GetProperty(npInstance, NPVARIANT_TO_OBJECT(npDocument), NPN_GetStringIdentifier("cookie"), &npCookie) == true)
        {
                if( npCookie.type == NPVariantType_String )
               {
                     NPVarint_ToADTCHAR(adtCookie, &npCookie);  //알아서 형변환
                }
                 NPN_ReleaseVariantValue(&npCookie);
         }
          NPN_ReleaseVariantValue(&npDocument);
    }
    NPN_ReleaseObject(pWndObject);
 }

 

SetCookie():

if( NPN_GetValue(npInstance, NPNVWindowNPObject, &pWndObject) == NPERR_NO_ERROR)
{
    if(NPN_GetProperty(npInstance, pWndObject, NPN_GetStringIdentifier("document"), &npDocument) == true)
    {
       if (ADTCHAR_ToNPVarint(&npCookie, adtCookie)) //알아서 형변환
       {
            if(NPN_SetProperty(npInstance, NPVARIANT_TO_OBJECT(npDocument), NPN_GetStringIdentifier("cookie"), &npCookie) == true)
            {
                 NPN_ReleaseVariantValue(&npCookie);
            }
        }
        NPN_ReleaseVariantValue(&npDocument);
    }
    NPN_ReleaseObject(pWndObject);
 }

 

Gecko 1.9 이상 버전 : (링크가기)

GetCookie():

char* cookie = NULL;
unsigned int length = 0;

/* where |myPlugin| is your plugin instance: */
sNPNFunctions->
    getvalueforurl(myPlugin, NPNURLVCookie,
                   "http://www.example.com/index.html",
                   &cookie, &length);
if (cookie) {
  printf("Hey, cookie is:/n/n%s/n", cookie);

  /* it has already been allocated! */
  free(cookie);
  cookie = NULL;

 

SetCookie():

setvalueforurl


 

(2012/03/09 추가분)

[1]의 모질라 홈페이지에 보면 함수 원형은 다음과 같이 나와 있다.

#include <npapi.h>

typedef enum {
  NPNURLVCookie = 501,
  NPNURLVProxy
} NPNURLVariable;

NPError NPN_GetValueForURL(NPP instance,
                           NPNURLVariable variable,
                           const char *url,
                           char **value,
                           uint32_t *len);

 
그리고 쿠키를 얻어오는 value란 파라미터에 대해서는 아래처럼 설명한다.
 
Out parameter. The browser passes back the requested information. If the function succeeds, the result buffer will be allocated with NPN_MemAlloc; the plugin is responsible for freeing the buffer. Note: the value may have internal NULL bytes and may not be NULL-terminated.
 
즉, 브라우저는 value라는 변수에 쿠키값을 넘겨주는데, 브라우저 내에서 NPN_MemAlloc라는 NPAPI 메소드로 메모리를 할당하며, 이 해제는 플러그인에서 책임을 져야한다는 내용이다.
물론 이 value를 해제할때는 NPN_MemFree를 사용 해야 한다.
그러나 여기서 주목할 점은 'value는 NULL byte를 갖지 않으며, NULL 문자로 끝나지 않을 수도 있다' 라고 적혀있다.
 
[2]를 보면 아래와 같은 방법으로 사용하라고 나와 있다.
 
'strncpy()를 사용하지 말고, memcpy()를 사용해야 하며, value의 length에 + 1을 해라'
 
그리고 NPN_MemAlloc()를 사용하는 모든 곳에 위와 같은 방법으로 문자열을 복사해야 한다


 

Internet Explorer

 

GetCookie():

   DWORD dwCookieSize = 256;
   InternetGetCookie(adtURL, NULL, adtCookie, &dwCookieSize);

 

SetCookie():

   InternetSetCookie(adtURL, NULL, adtCookie);

 

 

ps.

그리고 중요(?)한 사실 하나

NPN_SetProperty와 같은 NPAPI 함수들은 thread-safe하지 않다.

위 함수에서 자꾸 죽길래 모질라 홈페이지 찾아보니 thread내에서 사용하지 말라고 명시되어 있다.

이것 때문에 얼마나 고생을 했는지 ㅠ

 


 

<참조>

1. https://developer.mozilla.org/en/NPN_GetValueForURL

2. https://lists.webkit.org/pipermail/webkit-unassigned/2010-September/242823.html