diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-23 23:18:12 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-23 23:18:12 +0000 |
commit | fa89702052f10d610859e2d724a7262743fe7418 (patch) | |
tree | 8a3671e7dd6924bb181cdfbeec96a6f51232a38b | |
parent | 0b0f60d96020eb44648869a7199520fd5b9f75e0 (diff) | |
download | chromium_src-fa89702052f10d610859e2d724a7262743fe7418.zip chromium_src-fa89702052f10d610859e2d724a7262743fe7418.tar.gz chromium_src-fa89702052f10d610859e2d724a7262743fe7418.tar.bz2 |
Adding a new parsing system to the metadata url request.
BUG=none
TEST=none
Original review: http://codereview.chromium.org/155803
Patch by: dhg@google.com
Review URL: http://codereview.chromium.org/160059
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21469 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/net/metadata_url_request.cc | 51 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser.cc | 9 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser.h | 48 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser_factory.h | 27 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser_filebase.cc | 74 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser_filebase.h | 48 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser_jpeg.cc | 14 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser_jpeg.h | 20 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser_jpeg_factory.cc | 29 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser_jpeg_factory.h | 22 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser_manager.cc | 56 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser_manager.h | 40 | ||||
-rw-r--r-- | chrome/chrome.gyp | 11 |
13 files changed, 442 insertions, 7 deletions
diff --git a/chrome/browser/net/metadata_url_request.cc b/chrome/browser/net/metadata_url_request.cc index d901746..a0f33d2 100644 --- a/chrome/browser/net/metadata_url_request.cc +++ b/chrome/browser/net/metadata_url_request.cc @@ -4,12 +4,16 @@ #include "chrome/browser/net/metadata_url_request.h" +#include "base/file_path.h" #include "base/message_loop.h" #include "build/build_config.h" #include "googleurl/src/url_util.h" #include "net/base/io_buffer.h" +#include "net/base/net_util.h" #include "net/url_request/url_request.h" #include "net/url_request/url_request_job.h" +#include "chrome/browser/parsers/metadata_parser_manager.h" +#include "chrome/browser/parsers/metadata_parser.h" namespace { @@ -30,15 +34,16 @@ class MetadataRequestHandler : public URLRequestJob { private: void StartAsync(); - + std::string result_; + bool parsed; int data_offset_; - DISALLOW_COPY_AND_ASSIGN(MetadataRequestHandler); }; MetadataRequestHandler::MetadataRequestHandler(URLRequest* request) : URLRequestJob(request), data_offset_(0) { + parsed = false; } MetadataRequestHandler::~MetadataRequestHandler() { @@ -61,13 +66,47 @@ void MetadataRequestHandler::Kill() { bool MetadataRequestHandler::ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read) { - std::string fake_result = "{ \"foo\": 3.14 }"; + FilePath path; - int remaining = static_cast<int>(fake_result.size()) - data_offset_; + if (!request()->url().is_valid()) { + return false; + } + if (!net::FileURLToFilePath(request()->url(), &path)) { + return false; + } + if (!parsed) { + MetadataParserManager* manager = MetadataParserManager::Get(); + scoped_ptr<MetadataParser> parser(manager->GetParserForFile(path)); + if (parser != NULL) { + result_ = "{\n"; + parser->Parse(); + MetadataPropertyIterator *iter = parser->GetPropertyIterator(); + while (!iter->IsEnd()) { + std::string key; + std::string value; + if (iter->GetNext(&key, &value)) { + result_ += "\""; + result_ += key; + result_ += "\":"; + result_ += "\""; + result_ += value; + result_ += "\",\n"; + } else { + break; + } + } + result_ += "}"; + delete iter; + } else { + result_ = "{}"; + } + parsed = true; + } + int remaining = static_cast<int>(result_.size()) - data_offset_; if (buf_size > remaining) buf_size = remaining; if (buf_size > 0) { - memcpy(buf->data(), &fake_result[data_offset_], buf_size); + memcpy(buf->data(), &result_[data_offset_], buf_size); data_offset_ += buf_size; } *bytes_read = buf_size; @@ -86,8 +125,6 @@ void MetadataRequestHandler::StartAsync() { } // namespace void RegisterMetadataURLRequestHandler() { - // This is currently unfinished. It will eventually provide metadata - // but currently just returns a dummy stub string. #if defined(OS_CHROMEOS) URLRequest::RegisterProtocolFactory(kMetadataScheme, &MetadataRequestHandler::Factory); diff --git a/chrome/browser/parsers/metadata_parser.cc b/chrome/browser/parsers/metadata_parser.cc new file mode 100644 index 0000000..0bf120a --- /dev/null +++ b/chrome/browser/parsers/metadata_parser.cc @@ -0,0 +1,9 @@ +// Copyright (c) 2009 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. + +#include "chrome/browser/parsers/metadata_parser.h" + +const char* MetadataParser::kPropertyFilesize = "filesize"; +const char* MetadataParser::kPropertyType = "type"; +const char* MetadataParser::kPropertyTitle = "title"; diff --git a/chrome/browser/parsers/metadata_parser.h b/chrome/browser/parsers/metadata_parser.h new file mode 100644 index 0000000..97a4e85 --- /dev/null +++ b/chrome/browser/parsers/metadata_parser.h @@ -0,0 +1,48 @@ +// Copyright (c) 2009 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. + +#ifndef CHROME_BROWSER_METADATA_PARSER_H_ +#define CHROME_BROWSER_METADATA_PARSER_H_ + +#include <string> + +#include "base/file_path.h" + +// Allows for Iteration on the Properties of a given file. +class MetadataPropertyIterator { + public: + MetadataPropertyIterator() {} + + // Gets the next Property in the iterator. Returns false if at the end + // of the list. + virtual bool GetNext(std::string* key, std::string* value) = 0; + + // Gets the number of Properties in this iterator. + virtual int Length() = 0; + + // Checks to see if we're at the end of the list. + virtual bool IsEnd() = 0; +}; + +// Represents a single instance of parsing on a particular file. +class MetadataParser { + public: + MetadataParser(const FilePath& path) {} + + static const char* kPropertyType; + static const char* kPropertyFilesize; + static const char* kPropertyTitle; + + // Does all the heavy work of parsing out the file. Blocking until complete. + virtual bool Parse() = 0; + + // Gets a particular property found in a parse call. + virtual bool GetProperty(const std::string& key, std::string* value) = 0; + + // Gets an interator allowing you to iterate over all the properties found + // in a parse call. + virtual MetadataPropertyIterator* GetPropertyIterator() = 0; +}; + +#endif // CHROME_BROWSER-METADATA_PARSER_H_ diff --git a/chrome/browser/parsers/metadata_parser_factory.h b/chrome/browser/parsers/metadata_parser_factory.h new file mode 100644 index 0000000..c42e01d --- /dev/null +++ b/chrome/browser/parsers/metadata_parser_factory.h @@ -0,0 +1,27 @@ +// Copyright (c) 2009 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. + +#ifndef CHROME_BROWSER_METADATA_PARSER_FACTORY_H_ +#define CHROME_BROWSER_METADATA_PARSER_FACTORY_H_ + +#include "chrome/browser/parsers/metadata_parser.h" + +// Used to check to see if a parser can parse a particular file, and allows +// for creation of a parser on a particular file. +class MetadataParserFactory { + public: + MetadataParserFactory() {} + + // Used to check to see if the parser can parse the given file. This + // should not do any additional reading of the file. + virtual bool CanParse(const FilePath& path, + char* bytes, + int bytes_size) = 0; + + // Creates the parser on the given file. Creating the parser does not + // do any parsing on the file. Parse has to be called on the parser. + virtual MetadataParser* CreateParser(const FilePath& path) = 0; +}; + +#endif // CHROME_BROWSER_METADATA_PARSER_FACTORY_H_ diff --git a/chrome/browser/parsers/metadata_parser_filebase.cc b/chrome/browser/parsers/metadata_parser_filebase.cc new file mode 100644 index 0000000..2d515c6 --- /dev/null +++ b/chrome/browser/parsers/metadata_parser_filebase.cc @@ -0,0 +1,74 @@ +// Copyright (c) 2009 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. + +#include <iostream> +#include <sstream> + +#include "chrome/browser/parsers/metadata_parser_filebase.h" + +#include "base/file_util.h" +#include "base/string_util.h" + +FileMetadataParser::FileMetadataParser(const FilePath& path) + : MetadataParser(path){ + path_ = path; +} + +bool FileMetadataParser::Parse() { + std::string value; + int64 size; + if (file_util::GetFileSize(path_, &size)) { + properties_[MetadataParser::kPropertyFilesize] = Int64ToString(size); + } +#if defined(OS_WIN) + value = WideToUTF8(path_.BaseName().value()); + properties_[MetadataParser::kPropertyTitle] = value; +#elif defined(OS_POSIX) + properties_[MetadataParser::kPropertyTitle] = path_.BaseName().value(); +#endif + return true; +} + +bool FileMetadataParser::GetProperty(const std::string&key, + std::string* value) { + PropertyMap::iterator it = properties_.find(key.c_str()); + if (it == properties_.end()) { + return false; + } + + *value = properties_[key.c_str()]; + return true; +} + +MetadataPropertyIterator* FileMetadataParser::GetPropertyIterator() { + return new FileMetadataPropertyIterator(properties_); +} + +FileMetadataPropertyIterator::FileMetadataPropertyIterator( + PropertyMap& properties): properties_(properties){ + it = properties_.begin(); +} + +bool FileMetadataPropertyIterator::GetNext(std::string* key, + std::string* value) { + if (it == properties_.end()) { + return false; + } + *key = it->first; + *value = it->second; + it++; + return true; +} + +int FileMetadataPropertyIterator::Length() { + return properties_.size(); +} + +bool FileMetadataPropertyIterator::IsEnd() { + if (it == properties_.end()) { + return true; + } else { + return false; + } +} diff --git a/chrome/browser/parsers/metadata_parser_filebase.h b/chrome/browser/parsers/metadata_parser_filebase.h new file mode 100644 index 0000000..8b67a2d --- /dev/null +++ b/chrome/browser/parsers/metadata_parser_filebase.h @@ -0,0 +1,48 @@ +// Copyright (c) 2009 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. + +#ifndef CHROME_BROWSER_METADATA_PARSER_FILEBASE_H_ +#define CHROME_BROWSER_METADATA_PARSER_FILEBASE_H_ + +#include "chrome/browser/parsers/metadata_parser.h" + +typedef base::hash_map<std::string, std::string> PropertyMap; + +// Parser for the file type. Allows for parsing of files, and gets +// properties assiciated with files. +class FileMetadataParser : public MetadataParser { + public: + FileMetadataParser(const FilePath& path); + + // Implementation of MetadataParser + virtual bool Parse(); + virtual bool GetProperty(const std::string& key, std::string* value); + + MetadataPropertyIterator* GetPropertyIterator(); + + protected: + PropertyMap properties_; + FilePath path_; + + private: + DISALLOW_COPY_AND_ASSIGN(FileMetadataParser); +}; + +class FileMetadataPropertyIterator : public MetadataPropertyIterator { + public: + FileMetadataPropertyIterator(PropertyMap& properties); + + // Implementation of MetadataPropertyIterator + virtual bool GetNext(std::string* key, std::string* value); + virtual int Length(); + virtual bool IsEnd(); + + private: + PropertyMap& properties_; + PropertyMap::iterator it; + + DISALLOW_COPY_AND_ASSIGN(FileMetadataPropertyIterator); +}; + +#endif // CHROME_BROWSER_METADATA_PARSER_FILEBASE_H_ diff --git a/chrome/browser/parsers/metadata_parser_jpeg.cc b/chrome/browser/parsers/metadata_parser_jpeg.cc new file mode 100644 index 0000000..3e78978 --- /dev/null +++ b/chrome/browser/parsers/metadata_parser_jpeg.cc @@ -0,0 +1,14 @@ +// Copyright (c) 2009 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. + +#include "chrome/browser/parsers/metadata_parser_jpeg.h" + +JpegMetadataParser::JpegMetadataParser(const FilePath& path) + : FileMetadataParser(path) {} + +bool JpegMetadataParser::Parse() { + FileMetadataParser::Parse(); + properties_[MetadataParser::kPropertyType] = "jpeg"; + return true; +} diff --git a/chrome/browser/parsers/metadata_parser_jpeg.h b/chrome/browser/parsers/metadata_parser_jpeg.h new file mode 100644 index 0000000..449e816 --- /dev/null +++ b/chrome/browser/parsers/metadata_parser_jpeg.h @@ -0,0 +1,20 @@ +// Copyright (c) 2009 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. + +#ifndef CHROME_BROWSER_METADATA_PARSER_JPEG_H_ +#define CHROME_BROWSER_METADATA_PARSER_JPEG_H_ + +#include "chrome/browser/parsers/metadata_parser_filebase.h" + +class JpegMetadataParser : public FileMetadataParser { + public: + JpegMetadataParser(const FilePath& path); + // Implementation of MetadataParser + virtual bool Parse(); + + private: + DISALLOW_COPY_AND_ASSIGN(JpegMetadataParser); +}; + +#endif // CHROME_BROWSER_METADATA_PARSER_JPEG_H_ diff --git a/chrome/browser/parsers/metadata_parser_jpeg_factory.cc b/chrome/browser/parsers/metadata_parser_jpeg_factory.cc new file mode 100644 index 0000000..e49cbcb --- /dev/null +++ b/chrome/browser/parsers/metadata_parser_jpeg_factory.cc @@ -0,0 +1,29 @@ +// Copyright (c) 2009 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. + +#include "chrome/browser/parsers/metadata_parser_jpeg_factory.h" + +#include "chrome/browser/parsers/metadata_parser_jpeg.h" +#include "base/logging.h" +#include "base/string_util.h" + +MetadataParserJpegFactory::MetadataParserJpegFactory() + : MetadataParserFactory(){} + +bool MetadataParserJpegFactory::CanParse(const FilePath& path, + char* bytes, + int bytes_size) { +#if defined(OS_WIN) + FilePath::StringType ext = UTF8ToWide(std::string(".jpg")); +#elif defined(OS_POSIX) + FilePath::StringType ext = ".jpg"; +#endif + return path.MatchesExtension(ext); +} + +MetadataParser* MetadataParserJpegFactory::CreateParser(const FilePath& path) { + JpegMetadataParser* parser; + parser = new JpegMetadataParser(path); + return parser; +} diff --git a/chrome/browser/parsers/metadata_parser_jpeg_factory.h b/chrome/browser/parsers/metadata_parser_jpeg_factory.h new file mode 100644 index 0000000..48c059b --- /dev/null +++ b/chrome/browser/parsers/metadata_parser_jpeg_factory.h @@ -0,0 +1,22 @@ +// Copyright (c) 2009 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. + +#ifndef CHROME_BROWSER_METADATA_PARSER_JPEG_FACTORY_H_ +#define CHROME_BROWSER_METADATA_PARSER_JPEG_FACTORY_H_ + +#include "chrome/browser/parsers/metadata_parser_factory.h" + +class MetadataParserJpegFactory : public MetadataParserFactory { + public: + MetadataParserJpegFactory(); + + // Implementation of MetadataParserFactory + virtual bool CanParse(const FilePath& path, char* bytes, int bytes_size); + virtual MetadataParser* CreateParser(const FilePath& path); + + private: + DISALLOW_COPY_AND_ASSIGN(MetadataParserJpegFactory); +}; + +#endif // CHROME_BROWSER_METADATA_PARSER_JPEG_FACTORY_H_ diff --git a/chrome/browser/parsers/metadata_parser_manager.cc b/chrome/browser/parsers/metadata_parser_manager.cc new file mode 100644 index 0000000..3efce7d --- /dev/null +++ b/chrome/browser/parsers/metadata_parser_manager.cc @@ -0,0 +1,56 @@ +// Copyright (c) 2009 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. + +#include "chrome/browser/parsers/metadata_parser_manager.h" + +#include "base/logging.h" +#include "base/file_util.h" +#include "base/singleton.h" +#include "base/stl_util-inl.h" +#include "build/build_config.h" +#include "chrome/browser/parsers/metadata_parser_factory.h" +#include "chrome/browser/parsers/metadata_parser_jpeg_factory.h" + +static const int kAmountToRead = 256; + +// Gets the singleton +MetadataParserManager* MetadataParserManager::Get() { + // Uses the LeakySingletonTrait because cleanup is optional. + return + Singleton<MetadataParserManager, + LeakySingletonTraits<MetadataParserManager> >::get(); + } + +bool MetadataParserManager::RegisterParserFactory( + MetadataParserFactory* parser) { + factories_.push_back(parser); + return true; +} + +MetadataParserManager::MetadataParserManager() { + MetadataParserJpegFactory *factory = new MetadataParserJpegFactory(); + RegisterParserFactory(factory); +} + +MetadataParserManager::~MetadataParserManager() { + STLDeleteContainerPointers(factories_.begin(), factories_.end()); +} + +MetadataParser* MetadataParserManager::GetParserForFile(const FilePath& path) { + + char buffer[kAmountToRead]; + int amount_read = 0; + DLOG(ERROR) << path.value(); + amount_read = file_util::ReadFile(path, buffer, sizeof(buffer)); + if (amount_read <= 0) { + DLOG(ERROR) << "Unable to read file"; + return NULL; + } + for (size_t x = 0; x < factories_.size(); ++x) { + if (factories_[x]->CanParse(path, buffer, amount_read)) { + return factories_[x]->CreateParser(path); + } + } + return NULL; +} diff --git a/chrome/browser/parsers/metadata_parser_manager.h b/chrome/browser/parsers/metadata_parser_manager.h new file mode 100644 index 0000000..3249eb2 --- /dev/null +++ b/chrome/browser/parsers/metadata_parser_manager.h @@ -0,0 +1,40 @@ +// Copyright (c) 2009 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. + +#ifndef CHROME_BROWSER_METADATA_PARSER_MANAGER_H_ +#define CHROME_BROWSER_METADATA_PARSER_MANAGER_H_ + +#include <vector> + +#include "base/basictypes.h" + +class MetadataParserFactory; +class FilePath; +class MetadataParser; + +// Metadata Parser manager is used to find the correct parser for a +// given file. Allows parsers to register themselves. +class MetadataParserManager { + public: + // Creates a new MetadataParserManager. + MetadataParserManager(); + ~MetadataParserManager(); + + // Gets the singleton + static MetadataParserManager* Get(); + + // Adds a new Parser to the manager, when requests come in for a parser + // the manager will loop through the list of parsers, and query each. + bool RegisterParserFactory(MetadataParserFactory* parser); + + // Returns a new metadata parser for a given file. + MetadataParser* GetParserForFile(const FilePath& path); + + private: + std::vector<MetadataParserFactory*> factories_; + + DISALLOW_COPY_AND_ASSIGN(MetadataParserManager); +}; + +#endif // CHROME_BROWSER_METADATA_PARSER_MANAGER_H_ diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index ca3b3c3..c39beda 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -1305,6 +1305,17 @@ 'browser/page_info_model.h', 'browser/page_state.cc', 'browser/page_state.h', + 'browser/parsers/metadata_parser.h', + 'browser/parsers/metadata_parser_factory.h', + 'browser/parsers/metadata_parser_filebase.cc', + 'browser/parsers/metadata_parser_filebase.h', + 'browser/parsers/metadata_parser_jpeg.cc', + 'browser/parsers/metadata_parser_jpeg.h', + 'browser/parsers/metadata_parser_jpeg_factory.cc', + 'browser/parsers/metadata_parser_jpeg_factory.h', + 'browser/parsers/metadata_parser_manager.cc', + 'browser/parsers/metadata_parser_manager.h', + 'browser/parsers/metadata_parser.cc', 'browser/password_manager/encryptor_linux.cc', 'browser/password_manager/encryptor_mac.mm', 'browser/password_manager/encryptor_win.cc', |