Bug 567323: Layout bindings for camera. r=sicking sr=roc

This commit is contained in:
Kyle Huey 2010-07-23 14:35:51 -07:00
parent 4a03080254
commit cc5305fe73
4 changed files with 355 additions and 37 deletions

View File

@ -52,7 +52,7 @@ MODULE = layout
LIBRARY_NAME = gkforms_s
LIBXUL_LIBRARY = 1
XPIDLSRCS = nsICapturePicker.idl
EXPORTS = \
nsIListControlFrame.h \

View File

@ -93,6 +93,8 @@
#include "mozilla/Services.h"
#include "nsDirectoryServiceDefs.h"
#include "nsCharSeparatedTokenizer.h"
#include "nsICapturePicker.h"
#include "nsIFileURL.h"
#define SYNC_TEXT 0x1
#define SYNC_BUTTON 0x2
@ -157,8 +159,10 @@ nsFileControlFrame::Init(nsIContent* aContent,
nsresult rv = nsBlockFrame::Init(aContent, aParent, aPrevInFlow);
NS_ENSURE_SUCCESS(rv, rv);
mMouseListener = new MouseListener(this);
mMouseListener = new BrowseMouseListener(this);
NS_ENSURE_TRUE(mMouseListener, NS_ERROR_OUT_OF_MEMORY);
mCaptureMouseListener = new CaptureMouseListener(this);
NS_ENSURE_TRUE(mCaptureMouseListener, NS_ERROR_OUT_OF_MEMORY);
if (!gUploadLastDir)
nsFileControlFrame::InitUploadLastDir();
@ -178,6 +182,11 @@ nsFileControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
nsCOMPtr<nsIDOMEventGroup> systemGroup;
mContent->GetSystemEventGroup(getter_AddRefs(systemGroup));
nsCOMPtr<nsIDOM3EventTarget> dom3Capture = do_QueryInterface(mCapture);
if (dom3Capture) {
nsContentUtils::DestroyAnonymousContent(&mCapture);
}
nsCOMPtr<nsIDOM3EventTarget> dom3Browse = do_QueryInterface(mBrowse);
if (dom3Browse) {
dom3Browse->RemoveGroupedEventListener(click, mMouseListener, PR_FALSE,
@ -192,10 +201,62 @@ nsFileControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
nsContentUtils::DestroyAnonymousContent(&mTextContent);
}
mCaptureMouseListener->ForgetFrame();
mMouseListener->ForgetFrame();
nsBlockFrame::DestroyFrom(aDestructRoot);
}
struct CaptureCallbackData {
nsICapturePicker* picker;
PRUint32* mode;
};
typedef struct CaptureCallbackData CaptureCallbackData;
PRBool CapturePickerAcceptCallback(const nsAString& aAccept, void* aClosure)
{
nsresult rv;
PRBool captureEnabled;
CaptureCallbackData* closure = (CaptureCallbackData*)aClosure;
if (StringBeginsWith(aAccept,
NS_LITERAL_STRING("image/"))) {
rv = closure->picker->ModeMayBeAvailable(nsICapturePicker::MODE_STILL,
&captureEnabled);
NS_ENSURE_SUCCESS(rv, rv);
if (captureEnabled) {
*closure->mode = nsICapturePicker::MODE_STILL;
return PR_FALSE;
}
} else if (StringBeginsWith(aAccept,
NS_LITERAL_STRING("audio/"))) {
rv = closure->picker->ModeMayBeAvailable(nsICapturePicker::MODE_AUDIO_CLIP,
&captureEnabled);
NS_ENSURE_SUCCESS(rv, rv);
if (captureEnabled) {
*closure->mode = nsICapturePicker::MODE_AUDIO_CLIP;
return PR_FALSE;
}
} else if (StringBeginsWith(aAccept,
NS_LITERAL_STRING("video/"))) {
rv = closure->picker->ModeMayBeAvailable(nsICapturePicker::MODE_VIDEO_CLIP,
&captureEnabled);
NS_ENSURE_SUCCESS(rv, rv);
if (captureEnabled) {
*closure->mode = nsICapturePicker::MODE_VIDEO_CLIP;
return PR_FALSE;
}
rv = closure->picker->ModeMayBeAvailable(nsICapturePicker::MODE_VIDEO_NO_SOUND_CLIP,
&captureEnabled);
NS_ENSURE_SUCCESS(rv, rv);
if (captureEnabled) {
*closure->mode = nsICapturePicker::MODE_VIDEO_NO_SOUND_CLIP;
return PR_FALSE;;
}
}
return PR_TRUE;
}
nsresult
nsFileControlFrame::CreateAnonymousContent(nsTArray<nsIContent*>& aElements)
{
@ -258,6 +319,40 @@ nsFileControlFrame::CreateAnonymousContent(nsTArray<nsIContent*>& aElements)
mBrowse->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
NS_LITERAL_STRING("button"), PR_FALSE);
// Create the capture button
nsCOMPtr<nsICapturePicker> capturePicker;
capturePicker = do_GetService("@mozilla.org/capturepicker;1");
if (capturePicker) {
PRUint32 mode = 0;
CaptureCallbackData data;
data.picker = capturePicker;
data.mode = &mode;
ParseAcceptAttribute(&CapturePickerAcceptCallback, (void*)&data);
if (mode != 0) {
mCaptureMouseListener->mMode = mode;
nodeInfo = doc->NodeInfoManager()->GetNodeInfo(nsGkAtoms::input, nsnull,
kNameSpaceID_XHTML);
NS_NewHTMLElement(getter_AddRefs(mCapture), nodeInfo.forget(), PR_FALSE);
if (!mCapture)
return NS_ERROR_OUT_OF_MEMORY;
// Mark the element to be native anonymous before setting any attributes.
mCapture->SetNativeAnonymous();
mCapture->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
NS_LITERAL_STRING("button"), PR_FALSE);
mCapture->SetAttr(kNameSpaceID_None, nsGkAtoms::value,
NS_LITERAL_STRING("capture"), PR_FALSE);
nsCOMPtr<nsIDOMEventTarget> captureEventTarget =
do_QueryInterface(mCapture);
captureEventTarget->AddEventListener(click, mCaptureMouseListener, PR_FALSE);
}
}
nsCOMPtr<nsIDOMHTMLInputElement> fileContent = do_QueryInterface(mContent);
nsCOMPtr<nsIDOMHTMLInputElement> browseControl = do_QueryInterface(mBrowse);
if (fileContent && browseControl) {
@ -273,6 +368,9 @@ nsFileControlFrame::CreateAnonymousContent(nsTArray<nsIContent*>& aElements)
if (!aElements.AppendElement(mBrowse))
return NS_ERROR_OUT_OF_MEMORY;
if (mCapture && !aElements.AppendElement(mCapture))
return NS_ERROR_OUT_OF_MEMORY;
nsCOMPtr<nsIDOM3EventTarget> dom3Browse = do_QueryInterface(mBrowse);
NS_ENSURE_STATE(dom3Browse);
// Register as an event listener of the button
@ -291,6 +389,7 @@ nsFileControlFrame::AppendAnonymousContentTo(nsBaseContentList& aElements)
{
aElements.MaybeAppendElement(mTextContent);
aElements.MaybeAppendElement(mBrowse);
aElements.MaybeAppendElement(mCapture);
}
NS_QUERYFRAME_HEAD(nsFileControlFrame)
@ -303,14 +402,8 @@ nsFileControlFrame::SetFocus(PRBool aOn, PRBool aRepaint)
{
}
/**
* This is called when our browse button is clicked
*/
NS_IMETHODIMP
nsFileControlFrame::MouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
PRBool ShouldProcessMouseClick(nsIDOMEvent* aMouseEvent)
{
NS_ASSERTION(mFrame, "We should have been unregistered");
// only allow the left button
nsCOMPtr<nsIDOMMouseEvent> mouseEvent = do_QueryInterface(aMouseEvent);
nsCOMPtr<nsIDOMNSUIEvent> uiEvent = do_QueryInterface(aMouseEvent);
@ -318,20 +411,130 @@ nsFileControlFrame::MouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
PRBool defaultPrevented = PR_FALSE;
uiEvent->GetPreventDefault(&defaultPrevented);
if (defaultPrevented) {
return NS_OK;
return PR_FALSE;
}
PRUint16 whichButton;
if (NS_FAILED(mouseEvent->GetButton(&whichButton)) || whichButton != 0) {
return NS_OK;
return PR_FALSE;
}
PRInt32 clickCount;
if (NS_FAILED(mouseEvent->GetDetail(&clickCount)) || clickCount > 1) {
return PR_FALSE;
}
return PR_TRUE;
}
/**
* This is called when our capture button is clicked
*/
NS_IMETHODIMP
nsFileControlFrame::CaptureMouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
{
nsresult rv;
NS_ASSERTION(mFrame, "We should have been unregistered");
if (!ShouldProcessMouseClick(aMouseEvent))
return NS_OK;
// Get parent nsIDOMWindowInternal object.
nsIContent* content = mFrame->GetContent();
nsCOMPtr<nsIDOMNSHTMLInputElement> inputElem = do_QueryInterface(content);
nsCOMPtr<nsIFileControlElement> fileControl = do_QueryInterface(content);
if (!content || !inputElem || !fileControl)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIDocument> doc = content->GetDocument();
if (!doc)
return NS_ERROR_FAILURE;
// Get Loc title
nsXPIDLString title;
nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
"FileUpload", title);
nsPIDOMWindow* win = doc->GetWindow();
if (!win) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsICapturePicker> capturePicker;
capturePicker = do_CreateInstance("@mozilla.org/capturepicker;1");
if (!capturePicker)
return NS_ERROR_FAILURE;
rv = capturePicker->Init(win, title, mMode);
NS_ENSURE_SUCCESS(rv, rv);
// Tell our textframe to remember the currently focused value
mFrame->mTextFrame->InitFocusedValue();
// Show dialog
PRUint32 result;
rv = capturePicker->Show(&result);
NS_ENSURE_SUCCESS(rv, rv);
if (result == nsICapturePicker::RETURN_CANCEL)
return NS_OK;
if (!mFrame) {
// The frame got destroyed while the filepicker was up. Don't do
// anything here.
// (This listener itself can't be destroyed because the event listener
// manager holds a strong reference to us while it fires the event.)
return NS_OK;
}
nsresult result;
nsCOMPtr<nsIURI> uri;
rv = capturePicker->GetUri(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
nsTArray<nsString> newFileNames;
if (uri) {
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(uri);
if (!fileURL)
return NS_ERROR_UNEXPECTED;
nsCAutoString spec;
rv = uri->GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
newFileNames.AppendElement(NS_ConvertUTF8toUTF16(spec));
} else {
return NS_ERROR_FAILURE;
}
// XXXkhuey we really should have a better UI story than the tired old
// uneditable text box with the file name inside.
// Set new selected files
if (!newFileNames.IsEmpty()) {
// Tell mTextFrame that this update of the value is a user initiated
// change. Otherwise it'll think that the value is being set by a script
// and not fire onchange when it should.
PRBool oldState = mFrame->mTextFrame->GetFireChangeEventState();
mFrame->mTextFrame->SetFireChangeEventState(PR_TRUE);
fileControl->SetFileNames(newFileNames);
mFrame->mTextFrame->SetFireChangeEventState(oldState);
// May need to fire an onchange here
mFrame->mTextFrame->CheckFireOnChange();
}
return NS_OK;
}
/**
* This is called when our browse button is clicked
*/
NS_IMETHODIMP
nsFileControlFrame::BrowseMouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
{
nsresult rv;
NS_ASSERTION(mFrame, "We should have been unregistered");
if (!ShouldProcessMouseClick(aMouseEvent))
return NS_OK;
// Get parent nsIDOMWindowInternal object.
nsIContent* content = mFrame->GetContent();
@ -359,14 +562,13 @@ nsFileControlFrame::MouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
}
PRBool multi;
result = inputElem->GetMultiple(&multi);
NS_ENSURE_SUCCESS(result, result);
rv = inputElem->GetMultiple(&multi);
NS_ENSURE_SUCCESS(rv, rv);
result = filePicker->Init(win, title, multi ?
(PRInt16)nsIFilePicker::modeOpenMultiple :
(PRInt16)nsIFilePicker::modeOpen);
if (NS_FAILED(result))
return result;
rv = filePicker->Init(win, title, multi ?
(PRInt16)nsIFilePicker::modeOpenMultiple :
(PRInt16)nsIFilePicker::modeOpen);
NS_ENSURE_SUCCESS(rv, rv);
// We want to get the file filter from the accept attribute and we add the
// |filterAll| filter to be sure the user has a valid fallback.
@ -392,7 +594,7 @@ nsFileControlFrame::MouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
nsCOMPtr<nsIFile> parentFile;
oldFiles[0]->GetParent(getter_AddRefs(parentFile));
if (parentFile) {
nsCOMPtr<nsILocalFile> parentLocalFile = do_QueryInterface(parentFile, &result);
nsCOMPtr<nsILocalFile> parentLocalFile = do_QueryInterface(parentFile, &rv);
if (parentLocalFile) {
filePicker->SetDisplayDirectory(parentLocalFile);
}
@ -427,9 +629,8 @@ nsFileControlFrame::MouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
// Open dialog
PRInt16 mode;
result = filePicker->Show(&mode);
if (NS_FAILED(result))
return result;
rv = filePicker->Show(&mode);
NS_ENSURE_SUCCESS(rv, rv);
if (mode == nsIFilePicker::returnCancel)
return NS_OK;
@ -445,8 +646,8 @@ nsFileControlFrame::MouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
nsTArray<nsString> newFileNames;
if (multi) {
nsCOMPtr<nsISimpleEnumerator> iter;
result = filePicker->GetFiles(getter_AddRefs(iter));
NS_ENSURE_SUCCESS(result, result);
rv = filePicker->GetFiles(getter_AddRefs(iter));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsISupports> tmp;
PRBool prefSaved = PR_FALSE;
@ -454,15 +655,15 @@ nsFileControlFrame::MouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(tmp);
if (localFile) {
nsString unicodePath;
result = localFile->GetPath(unicodePath);
rv = localFile->GetPath(unicodePath);
if (!unicodePath.IsEmpty()) {
newFileNames.AppendElement(unicodePath);
}
if (!prefSaved) {
// Store the last used directory using the content pref service
result = gUploadLastDir->StoreLastUsedDirectory(doc->GetDocumentURI(),
localFile);
NS_ENSURE_SUCCESS(result, result);
rv = gUploadLastDir->StoreLastUsedDirectory(doc->GetDocumentURI(),
localFile);
NS_ENSURE_SUCCESS(rv, rv);
prefSaved = PR_TRUE;
}
}
@ -470,17 +671,17 @@ nsFileControlFrame::MouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
}
else {
nsCOMPtr<nsILocalFile> localFile;
result = filePicker->GetFile(getter_AddRefs(localFile));
rv = filePicker->GetFile(getter_AddRefs(localFile));
if (localFile) {
nsString unicodePath;
result = localFile->GetPath(unicodePath);
rv = localFile->GetPath(unicodePath);
if (!unicodePath.IsEmpty()) {
newFileNames.AppendElement(unicodePath);
}
// Store the last used directory using the content pref service
result = gUploadLastDir->StoreLastUsedDirectory(doc->GetDocumentURI(),
localFile);
NS_ENSURE_SUCCESS(result, result);
rv = gUploadLastDir->StoreLastUsedDirectory(doc->GetDocumentURI(),
localFile);
NS_ENSURE_SUCCESS(rv, rv);
}
}

View File

@ -42,6 +42,7 @@
#include "nsIFormControlFrame.h"
#include "nsIDOMMouseListener.h"
#include "nsIAnonymousContentCreator.h"
#include "nsICapturePicker.h"
#include "nsCOMPtr.h"
#include "nsTextControlFrame.h"
@ -137,16 +138,30 @@ protected:
// and textfield.
NS_IMETHOD MouseDown(nsIDOMEvent* aMouseEvent) { return NS_OK; }
NS_IMETHOD MouseUp(nsIDOMEvent* aMouseEvent) { return NS_OK; }
NS_IMETHOD MouseClick(nsIDOMEvent* aMouseEvent);
NS_IMETHOD MouseClick(nsIDOMEvent* aMouseEvent) = 0;
NS_IMETHOD MouseDblClick(nsIDOMEvent* aMouseEvent) { return NS_OK; }
NS_IMETHOD MouseOver(nsIDOMEvent* aMouseEvent) { return NS_OK; }
NS_IMETHOD MouseOut(nsIDOMEvent* aMouseEvent) { return NS_OK; }
NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) { return NS_OK; }
private:
protected:
nsFileControlFrame* mFrame;
};
class CaptureMouseListener: public MouseListener {
public:
CaptureMouseListener(nsFileControlFrame* aFrame) : MouseListener(aFrame),
mMode(0) {};
NS_IMETHOD MouseClick(nsIDOMEvent* aMouseEvent);
PRUint32 mMode;
};
class BrowseMouseListener: public MouseListener {
public:
BrowseMouseListener(nsFileControlFrame* aFrame) : MouseListener(aFrame) {};
NS_IMETHOD MouseClick(nsIDOMEvent* aMouseEvent);
};
virtual PRBool IsFrameOfType(PRUint32 aFlags) const
{
return nsBlockFrame::IsFrameOfType(aFlags &
@ -171,10 +186,17 @@ protected:
*/
nsCOMPtr<nsIContent> mBrowse;
/**
* The capture button input.
* @see nsFileControlFrame::CreateAnonymousContent
*/
nsCOMPtr<nsIContent> mCapture;
/**
* Our mouse listener. This makes sure we don't get used after destruction.
*/
nsRefPtr<MouseListener> mMouseListener;
nsRefPtr<BrowseMouseListener> mMouseListener;
nsRefPtr<CaptureMouseListener> mCaptureMouseListener;
private:
/**

View File

@ -0,0 +1,95 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Mozilla camera code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Kyle Huey <me@kylehuey.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
interface nsIDOMWindow;
interface nsIURI;
[scriptable, uuid(a4e2b2de-5712-4f80-aabb-7de3a747f227)]
interface nsICapturePicker : nsISupports
{
const long MODE_STILL = 1; // Capture a still (image)
const long MODE_AUDIO_CLIP = 2; // Capture a clip of audio
const long MODE_VIDEO_CLIP = 3; // Capture a clip of video
const long MODE_VIDEO_NO_SOUND_CLIP = 4; // Capture a clip of video (no sound)
// Return codes from the dialog
const long RETURN_OK = 0;
const long RETURN_CANCEL = 1;
/**
* Initialize the camera picker widget. The camera picker is not valid until this
* method is called.
*
* @param parent nsIDOMWindow parent. This dialog will be dependent
* on this parent. This must not be null.
* @param title The title for the file widget
* @param flags Mode and type flags for what to capture
*
*/
void init(in nsIDOMWindow parent, in AString title, in unsigned long mode);
/**
* Show file dialog. The dialog is displayed modally.
*
* @return returnOK if the user captures the requested content, returnCancel if
* the user cancels the capture process
*/
unsigned long show();
/**
* Determine if the given mode might be available. Consumers should take a
* true value to be a hint of what might be available, not a guarantee.
*
* @param mode Mode to examine
*
* @return false if the requested mode can definitely not be captured,
* true otherwise.
*/
boolean modeMayBeAvailable(in unsigned long mode);
/**
* Get the captured image/video/audio. This may be a data URI, file URI,
* or a moz-filedata reference URI.
*/
readonly attribute nsIURI uri;
// The MIME type of the captured content. Cannot be set after calling show()
attribute AString type;
};