×
Namespaces

Variants
Actions

访问受DRM保护的文件---IAP编程及例子解析

From Nokia Developer Wiki
Jump to: navigation, search

Delete instructional text in italic

This article explains how to ... Replace the abstract text with a short paragraph (or sentence) describing what the topic covers.

Enter article metadata as described below. Note that this template can be placed anywhere in the article.


Article Metadata
Article
Created: ()
Last edited: hamishwillee (30 May 2013)

Contents

介绍

在IAP编程中一个重要的部分就是访问受DRM保护的文件,它不能使用RFile来操作,Symbian平台提供了CAF(Content Access Framwork)框架来访问DRM内容,下面我们将结合例子程序(media:IBook.zip)逐步分析使用CAF来访问DRM内容的方法。

前提

首先在我们的应用中已经提供了由DRM保护的文件,如何用DRM保护文件可以参考:程序内支付(IAP)编程及例子解析
在工程文件引用了相应的动态链接库:

LIBS+= -lcaf \
-lcafutils\
-lapmime

实现

由于CAF框架中的API是Symbian的实现方法,在Qt的应用程序中,我们最好实现个Wrapper类,提供Qt风格的接口。

class DRMFile::public QObejct
{
Q_OBJECT
Q_DECLARE_PRIVATE(DRMFile)
...
 
public:
//打开受保护的文件, 如果不能正确打开文件则返回一个负数
int open(const QString &name);
//data,分配的一段内存并将整个文件的内容读入到其中;
//客户端负责释放
int read(uchar*& data);
//关闭文件
void close();
 
...
 
protected:
DRMFilePrivate* const d_ptr;
 
}

在DRMFilePrivate中包裹CContent和CData,使用这两个类实现文件的打开、读、关闭方法。关于Qt的这种Private设计模式,可以参考Qt 开发

class DRMFilePrivate
{
...
private:
CContent *CAF_file;
CData *CAF_data;
TBool intentExecuted;
..,
}

打开文件:

int DRMFilePrivate::openL(const TDesC& fileName)
{
int result = KErrNone;
if((CAF_file!=NULL) || (CAF_data!=NULL))
return KErrInUse;
 
CAF_file = CContent::NewL(fileName);
RStreamablePtrArray <CEmbeddedObject> myArray;
CleanupClosePushL(myArray);
//将当前容器的内容读入到数组中
CAF_file->GetEmbeddedObjectsL(myArray);
if(myArray.Count() > 0)
{
// 打开第一个对象
CAF_data = CAF_file->OpenContentL(ContentAccess::EExecute, myArray[0]->UniqueId());
}
else
{
// no objects found
result = KErrNotFound;
}
CleanupStack::PopAndDestroy(&myArray);
return result;
}

将读入的内容保存到data中,返回错误码

int DRMFilePrivate::read(uchar*& data)
{
int result = KErrNone;
data = NULL;
while(true)
{
if(CAF_data == NULL)
{
result = KErrBadHandle;
break;
}
RBuf8 buffer;
int length = 0;
TRAP(result, CAF_data->DataSizeL(length));
if(result != KErrNone)
break;
length++; //to be able use PtrZ() call
TRAP (result, buffer.CreateL(length));
if(result != KErrNone)
break;
if(!intentExecuted)
result = CAF_data->ExecuteIntent(ContentAccess::EExecute);
if(result != KErrNone)
break;
intentExecuted = ETrue;
result = CAF_data->Read(buffer);
if(result != KErrNone)
break;
result = length;
uchar* ptr = const_cast<uchar*>(buffer.PtrZ()); //give up ownership from RBuf8
data = ptr;
break;
}
if(result < KErrNone)
RDebug::Print(_L("DRMFilePrivate::read ContentAccess::CData::Read error %d"), result);
return result;
}

关闭文件:

void DRMFilePrivate::close()
{
if(CAF_data!=NULL)
{
delete CAF_data;
CAF_data = NULL;
}
if(CAF_file!=NULL)
{
delete CAF_file;
CAF_file = NULL;
}
}

总结

更多关于CAF和DRM的内容可以参考:

相关链接

Add categories below. Remove Category:Draft when the page is complete or near complete

This page was last modified on 30 May 2013, at 06:34.
136 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×