diff options
author | initial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-07-26 23:55:29 +0000 |
---|---|---|
committer | initial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-07-26 23:55:29 +0000 |
commit | 09911bf300f1a419907a9412154760efd0b7abc3 (patch) | |
tree | f131325fb4e2ad12c6d3504ab75b16dd92facfed /chrome/browser/gears_integration.cc | |
parent | 586acc5fe142f498261f52c66862fa417c3d52d2 (diff) | |
download | chromium_src-09911bf300f1a419907a9412154760efd0b7abc3.zip chromium_src-09911bf300f1a419907a9412154760efd0b7abc3.tar.gz chromium_src-09911bf300f1a419907a9412154760efd0b7abc3.tar.bz2 |
Add chrome to the repository.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/gears_integration.cc')
-rw-r--r-- | chrome/browser/gears_integration.cc | 331 |
1 files changed, 331 insertions, 0 deletions
diff --git a/chrome/browser/gears_integration.cc b/chrome/browser/gears_integration.cc new file mode 100644 index 0000000..d236272 --- /dev/null +++ b/chrome/browser/gears_integration.cc @@ -0,0 +1,331 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "chrome/browser/gears_integration.h" + +#include "base/gfx/png_encoder.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/ref_counted.h" +#include "base/string_util.h" +#include "chrome/browser/chrome_plugin_host.h" +#include "chrome/common/chrome_plugin_util.h" +#include "chrome/common/gears_api.h" +#include "googleurl/src/gurl.h" +#include "net/base/base64.h" +#include "skia/include/SkBitmap.h" +#include "webkit/glue/dom_operations.h" + +// The following 2 helpers are borrowed from the Gears codebase. + +const size_t kUserPathComponentMaxChars = 64; + +// Returns true if and only if the char meets the following criteria: +// +// - visible ASCII +// - None of the following characters: / \ : * ? " < > | ; , +// +// This function is a heuristic that should identify most strings that are +// invalid pathnames on popular OSes. It's both overinclusive and +// underinclusive, though. +template<class CharT> +inline bool IsCharValidInPathComponent(CharT c) { + // Not visible ASCII? + // Note: the Gears version of this function excludes spaces (32) as well. We + // allow them for file names. + if (c < 32 || c >= 127) { + return false; + } + + // Illegal characters? + switch (c) { + case '/': + case '\\': + case ':': + case '*': + case '?': + case '"': + case '<': + case '>': + case '|': + case ';': + case ',': + return false; + + default: + return true; + } +} + +// Modifies a string, replacing characters that are not valid in a file path +// component with the '_' character. Also replaces leading and trailing dots +// with the '_' character. +// See IsCharValidInPathComponent +template<class StringT> +inline void EnsureStringValidPathComponent(StringT &s) { + if (s.empty()) { + return; + } + + typename StringT::iterator iter = s.begin(); + typename StringT::iterator end = s.end(); + + // Does it start with a dot? + if (*iter == '.') { + *iter = '_'; + ++iter; // skip it in the loop below + } + // Is every char valid? + while (iter != end) { + if (!IsCharValidInPathComponent(*iter)) { + *iter = '_'; + } + ++iter; + } + // Does it end with a dot? + --iter; + if (*iter == '.') { + *iter = '_'; + } + + // Is it too long? + if (s.size() > kUserPathComponentMaxChars) + s.resize(kUserPathComponentMaxChars); +} + +void GearsSettingsPressed(HWND parent_hwnd) { + CPBrowsingContext context = static_cast<CPBrowsingContext>( + reinterpret_cast<uintptr_t>(parent_hwnd)); + CPHandleCommand(GEARSPLUGINCOMMAND_SHOW_SETTINGS, NULL, context); +} + +// Gears only supports certain icon sizes. +enum GearsIconSizes { + SIZE_16x16 = 0, + SIZE_32x32, + SIZE_48x48, + SIZE_128x128, + NUM_GEARS_ICONS, +}; + +// Helper function to convert a 16x16 favicon to a data: URL with the icon +// encoded as a PNG. +static GURL ConvertSkBitmapToDataURL(const SkBitmap& icon) { + DCHECK(!icon.isNull()); + DCHECK(icon.width() == 16 && icon.height() == 16); + + // Get the FavIcon data. + std::vector<unsigned char> icon_data; + { + SkAutoLockPixels icon_lock(icon); + PNGEncoder::Encode(static_cast<unsigned char*>(icon.getPixels()), + PNGEncoder::FORMAT_BGRA, icon.width(), + icon.height(), icon.width()* 4, false, + &icon_data); + } + + // Base64-encode it (to make it a data URL). + std::string icon_data_str(reinterpret_cast<char*>(&icon_data[0]), + icon_data.size()); + std::string icon_base64_encoded; + Base64Encode(icon_data_str, &icon_base64_encoded); + GURL icon_url("data:image/png;base64," + icon_base64_encoded); + + return icon_url; +} + +// This class holds and manages the data passed to the +// GEARSPLUGINCOMMAND_CREATE_SHORTCUT plugin command. +class CreateShortcutCommand : public CPCommandInterface { + public: + CreateShortcutCommand( + const std::string& name, const std::string& url, + const std::string& description, + const std::vector<webkit_glue::WebApplicationInfo::IconInfo> &icons, + const SkBitmap& fallback_icon, + GearsCreateShortcutCallback* callback) + : name_(name), url_(url), description_(description), callback_(callback), + calling_loop_(MessageLoop::current()) { + // shortcut_data_ has the same lifetime as our strings, so we just point it + // at their internal data. + memset(&shortcut_data_, 0, sizeof(shortcut_data_)); + shortcut_data_.name = name_.c_str(); + shortcut_data_.url = url_.c_str(); + shortcut_data_.description = description_.c_str(); + + // Search the icons array for Gears-supported sizes and copy the strings. + bool has_icon = false; + + for (size_t i = 0; i < icons.size(); ++i) { + const webkit_glue::WebApplicationInfo::IconInfo& icon = icons[i]; + if (icon.width == 16 && icon.height == 16) { + has_icon = true; + InitIcon(SIZE_16x16, icon.url, 16, 16); + } else if (icon.width == 32 && icon.height == 32) { + has_icon = true; + InitIcon(SIZE_32x32, icon.url, 32, 32); + } else if (icon.width == 48 && icon.height == 48) { + has_icon = true; + InitIcon(SIZE_48x48, icon.url, 48, 48); + } else if (icon.width == 128 && icon.height == 128) { + has_icon = true; + InitIcon(SIZE_128x128, icon.url, 128, 128); + } + } + + if (!has_icon) { + // Fall back to the favicon only if the site provides no icons at all. We + // assume if a site provides any icons, it wants to override default + // behavior. + InitIcon(SIZE_16x16, ConvertSkBitmapToDataURL(fallback_icon), 16, 16); + } + + shortcut_data_.command_interface = this; + } + virtual ~CreateShortcutCommand() { } + + // CPCommandInterface + virtual void* GetData() { return &shortcut_data_; } + virtual void OnCommandInvoked(CPError retval) { + if (retval != CPERR_IO_PENDING) { + // Older versions of Gears don't send a response, so don't wait for one. + OnCommandResponse(CPERR_FAILURE); + } + } + virtual void OnCommandResponse(CPError retval) { + calling_loop_->PostTask(FROM_HERE, NewRunnableMethod( + this, &CreateShortcutCommand::ReportResults, retval)); + } + + private: + void ReportResults(CPError retval) { + callback_->Run(shortcut_data_, retval == CPERR_SUCCESS); + delete this; + } + + void InitIcon(GearsIconSizes size, const GURL& url, int width, int height) { + icon_urls_[size] = url.spec(); // keeps the string memory in scope + shortcut_data_.icons[size].url = icon_urls_[size].c_str(); + shortcut_data_.icons[size].width = width; + shortcut_data_.icons[size].height = height; + } + + GearsCreateShortcutData shortcut_data_; + std::string name_; + std::string url_; + std::string description_; + std::string icon_urls_[NUM_GEARS_ICONS]; + scoped_ptr<GearsCreateShortcutCallback> callback_; + MessageLoop* calling_loop_; +}; + +// Allows InvokeLater without adding refcounting. The object is only deleted +// when its last InvokeLater is run anyway. +void RunnableMethodTraits<CreateShortcutCommand>::RetainCallee( + CreateShortcutCommand* remover) { +} +void RunnableMethodTraits<CreateShortcutCommand>::ReleaseCallee( + CreateShortcutCommand* remover) { +} + +void GearsCreateShortcut( + const webkit_glue::WebApplicationInfo& app_info, + const std::wstring& fallback_name, + const GURL& fallback_url, + const SkBitmap& fallback_icon, + GearsCreateShortcutCallback* callback) { + std::wstring name = + !app_info.title.empty() ? app_info.title : fallback_name; + EnsureStringValidPathComponent(name); + + std::string name_utf8 = WideToUTF8(name); + std::string description_utf8 = WideToUTF8(app_info.description); + const GURL& url = + !app_info.app_url.is_empty() ? app_info.app_url : fallback_url; + + CreateShortcutCommand* command = + new CreateShortcutCommand(name_utf8, url.spec(), description_utf8, + app_info.icons, fallback_icon, callback); + CPHandleCommand(GEARSPLUGINCOMMAND_CREATE_SHORTCUT, command, NULL); +} + +// This class holds and manages the data passed to the +// GEARSPLUGINCOMMAND_GET_SHORTCUT_LIST plugin command. When the command is +// invoked, we proxy the results over to the calling thread. +class QueryShortcutsCommand : public CPCommandInterface { + public: + explicit QueryShortcutsCommand(GearsQueryShortcutsCallback* callback) : + callback_(callback), calling_loop_(MessageLoop::current()) { + shortcut_list_.shortcuts = NULL; + shortcut_list_.num_shortcuts = 0; + } + virtual ~QueryShortcutsCommand() { } + virtual void* GetData() { return &shortcut_list_; } + virtual void OnCommandInvoked(CPError retval) { + calling_loop_->PostTask(FROM_HERE, NewRunnableMethod( + this, &QueryShortcutsCommand::ReportResults, retval)); + } + + private: + void ReportResults(CPError retval) { + callback_->Run(retval == CPERR_SUCCESS ? &shortcut_list_ : NULL); + FreeGearsShortcutList(); + delete this; + } + + void FreeGearsShortcutList() { + for (size_t i = 0; i < shortcut_list_.num_shortcuts; ++i) { + CPB_Free(const_cast<char*>(shortcut_list_.shortcuts[i].description)); + CPB_Free(const_cast<char*>(shortcut_list_.shortcuts[i].name)); + CPB_Free(const_cast<char*>(shortcut_list_.shortcuts[i].url)); + for (size_t j = 0; j < 4; ++j) + CPB_Free(const_cast<char*>(shortcut_list_.shortcuts[i].icons[j].url)); + } + CPB_Free(shortcut_list_.shortcuts); + } + + GearsShortcutList shortcut_list_; + scoped_ptr<GearsQueryShortcutsCallback> callback_; + MessageLoop* calling_loop_; +}; + +// Allows InvokeLater without adding refcounting. The object is only deleted +// when its last InvokeLater is run anyway. +void RunnableMethodTraits<QueryShortcutsCommand>::RetainCallee( + QueryShortcutsCommand* remover) { +} +void RunnableMethodTraits<QueryShortcutsCommand>::ReleaseCallee( + QueryShortcutsCommand* remover) { +} + +void GearsQueryShortcuts(GearsQueryShortcutsCallback* callback) { + CPHandleCommand(GEARSPLUGINCOMMAND_GET_SHORTCUT_LIST, + new QueryShortcutsCommand(callback), + NULL); +} |