Commit ef12ed3c authored by vandebo@chromium.org's avatar vandebo@chromium.org

Media Galleries API: hookup the AllGalleries permission and add tests.

BUG=NONE
TEST=NONE


Review URL: https://chromiumcodereview.appspot.com/10822002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148694 0039d316-1c4b-4281-b951-d872f2087c98
parent abb1b86e
......@@ -60,7 +60,7 @@ bool MediaGalleriesGetMediaFileSystemsFunction::RunImpl() {
chrome::MediaFileSystemRegistry* media_fs_registry =
MediaFileSystemRegistry::GetInstance();
const std::vector<MediaFileSystemRegistry::MediaFSIDAndPath> filesystems =
media_fs_registry->GetMediaFileSystems(rph);
media_fs_registry->GetMediaFileSystems(rph, *GetExtension());
const int child_id = rph->GetID();
base::ListValue* list = new base::ListValue();
......
......@@ -3,21 +3,88 @@
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/platform_app_browsertest_util.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#if defined(OS_LINUX)
#include <fstream>
#include "base/environment.h"
#include "base/scoped_temp_dir.h"
#endif
namespace {
class MediaGalleriesApiTest: public PlatformAppBrowserTest {
class ExperimentalMediaGalleriesApiTest : public ExtensionApiTest {
public:
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
PlatformAppBrowserTest::SetUpCommandLine(command_line);
ExtensionApiTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
}
};
class EnsurePictureDirectoryExists {
public:
EnsurePictureDirectoryExists() {
Init();
}
private:
void Init() {
#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
return;
#elif defined(OS_LINUX)
// On Linux, the picture directory probably doesn't exist by default,
// so we override the settings to point to a tempdir.
ASSERT_TRUE(xdg_dir_.CreateUniqueTempDir());
FilePath config_file(xdg_dir_.path().Append("user-dirs.dirs"));
std::ofstream file;
file.open(config_file.value().c_str());
ASSERT_TRUE(file.is_open());
file << "XDG_PICTURES_DIR=\"" << xdg_dir_.path().value() << "\"";
file.close();
scoped_ptr<base::Environment> env(base::Environment::Create());
env->SetVar("XDG_CONFIG_HOME", xdg_dir_.path().value());
#else
FilePath pictures_path;
ASSERT_TRUE(PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path));
ASSERT_TRUE(file_util::DirectoryExists(pictures_path));
#endif
}
#if defined(OS_LINUX)
ScopedTempDir xdg_dir_;
#endif
};
} // namespace
IN_PROC_BROWSER_TEST_F(MediaGalleriesApiTest, MediaGalleries) {
ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries")) << message_;
IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, NoGalleries) {
ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/no_galleries"))
<< message_;
}
IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MediaGalleriesRead) {
EnsurePictureDirectoryExists picture_directory;
ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/read_access"))
<< message_;
}
IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MediaGalleriesNoAccess) {
EnsurePictureDirectoryExists picture_directory;
ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/no_access"))
<< message_;
}
IN_PROC_BROWSER_TEST_F(ExperimentalMediaGalleriesApiTest,
ExperimentalMediaGalleries) {
ASSERT_TRUE(RunExtensionTest("media_galleries/experimental")) << message_;
}
......@@ -8,9 +8,12 @@
#include <set>
#include "base/file_path.h"
#include "base/path_service.h"
#include "base/system_monitor/system_monitor.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
......@@ -28,6 +31,22 @@ using content::BrowserThread;
using content::RenderProcessHost;
using fileapi::IsolatedContext;
namespace {
bool IsGalleryPermittedForExtension(const extensions::Extension& extension,
SystemMonitor::MediaDeviceType type,
const FilePath::StringType& location) {
if (extension.HasAPIPermission(
extensions::APIPermission::kMediaGalleriesAllGalleries)) {
return true;
}
// TODO(vandebo) Check with prefs for permission to this gallery.
return false;
}
} // namespace
/******************
* Public methods
******************/
......@@ -39,7 +58,8 @@ MediaFileSystemRegistry* MediaFileSystemRegistry::GetInstance() {
std::vector<MediaFileSystemRegistry::MediaFSIDAndPath>
MediaFileSystemRegistry::GetMediaFileSystems(
const content::RenderProcessHost* rph) {
const content::RenderProcessHost* rph,
const extensions::Extension& extension) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
std::vector<MediaFSIDAndPath> results;
......@@ -51,7 +71,10 @@ MediaFileSystemRegistry::GetMediaFileSystems(
// file system mappings.
RegisterForRPHGoneNotifications(rph);
FilePath pictures_path;
if (PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path)) {
// TODO(vandebo) file system galleries need a unique id as well.
if (PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path) &&
IsGalleryPermittedForExtension(extension, SystemMonitor::TYPE_PATH,
pictures_path.value())) {
std::string fsid = RegisterPathAsFileSystem(pictures_path);
child_it->second.insert(std::make_pair(pictures_path, fsid));
}
......@@ -62,7 +85,9 @@ MediaFileSystemRegistry::GetMediaFileSystems(
const std::vector<SystemMonitor::MediaDeviceInfo> media_devices =
monitor->GetAttachedMediaDevices();
for (size_t i = 0; i < media_devices.size(); ++i) {
if (media_devices[i].type == SystemMonitor::TYPE_PATH) {
if (media_devices[i].type == SystemMonitor::TYPE_PATH &&
IsGalleryPermittedForExtension(extension, media_devices[i].type,
media_devices[i].location)) {
FilePath path(media_devices[i].location);
device_id_map_.insert(std::make_pair(media_devices[i].unique_id, path));
const std::string fsid = RegisterPathAsFileSystem(path);
......
......@@ -25,6 +25,10 @@ namespace content {
class RenderProcessHost;
}
namespace extensions {
class Extension;
}
namespace fileapi {
class IsolatedContext;
}
......@@ -44,7 +48,8 @@ class MediaFileSystemRegistry
// Returns the list of media filesystem IDs and paths for a given RPH.
// Called on the UI thread.
std::vector<MediaFSIDAndPath> GetMediaFileSystems(
const content::RenderProcessHost* rph);
const content::RenderProcessHost* rph,
const extensions::Extension& extension);
// base::SystemMonitor::DevicesChangedObserver implementation.
virtual void OnMediaDeviceDetached(const std::string& id) OVERRIDE;
......
......@@ -3,10 +3,8 @@
"version": "0.1",
"manifest_version": 2,
"description": "end-to-end browser test for chrome.experimental.mediaGalleries API",
"app": {
"background": {
"scripts": ["test.js"]
}
"background": {
"scripts": ["test.js"]
},
"permissions": ["experimental", "mediaGalleriesRead"]
"permissions": ["experimental"]
}
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var experimentalMediaGalleries = chrome.experimental.mediaGalleries;
var nullCallback = function(result) {
chrome.test.assertEq(null, result);
};
chrome.test.runTests([
function extractEmbeddedThumbnails() {
var result = experimentalMediaGalleries.extractEmbeddedThumbnails({});
chrome.test.assertEq(null, result);
chrome.test.succeed();
},
function assembleMediaFile() {
experimentalMediaGalleries.assembleMediaFile(
new Blob, {},
chrome.test.callbackPass(nullCallback));
},
]);
{
"name": "chrome.mediaGalleries.no_access",
"version": "0.1",
"manifest_version": 2,
"description": "test having no access for chrome.mediaGalleries.getMediaFileSystems fails appropriately",
"app": {
"background": {
"scripts": ["test.js"]
}
},
"permissions": ["mediaGalleries", "mediaGalleriesAllGalleries"]
}
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var mediaGalleries = chrome.mediaGalleries;
var mediaFileSystemsDirectoryEntryCallback = function(entries) {
chrome.test.fail("Shouldn't have been able to get a directory listing.");
};
var mediaFileSystemsListCallback = function(results) {
// There should be a "Pictures" directory on all desktop platforms.
var expectedFileSystems = 1;
// But not on Android and ChromeOS.
if (/Android/.test(navigator.userAgent) || /CrOS/.test(navigator.userAgent))
expectedFileSystems = 0;
chrome.test.assertEq(expectedFileSystems, results.length);
if (expectedFileSystems) {
var dir_reader = results[0].root.createReader();
dir_reader.readEntries(mediaFileSystemsDirectoryEntryCallback,
chrome.test.callbackPass());
}
};
chrome.test.runTests([
function getGalleries() {
mediaGalleries.getMediaFileSystems(
chrome.test.callbackPass(mediaFileSystemsListCallback));
},
]);
{
"name": "chrome.mediaGalleries.no_galleries",
"version": "0.1",
"manifest_version": 2,
"description": "Test that without the allGalleries permission, no galleries are present",
"app": {
"background": {
"scripts": ["test.js"]
}
},
"permissions": ["mediaGalleriesRead"]
}
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var mediaGalleries = chrome.mediaGalleries;
var mediaFileSystemsListCallback = function(results) {
chrome.test.assertEq(0, results.length);
};
chrome.test.runTests([
function getGalleries() {
mediaGalleries.getMediaFileSystems(
chrome.test.callbackPass(mediaFileSystemsListCallback));
},
]);
{
"name": "chrome.mediaGalleries.read_access",
"version": "0.1",
"manifest_version": 2,
"description": "test read access for chrome.mediaGalleries.getMediaFileSystems",
"app": {
"background": {
"scripts": ["test.js"]
}
},
"permissions": ["mediaGalleriesRead", "mediaGalleriesAllGalleries"]
}
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var mediaGalleries = chrome.mediaGalleries;
var mediaFileSystemsDirectoryErrorCallback = function(err) {
chrome.test.fail("Couldn't read from directory: " + err);
};
var mediaFileSystemsListCallback = function(results) {
// There should be a "Pictures" directory on all desktop platforms.
var expectedFileSystems = 1;
// But not on Android and ChromeOS.
if (/Android/.test(navigator.userAgent) || /CrOS/.test(navigator.userAgent))
expectedFileSystems = 0;
chrome.test.assertEq(expectedFileSystems, results.length);
if (expectedFileSystems) {
var dir_reader = results[0].root.createReader();
dir_reader.readEntries(chrome.test.callbackPass(),
mediaFileSystemsDirectoryErrorCallback);
}
};
chrome.test.runTests([
function getGalleries() {
mediaGalleries.getMediaFileSystems(
chrome.test.callbackPass(mediaFileSystemsListCallback));
},
]);
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
chrome.experimental.app.onLaunched.addListener(function() {
var experimentalMediaGalleries = chrome.experimental.mediaGalleries;
var mediaGalleries = chrome.mediaGalleries;
var mediaFileSystemsListCallback = function(results) {
// There should be a "Pictures" directory on all desktop platforms.
var expectedFileSystems = 1;
// But not on Android and ChromeOS.
if (/Android/.test(navigator.userAgent) || /CrOS/.test(navigator.userAgent))
expectedFileSystems = 0;
chrome.test.assertEq(expectedFileSystems, results.length);
// TODO(vandebo) Test that we can read from the file system object.
};
var nullCallback = function(result) {
chrome.test.assertEq(null, result);
};
function runTests(tests) {
chrome.test.getConfig(function(config) {
operatingSystem = config.osName;
chrome.test.runTests(tests);
});
}
chrome.test.runTests([
function getGalleries() {
mediaGalleries.getMediaFileSystems(
chrome.test.callbackPass(mediaFileSystemsListCallback));
},
function extractEmbeddedThumbnails() {
var result = experimentalMediaGalleries.extractEmbeddedThumbnails({});
chrome.test.assertEq(null, result);
chrome.test.succeed();
},
function assembleMediaFile() {
experimentalMediaGalleries.assembleMediaFile(
new Blob, {},
chrome.test.callbackPass(nullCallback));
},
]);
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment