Dcef3 How to use filters,The following code and some can and some can not be opened
Closed this issue · 2 comments
GoogleCodeExporter commented
procedure TChromiumPage.ChromiumGetResourceHandler(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest;
out Result: ICefResourceHandler);
var
CefRH: ICefResourceHandler;
begin
if frame.IsMain and GetPostFileNameOk(request.url) then
begin
CefRH := TMyPowerScheme.Create(browser, frame, '', request);
Result := CefRH;
end;
end;
//{UnitMyFileScheme begin}----------------------------------------------
{$IFDEF FPC}
{$MODE DELPHI}{$H+}
{$ENDIF}
unit UnitMyFileScheme;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses ceflib, Classes, Vcl.Dialogs;
type
TMyPowerScheme = class;
TMyCefUrlrequestClientOwn = class(TCefUrlrequestClientOwn)
private
protected
procedure OnRequestComplete(const request: ICefUrlRequest); override;
procedure OnUploadProgress(const request: ICefUrlRequest;
current, total: UInt64); override;
procedure OnDownloadProgress(const request: ICefUrlRequest;
current, total: UInt64); override;
procedure OnDownloadData(const request: ICefUrlRequest; data: Pointer;
dataLength: NativeUInt); override;
public
statusText: ustring;
_resourceHandler: TMyPowerScheme;
_response: ICefResponse;
_dataLength: UInt64;
m_url: ustring;
constructor Create; override;
(a_uploadTotal,a_downloadTotal:UInt64;a_frame:ICefFrame;a_url:ustring);
destructor Destroy; override;
end;
TMyPowerScheme = class(TCefResourceHandlerOwn)
private
FPath: string;
FStatus: Integer;
FStatusText: string;
FMimeType: string;
Fbrowser: ICefBrowser;
Fframe: ICefFrame;
FschemeName: ustring;
Frequest: ICefRequest;
// CefUrlrequest: ICefUrlRequest; // TCefUrlRequestRef
// CefRequest: ICefRequest;
// CefurlrequestClient: ICefUrlrequestClient; // TCefUrlrequestClientOwn
_responseHeadersReadyCallback: ICefCallback;
_webRequestClient: TMyCefUrlrequestClientOwn;
_webRequest: ICefUrlRequest;
// _offsetRead: UInt64;
protected
function ProcessRequest(const request: ICefRequest;
const callback: ICefCallback): Boolean; override;
procedure GetResponseHeaders(const response: ICefResponse;
out responseLength: Int64; out redirectUrl: ustring); override;
function ReadResponse(const dataOut: Pointer; bytesToRead: Integer;
var bytesRead: Integer; const callback: ICefCallback): Boolean; override;
function CanGetCookie(const cookie: PCefCookie): Boolean; override;
function CanSetCookie(const cookie: PCefCookie): Boolean; override;
procedure Cancel; override;
public
FDataStream: TMemoryStream;
constructor Create(const browser: ICefBrowser; const frame: ICefFrame;
const schemeName: ustring; const request: ICefRequest); override;
destructor Destroy; override;
end;
implementation
uses Windows, SysUtils, Registry, Contnrs, UnitMain, UnitChromiumTools;
function HTTPDecode(const AStr: ustring): rbstring;
var
Sp, Rp, Cp: PAnsiChar;
src: rbstring;
begin
src := rbstring(AStr);
SetLength(Result, Length(src));
Sp := PAnsiChar(src);
Rp := PAnsiChar(Result);
while Sp^ <> #0 do
begin
case Sp^ of
'+':
Rp^ := ' ';
'%':
begin
Inc(Sp);
if Sp^ = '%' then
Rp^ := '%'
else
begin
Cp := Sp;
Inc(Sp);
if (Cp^ <> #0) and (Sp^ <> #0) then
Rp^ := AnsiChar(StrToInt('$' + Char(Cp^) + Char(Sp^)))
else
begin
Result := '';
Exit;
end;
end;
end;
else
Rp^ := Sp^;
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PAnsiChar(Result));
end;
function ParseFileUrl(const url: ustring): ustring;
label
error;
var
p, s: PWideChar;
state: Integer;
begin
p := PWideChar(url);
s := nil;
state := 0;
while True do
begin
case state of
0:
case p^ of
':':
state := 1;
#0:
goto error;
end;
1:
if p^ = '/' then
state := 2
else
goto error;
2:
if p^ = '/' then
begin
state := 3;
s := p;
end
else
goto error;
3:
case p^ of
'/':
begin
p[-1] := ':';
p^ := '\';
state := 4;
end;
#0:
goto error;
else
p[-1] := p^;
end;
4:
begin
case p^ of
'/':
p^ := '\';
#0:
{$IFDEF UNICODE}
Exit(ustring(HTTPDecode(string(UTF8String(s)))));
{$ELSE}
begin
Result := UTF8Decode(HTTPDecode(s));
Exit;
end;
{$ENDIF}
end;
end;
end;
Inc(p);
end;
error:
Result := '';
end;
{ TMyFileScheme }
procedure TMyPowerScheme.Cancel;
begin
inherited;
end;
function TMyPowerScheme.CanGetCookie(const cookie: PCefCookie): Boolean;
begin
// TCefCookie(cookie^).name
Result := True;
end;
function TMyPowerScheme.CanSetCookie(const cookie: PCefCookie): Boolean;
begin
Result := True;
end;
constructor TMyPowerScheme.Create(const browser: ICefBrowser;
const frame: ICefFrame; const schemeName: ustring;
const request: ICefRequest);
begin
inherited;
FDataStream := TMemoryStream.Create;
Fbrowser := browser;
Fframe := frame;
FschemeName := schemeName;
Frequest := request;
end;
destructor TMyPowerScheme.Destroy;
begin
if FDataStream <> nil then
FDataStream.Free;
inherited;
end;
procedure TMyPowerScheme.GetResponseHeaders(const response: ICefResponse;
out responseLength: Int64; out redirectUrl: ustring);
var
wrcResponse: ICefResponse;
headerMap: ICefStringMultimap;
begin
// inherited;
// response.Status := FStatus;
// response.StatusText := FStatusText;
// response.MimeType := FMimeType;
// responseLength := FDataStream.Size;
{
response.Status := CefUrlrequest.GetResponse.Status;
response.StatusText := CefUrlrequest.GetResponse.StatusText;
response.MimeType := CefUrlrequest.GetResponse.MimeType;
}
// showmessage(CefUrlrequest.GetResponse.StatusText);
// sleep(9000);
// response.Status := 200;
// response.StatusText := 'OK';
// response.MimeType := 'text/html';
// responseLength := FDataStream.Size;
// responseLength := 1985;
wrcResponse := self._webRequestClient._response;
if not assigned(wrcResponse) then
Exit;
response.Status := wrcResponse.Status;
response.statusText := wrcResponse.statusText;
response.MimeType := wrcResponse.MimeType;
headerMap := TCefStringMultimapOwn.Create;
wrcResponse.GetHeaderMap(headerMap);
response.SetHeaderMap(headerMap);
//headerMap := nil;
// ----------------------------------------------------------------------------
{ headerMap := TCefStringMultimapOwn.Create;
if assigned(headerMap.Handle) then
begin
wrcResponse.GetHeaderMap(headerMap); // ??????????
response.SetHeaderMap(headerMap); // ??????????
end;
}
// ----------------------------------------------------------------------------
responseLength := self._webRequestClient._dataLength;
// responseLength := self._webRequestClient.FDataStream.Size;
responseLength := self.FDataStream.Size;
self._webRequestClient._dataLength := 0;
(*
MainForm.Memo1.Lines.Add('GetResponseHeaders->responseLength=' +
IntToStr(responseLength));
MainForm.Memo1.Lines.Add('GetResponseHeaders->response=' +
response.statusText);
MainForm.Memo1.Lines.Add('GetResponseHeaders->GetHeader=' +
response.GetHeader('Content-Type'));
MainForm.Memo1.Lines.Add('GetResponseHeaders->GetHeader=' +
response.GetHeader('Content-Length'));
*)
end;
function TMyPowerScheme.ProcessRequest(const request: ICefRequest;
const callback: ICefCallback): Boolean;
var
RequestHead: ICefStringMultimap;
begin
RequestHead := TCefStringMultimapOwn.Create;
request.GetHeaderMap(RequestHead);
RequestHead.Append('Accept-LanguageX', 'zh-CN,zh;q=0.8');
// RequestHead.Value[RequestHead.FindCount('')] := '';
request.SetHeaderMap(RequestHead);
RequestHead := nil;
//FDataStream := TMemoryStream.Create;
self._responseHeadersReadyCallback := callback;
self._webRequestClient := TMyCefUrlrequestClientOwn.Create;
self._webRequestClient._resourceHandler := self;
self._webRequestClient.m_url := request.url;
_webRequest := TCefUrlRequestRef.New(request, self._webRequestClient);
// MainForm.Memo1.Lines.Add('TMyPowerScheme_ProcessRequest:' + request.url);
// MainForm.Memo1.Lines.Add('ProcessRequest->' + ParseFileUrl(request.url));
Result := True;
// callback.Cont;
end;
function TMyPowerScheme.ReadResponse(const dataOut: Pointer;
bytesToRead: Integer; var bytesRead: Integer;
const callback: ICefCallback): Boolean;
begin
// FDataStream.Position := 0;
// MainForm.Memo1.Lines.Add('ReadResponse:bytesRead'+IntToStr(bytesRead));
// bytesRead := FDataStream.Read(dataOut^, bytesToRead);
// TMemoryStream(FDataStream).Clear;
// Result := True;
// callback.Cont;
// inherited;
// if self._offsetRead < self._webRequestClient._dataLength then
//不能这样写,不能访问self._webRequestClient._dataLength
//if self._webRequestClient._dataLength > 0 then
if bytesToRead > 0 then
begin
// self._webRequestClient.FDataStream.Position := 0;
// bytesRead := self._webRequestClient.FDataStream.Read(dataOut^, bytesToRead);
// self._webRequestClient.FDataStream.Clear;
self.FDataStream.Position := 0;
//bytesRead := self.FDataStream.Read(dataOut^, bytesToRead);
bytesRead := self.FDataStream.Read(dataOut^, self.FDataStream.Size);
self.FDataStream.Position := 0;
self.FDataStream.SaveToFile(ExtractFilePath(ParamStr(0)) +'download\' +GetPostFileName(Frequest.Url));
self.FDataStream.Position := 0;
self.FDataStream.Clear;
//self._webRequestClient._dataLength := 0;
self._webRequestClient := nil;
Result := True;
end else
begin
// self._clientHandler._ReleaseStrongReference(self);
self._webRequestClient := nil;
//self._webRequestClient._dataLength := 0;
Result := False;
end;
callback.Cont;
end;
{ TMyCefUrlrequestClientOwn }
constructor TMyCefUrlrequestClientOwn.Create;
begin
inherited;
self._dataLength := 0;
end;
destructor TMyCefUrlrequestClientOwn.Destroy;
begin
inherited;
end;
procedure TMyCefUrlrequestClientOwn.OnDownloadData(const request
: ICefUrlRequest; data: Pointer; dataLength: NativeUInt);
begin
// inherited;
// self._data := self._data + string(data);
// self.FDataStream.Write(data, dataLength);
// self.FDataStream.Write(data, dataLength);
// self._dataLength := self._dataLength + dataLength;
self._resourceHandler.FDataStream.Write(data, dataLength);
self._dataLength := self._dataLength + dataLength;
// m_downloadData := copy(data,0,dataLength);
// MainForm.Memo1.Lines.Add('OnDownloadData' + IntToStr(dataLength));
end;
procedure TMyCefUrlrequestClientOwn.OnDownloadProgress(const request
: ICefUrlRequest; current, total: UInt64);
begin
// inherited;
//MainForm.Memo1.Lines.Add('OnDownloadProgress' + IntToStr(current) + ':' + IntToStr(total));
end;
const
RequestStatusText: array [0 .. 4] of string = ('Unknown', 'Success',
'Pending', 'Canceled', 'Failed');
procedure TMyCefUrlrequestClientOwn.OnRequestComplete(const request
: ICefUrlRequest);
begin
// FDataStream.Position := 0;
// TMemoryStream(FDataStream).SaveToFile('aaa.html');
// TMemoryStream(FDataStream).Clear;
// MainForm.Memo1.Lines.Add('OnRequestComplete');
// m_frame.LoadString(m_downloadData, '');
// inherited;
// if (request.GetRequestStatus() is TCefUrlRequestStatus) then
self._response := request.GetResponse;
// self._resourceHandler.
{
self._data := self._resourceHandler._clientHandler._OnResourceResponse(
self._resourceHandler._browser,
self._resourceHandler._frame,
request.GetRequest(),
request.GetRequestStatus(),
request.GetRequestError(),
request.GetResponse(),
self._data);
}
// self._dataLength := length(self._data);
// self._dataLength := FDataStream.Size;
// FDataStream.Position := 0;
// FDataStream.SaveToFile(ExtractFilePath(ParamStr(0)) + GetPostFileName(m_url));
// FDataStream.Position := 0;
self._resourceHandler.FDataStream.Position := 0;
//self._resourceHandler.FDataStream.SaveToFile(ExtractFilePath(ParamStr(0)) +'download\' +GetPostFileName(m_url));
self._resourceHandler.FDataStream.Position := 0;
self._resourceHandler._responseHeadersReadyCallback.Cont;
end;
procedure TMyCefUrlrequestClientOwn.OnUploadProgress(const request
: ICefUrlRequest; current, total: UInt64);
begin
//inherited;
//MainForm.Memo1.Lines.Add('OnUploadProgress' + IntToStr(current) + ':' + IntToStr(total));
end;
initialization
CefUserAgent :=
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1788.0 Safari/537.36';
end.
//{UnitMyFileScheme end}----------------------------------------------
Original issue reported on code.google.com by scnclis...@163.com
on 22 May 2014 at 7:43
GoogleCodeExporter commented
[deleted comment]
GoogleCodeExporter commented
Original comment by hgourv...@gmail.com
on 1 Mar 2015 at 11:21
- Changed state: Invalid
- Added labels: ****
- Removed labels: ****