Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #21199 from dmarcos/bug1024692master
Browse files Browse the repository at this point in the history
Bug 1024692: limit image decode size to 3mp on 256mb devices
  • Loading branch information
dmarcos committed Jul 9, 2014
2 parents d9c990c + 3bae741 commit abf2a23
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 122 deletions.
8 changes: 2 additions & 6 deletions apps/camera/js/app.js
Expand Up @@ -16,7 +16,6 @@ var ZoomBarView = require('views/zoom-bar');
var bindAll = require('lib/bind-all');
var debug = require('debug')('app');
var HudView = require('views/hud');
var Pinch = require('lib/pinch');
var bind = require('lib/bind');
var model = require('model');

Expand Down Expand Up @@ -58,7 +57,8 @@ function App(options) {
this.settings = options.settings;
this.camera = options.camera;
this.activity = {};

this.sounds = options.sounds;
this.Pinch = options.Pinch;
//
// If the system app is opening an attention screen (because
// of an incoming call or an alarm, e.g.) and if we are
Expand Down Expand Up @@ -201,10 +201,6 @@ App.prototype.bindEvents = function() {
bind(this.win, 'beforeunload', this.onBeforeUnload);
bind(this.el, 'click', this.onClick);

// Pinch
this.pinch = new Pinch(this.el);
this.pinch.on('pinchchanged', this.firer('pinchchanged'));

debug('events bound');
};

Expand Down
10 changes: 2 additions & 8 deletions apps/camera/js/config/config.js
Expand Up @@ -7,8 +7,8 @@ module.exports = {
// shared/js/media/media_frame.js
globals : {
// The maximum picture size that camera is allowed to take
CONFIG_MAX_IMAGE_PIXEL_SIZE: 5242880,
CONFIG_MAX_SNAPSHOT_PIXEL_SIZE: 5242880,
CONFIG_MAX_IMAGE_PIXEL_SIZE: 3145728, // 3MP
CONFIG_MAX_SNAPSHOT_PIXEL_SIZE: 5242880, // 5MP

// Size of the exif preview embeded in images taken by camera
CONFIG_REQUIRED_EXIF_PREVIEW_WIDTH: 0,
Expand Down Expand Up @@ -45,12 +45,6 @@ module.exports = {
},

previewGallery: {

// Flag for determining if the preview should limit the
// image size to the value of CONFIG_MAX_IMAGE_PIXEL_SIZE
// (enable for devices with limited memory)
limitMaxPreviewSize: false,

// Dimensions for thumbnail image (will automatically be
// multiplied by the devicePixelRatio)
thumbnailWidth: 54,
Expand Down
34 changes: 15 additions & 19 deletions apps/camera/js/controllers/camera.js
Expand Up @@ -43,6 +43,7 @@ CameraController.prototype.bindEvents = function() {

// Relaying camera events means other modules
// don't have to depend directly on camera
camera.on('change:previewActive', this.app.firer('camera:previewactive'));
camera.on('change:videoElapsed', app.firer('camera:recorderTimeUpdate'));
camera.on('autofocuschanged', app.firer('camera:autofocuschanged'));
camera.on('focusconfigured', app.firer('camera:focusconfigured'));
Expand All @@ -62,10 +63,10 @@ CameraController.prototype.bindEvents = function() {

// App
app.on('viewfinder:focuspointchanged', this.onFocusPointChanged);
app.on('previewgallery:opened', this.onPreviewGalleryOpened);
app.on('previewgallery:closed', this.onPreviewGalleryClosed);
app.on('change:batteryStatus', this.onBatteryStatusChange);
app.on('settings:configured', this.onSettingsConfigured);
app.on('previewgallery:opened', this.shutdownCamera);
app.on('previewgallery:closed', this.onGalleryClosed);
app.on('storage:changed', this.onStorageChanged);
app.on('activity:pick', this.onPickActivity);
app.on('timer:ended', this.capture);
Expand Down Expand Up @@ -341,27 +342,22 @@ CameraController.prototype.onStorageChanged = function(state) {
};

/**
* Resets the camera zoom and stops focus when the preview gallery
* is opened.
* Updates focus area when the user clicks on the viewfinder
*/
CameraController.prototype.onPreviewGalleryOpened = function() {
this.camera.configureZoom(this.camera.previewSize());
this.camera.stopFocus();
CameraController.prototype.onFocusPointChanged = function(focusPoint) {
this.camera.updateFocusArea(focusPoint.area);
};

/**
* Resumes focus when the preview gallery
* is opened.
*/
CameraController.prototype.onPreviewGalleryClosed = function() {
this.camera.resumeFocus();
CameraController.prototype.shutdownCamera = function() {
this.camera.stopRecording();
this.camera.set('previewActive', false);
this.camera.set('focus', 'none');
this.camera.release();
};

/**
* Updates focus area when the user clicks on the viewfinder
*/
CameraController.prototype.onFocusPointChanged = function(focusPoint) {
this.camera.updateFocusArea(focusPoint.area);
CameraController.prototype.onGalleryClosed = function() {
this.app.showLoading();
this.camera.load(this.app.clearLoading);
};

});
});
7 changes: 1 addition & 6 deletions apps/camera/js/controllers/confirm.js
Expand Up @@ -51,13 +51,8 @@ ConfirmController.prototype.renderView = function() {
return;
}

// Check whether the MediaFrame should limit the pixel size.
var maxPreviewSize =
this.settings.previewGallery.get('limitMaxPreviewSize') ?
window.CONFIG_MAX_IMAGE_PIXEL_SIZE : 0;

this.confirmView = new this.ConfirmView();
this.confirmView.maxPreviewSize = maxPreviewSize;
this.confirmView.maxPreviewSize = window.CONFIG_MAX_IMAGE_PIXEL_SIZE;
this.confirmView.render().appendTo(this.container);

this.confirmView.on('click:select', this.onSelectMedia);
Expand Down
9 changes: 2 additions & 7 deletions apps/camera/js/controllers/preview-gallery.js
Expand Up @@ -63,10 +63,7 @@ PreviewGalleryController.prototype.openPreview = function() {
return;
}

// Check whether the MediaFrame should limit the pixel size.
var maxPreviewSize =
this.settings.previewGallery.get('limitMaxPreviewSize') ?
window.CONFIG_MAX_IMAGE_PIXEL_SIZE : 0;
var maxPreviewSize = window.CONFIG_MAX_IMAGE_PIXEL_SIZE;

this.view = new PreviewGalleryView();
this.view.maxPreviewSize = maxPreviewSize;
Expand All @@ -84,7 +81,6 @@ PreviewGalleryController.prototype.openPreview = function() {
this.view.set('secure-mode', secureMode);
this.view.open();

this.app.set('previewGalleryOpen', true);
this.previewItem();
this.app.emit('previewgallery:opened');
};
Expand All @@ -104,7 +100,6 @@ PreviewGalleryController.prototype.closePreview = function() {
this.view = null;
}

this.app.set('previewGalleryOpen', false);
this.app.emit('previewgallery:closed');
};

Expand Down Expand Up @@ -336,7 +331,7 @@ PreviewGalleryController.prototype.previewItem = function() {
* @param {Object} filepath
*/
PreviewGalleryController.prototype.onItemDeleted = function(data) {

// Check if this event is being stopped such as in the case
// of resizing an image for a share activity.
if (this.stopItemDeletedEvent) {
Expand Down
45 changes: 38 additions & 7 deletions apps/camera/js/controllers/viewfinder.js
Expand Up @@ -38,6 +38,7 @@ function ViewfinderController(app) {
this.views.focus.appendTo(this.views.viewfinder.el);
this.views.faces = new FacesView();
this.views.faces.appendTo(this.views.viewfinder.el);
this.pinch = new app.Pinch(app.el);
this.bindEvents();
this.configure();
debug('initialized');
Expand Down Expand Up @@ -111,11 +112,14 @@ ViewfinderController.prototype.bindEvents = function() {
this.app.on('camera:ready', this.views.viewfinder.enable);
this.app.on('previewgallery:closed', this.onPreviewGalleryClosed);
this.app.on('camera:configured', this.onCameraConfigured);
this.app.on('previewgallery:opened', this.stopStream);
this.app.on('settings:closed', this.configureGrid);
this.app.on('settings:opened', this.hideGrid);
this.app.on('previewgallery:opened', this.onGalleryOpened);
this.app.on('previewgallery:closed', this.onGalleryClosed);
this.app.on('camera:previewactive', this.onPreviewActive);
this.app.on('settings:closed', this.onSettingsClosed);
this.app.on('settings:opened', this.onSettingsOpened);
this.app.on('hidden', this.stopStream);
this.app.on('pinchchanged', this.onPinchChanged);

this.pinch.on('pinchchanged', this.onPinchChanged);
};

/**
Expand All @@ -126,7 +130,7 @@ ViewfinderController.prototype.bindEvents = function() {
*/
ViewfinderController.prototype.onCameraConfigured = function() {
debug('configuring');
this.startStream();
this.loadStream();
this.configurePreview();

// BUG: We have to use a 300ms timeout here
Expand Down Expand Up @@ -195,8 +199,7 @@ ViewfinderController.prototype.onPreviewGalleryClosed = function() {
*
* @private
*/
ViewfinderController.prototype.startStream = function() {
if (this.app.get('previewGalleryOpen')) { return; }
ViewfinderController.prototype.loadStream = function() {
this.camera.loadStreamInto(this.views.viewfinder.els.video);
debug('stream started');
};
Expand Down Expand Up @@ -304,4 +307,32 @@ ViewfinderController.prototype.onViewfinderClicked = function(e) {
this.app.emit('viewfinder:focuspointchanged', focusPoint);
};

ViewfinderController.prototype.onSettingsOpened = function() {
this.hideGrid();
this.pinch.disable();
};

ViewfinderController.prototype.onSettingsClosed = function() {
this.configureGrid();
this.pinch.enable();
};

ViewfinderController.prototype.onGalleryOpened = function() {
// Disables events when the gallery opens
this.viewfinder.disable();
this.pinch.disable();
};

ViewfinderController.prototype.onGalleryClosed = function() {
// Renables events when the gallery closes
this.viewfinder.enable();
this.pinch.enable();
};

ViewfinderController.prototype.onPreviewActive = function(active) {
if (!active) {
this.stopStream();
}
};

});
14 changes: 11 additions & 3 deletions apps/camera/js/lib/pinch.js
Expand Up @@ -48,8 +48,16 @@ Pinch.prototype.detach = function() {
this.el = null;
};

Pinch.prototype.enable = function() {
this.disabled = false;
};

Pinch.prototype.disable = function() {
this.disabled = true;
};

Pinch.prototype.onTouchStart = function(evt) {
if (evt.touches.length !== 2) {
if (evt.touches.length !== 2 || this.disabled) {
return;
}

Expand All @@ -60,7 +68,7 @@ Pinch.prototype.onTouchStart = function(evt) {
};

Pinch.prototype.onTouchMove = function(evt) {
if (!this.isPinching) {
if (!this.isPinching || this.disabled) {
return;
}

Expand All @@ -75,7 +83,7 @@ Pinch.prototype.onTouchMove = function(evt) {
};

Pinch.prototype.onTouchEnd = function(evt) {
if (!this.isPinching) {
if (!this.isPinching || this.disabled) {
return;
}

Expand Down
6 changes: 4 additions & 2 deletions apps/camera/js/main.js
Expand Up @@ -10,6 +10,7 @@ var GeoLocation = require('lib/geo-location');
var settingsData = require('config/config');
var settings = new Settings(settingsData);
var Camera = require('lib/camera/camera');
var Pinch = require('lib/pinch');
var App = require('app');

// Log dom-loaded to keep perf on our radar
Expand All @@ -28,6 +29,7 @@ if (settingsData.globals) {
var app = window.app = new App({
settings: settings,
geolocation: new GeoLocation(),
Pinch: Pinch,

el: document.body,
doc: document,
Expand Down Expand Up @@ -55,7 +57,7 @@ var app = window.app = new App({
confirm: 'controllers/confirm',
battery: 'controllers/battery',
sounds: 'controllers/sounds',
timer: 'controllers/timer',
timer: 'controllers/timer'
}
});

Expand All @@ -66,4 +68,4 @@ app.camera.load();
app.settings.fetch();
app.boot();

});
});
3 changes: 1 addition & 2 deletions apps/camera/style/viewfinder.css
Expand Up @@ -25,8 +25,7 @@
/**
* disabled
*/

.viewfinder[enabled=false] {
.viewfinder.disabled {
pointer-events: none;
}

Expand Down
15 changes: 0 additions & 15 deletions apps/camera/test/unit/controllers/camera_test.js
Expand Up @@ -369,21 +369,6 @@ suite('controllers/camera', function() {
});
});

suite('CameraController#onPreviewGalleryOpened()', function() {
test('Should configure zoom and stop focus', function() {
this.controller.onPreviewGalleryOpened();
assert.ok(this.camera.configureZoom.called);
assert.ok(this.camera.stopFocus.called);
});
});

suite('CameraController#onPreviewGalleryClosed()', function() {
test('Should resume focus', function() {
this.controller.onPreviewGalleryClosed();
assert.ok(this.camera.resumeFocus.called);
});
});

suite('CameraController#capture()', function() {
test('Should not start countdown if now timer setting is set', function() {
this.app.settings.timer.selected.returns(0);
Expand Down
12 changes: 9 additions & 3 deletions apps/camera/test/unit/controllers/preview-gallery_test.js
Expand Up @@ -338,18 +338,24 @@ suite('controllers/preview-gallery', function() {
this.controller.openPreview();
});

test('Should set `previewGalleryOpen` to `true` on app', function() {
assert.isTrue(this.app.set.calledWith('previewGalleryOpen', true));
test('Should call previewItem', function() {
assert.isTrue(this.controller.previewItem.called);
});
});

suite('PreviewGalleryController#closePreview()', function() {
setup(function() {
this.controller.view = new this.PreviewGalleryView();
this.previewGalleryView = this.controller.view;
this.controller.view.close = sinon.spy();
this.controller.view.destroy = sinon.spy();
this.controller.closePreview();
});

test('Should set `previewGalleryOpen` to `false` on app', function() {
assert.isTrue(this.app.set.calledWith('previewGalleryOpen', false));
assert.isTrue(this.previewGalleryView.close.called);
assert.isTrue(this.previewGalleryView.destroy.called);
assert.isTrue(this.controller.view === null);
});
});

Expand Down

0 comments on commit abf2a23

Please sign in to comment.