Index: chrome/renderer/searchbox/searchbox_extension.cc |
diff --git a/chrome/renderer/searchbox/searchbox_extension.cc b/chrome/renderer/searchbox/searchbox_extension.cc |
index c2aa8108a1ff8ac074d22f41b804859c1fcb5c3a..ce04a8cc473dd6a803490fd0e0a9052d51dbb901 100644 |
--- a/chrome/renderer/searchbox/searchbox_extension.cc |
+++ b/chrome/renderer/searchbox/searchbox_extension.cc |
@@ -4,8 +4,11 @@ |
#include "chrome/renderer/searchbox/searchbox_extension.h" |
+#include "base/string_number_conversions.h" |
#include "base/stringprintf.h" |
#include "base/utf_string_conversions.h" |
+#include "chrome/browser/themes/theme_service.h" |
+#include "chrome/common/extensions/extension.h" |
#include "chrome/renderer/searchbox/searchbox.h" |
#include "content/public/renderer/render_view.h" |
#include "grit/renderer_resources.h" |
@@ -19,6 +22,11 @@ |
namespace { |
+const char kCSSBackgroundImageFormat[] = |
+ "-webkit-image-set(url(chrome://theme/IDR_THEME_BACKGROUND?%s) 1x)"; |
+ |
+const char kCSSBackgroundColorFormat[] = "rgba(%d,%d,%d,%s)"; |
+ |
// Converts a V8 value to a string16. |
string16 V8ValueToUTF16(v8::Handle<v8::Value> v) { |
v8::String::Value s(v); |
@@ -30,6 +38,11 @@ v8::Handle<v8::String> UTF16ToV8String(const string16& s) { |
return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size()); |
} |
+// Converts std::string to V8 String. |
+v8::Handle<v8::String> UTF8ToV8String(const std::string& s) { |
+ return v8::String::New(s.data(), s.size()); |
+} |
+ |
void Dispatch(WebKit::WebFrame* frame, const WebKit::WebString& script) { |
if (!frame) return; |
frame->executeScript(WebKit::WebScriptSource(script)); |
@@ -118,6 +131,25 @@ static const char kDispatchContextChangeEventScript[] = |
" true;" |
"}"; |
+static const char kDispatchThemeChangeEventScript[] = |
+ "if (window.chrome &&" |
+ " window.chrome.searchBox &&" |
+ " window.chrome.searchBox.onthemechange &&" |
+ " typeof window.chrome.searchBox.onthemechange == 'function') {" |
+ " window.chrome.searchBox.onthemechange();" |
+ " true;" |
+ "}"; |
+ |
+static const char kDispatchThemeAreaHeightChangeEventScript[] = |
+ "if (window.chrome &&" |
+ " window.chrome.searchBox &&" |
+ " window.chrome.searchBox.onthemeareaheightchange &&" |
+ " typeof window.chrome.searchBox.onthemeareaheightchange ==" |
+ " 'function') {" |
+ " window.chrome.searchBox.onthemeareaheightchange();" |
+ " true;" |
+ "}"; |
+ |
// ---------------------------------------------------------------------------- |
class SearchBoxExtensionWrapper : public v8::Extension { |
@@ -167,6 +199,18 @@ class SearchBoxExtensionWrapper : public v8::Extension { |
// Gets the current session context. |
static v8::Handle<v8::Value> GetContext(const v8::Arguments& args); |
+ // Gets the background info of the theme currently adopted by browser. |
+ // Call only when overlay is showing NTP page. |
+ static v8::Handle<v8::Value> GetThemeBackgroundInfo( |
+ const v8::Arguments& args); |
+ |
+ // Gets the theme area height that the entire theme background image should |
+ // fill up. |
+ // Call only when overlay is showing NTP page and GetThemeBackgroundInfo |
+ // returns a non-empty image_url and an image_vertical_alignment that is not |
+ // "top". |
+ static v8::Handle<v8::Value> GetThemeAreaHeight(const v8::Arguments& args); |
+ |
// Navigates the window to a URL represented by either a URL string or a |
// restricted ID. |
static v8::Handle<v8::Value> NavigateContentWindow(const v8::Arguments& args); |
@@ -222,6 +266,10 @@ v8::Handle<v8::FunctionTemplate> SearchBoxExtensionWrapper::GetNativeFunction( |
return v8::FunctionTemplate::New(GetAutocompleteResults); |
if (name->Equals(v8::String::New("GetContext"))) |
return v8::FunctionTemplate::New(GetContext); |
+ if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) |
+ return v8::FunctionTemplate::New(GetThemeBackgroundInfo); |
+ if (name->Equals(v8::String::New("GetThemeAreaHeight"))) |
+ return v8::FunctionTemplate::New(GetThemeAreaHeight); |
if (name->Equals(v8::String::New("NavigateContentWindow"))) |
return v8::FunctionTemplate::New(NavigateContentWindow); |
if (name->Equals(v8::String::New("SetSuggestions"))) |
@@ -367,6 +415,95 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetContext( |
return context; |
} |
+v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetThemeBackgroundInfo( |
+ const v8::Arguments& args) { |
+ content::RenderView* render_view = GetRenderView(); |
+ if (!render_view) return v8::Undefined(); |
+ |
+ const ThemeBackgroundInfo& theme_info = |
+ SearchBox::Get(render_view)->GetThemeBackgroundInfo(); |
+ v8::Handle<v8::Object> info = v8::Object::New(); |
+ |
+ // The theme background color is in RGBA format "rgba(R,G,B,A)" where R, G and |
+ // B are between 0 and 255 inclusive, and A is a double between 0 and 1 |
+ // inclusive. |
+ // This is the CSS "background-color" format. |
+ // Value is always valid. |
+ info->Set(v8::String::New("colorRgba"), UTF8ToV8String( |
+ // Convert the alpha using DoubleToString because StringPrintf will use |
+ // locale specific formatters (e.g., use , instead of . in German). |
+ base::StringPrintf( |
+ kCSSBackgroundColorFormat, |
+ theme_info.color_r, |
+ theme_info.color_g, |
+ theme_info.color_b, |
+ base::DoubleToString(theme_info.color_a / 255.0).c_str()))); |
palmer
2012/11/19 22:23:20
Seems strange to convert this to a string. Can you
kuan
2012/11/20 00:12:44
the comment just above base::StringPrintf explains
palmer
2012/11/20 19:55:30
But I had thought that %f followed the same locale
|
+ |
+ // The theme background image url is of format |
+ // "-webkit-image-set(url(chrome://theme/IDR_THEME_BACKGROUND?<theme_id>) 1x)" |
+ // where <theme_id> is the id that identifies the theme. |
+ // This is the CSS "background-image" format. |
+ // Value is only valid if there's a custom theme background image. |
+ std::string image_url; |
+ if (extensions::Extension::IdIsValid(theme_info.theme_id)) { |
+ image_url = base::StringPrintf(kCSSBackgroundImageFormat, |
+ theme_info.theme_id.c_str()); |
palmer
2012/11/19 22:23:20
Where does this get validated to ensure that it is
kuan
2012/11/20 00:12:44
in the line just above StringPrintf, i call IdIsVa
palmer
2012/11/20 19:55:30
Sorry, my bad. :)
|
+ } |
+ info->Set(v8::String::New("imageUrl"), UTF8ToV8String(image_url)); |
+ |
+ // The theme background image horizontal alignment is one of "left", "right", |
+ // "center". |
+ // This is the horizontal component of the CSS "background-position" format. |
+ // Value is only valid if |image_url| is not empty. |
+ std::string alignment = ThemeService::kAlignmentCenter; |
+ if (!image_url.empty()) { |
+ if (theme_info.image_alignment & ThemeService::ALIGN_LEFT) |
+ alignment = ThemeService::kAlignmentLeft; |
+ else if (theme_info.image_alignment & ThemeService::ALIGN_RIGHT) |
+ alignment = ThemeService::kAlignmentRight; |
+ } |
+ info->Set(v8::String::New("imageHorizontalAlignment"), |
+ UTF8ToV8String(alignment)); |
+ |
+ // The theme background image vertical alignment is one of "top", "bottom", |
+ // "center". |
+ // This is the vertical component of the CSS "background-position" format. |
+ // Value is only valid if |image_url| is not empty. |
+ alignment = alignment = ThemeService::kAlignmentCenter; |
+ if (!image_url.empty()) { |
+ if (theme_info.image_alignment & ThemeService::ALIGN_TOP) |
+ alignment = ThemeService::kAlignmentTop; |
+ else if (theme_info.image_alignment & ThemeService::ALIGN_BOTTOM) |
+ alignment = ThemeService::kAlignmentBottom; |
+ } |
+ info->Set(v8::String::New("imageVerticalAlignment"), |
+ UTF8ToV8String(alignment)); |
+ |
+ // The tiling of the theme background image is one of "no-repeat", "repeat-x", |
+ // "repeat-y", "repeat". |
+ // This is the CSS "background-repeat" format. |
+ // Value is only valid if |image_url| is not empty. |
+ std::string tiling = ThemeService::kTilingNoRepeat; |
+ if (!image_url.empty()) |
+ tiling = ThemeService::TilingToString(theme_info.image_tiling); |
+ info->Set(v8::String::New("imageTiling"), UTF8ToV8String(tiling)); |
+ |
+ // The theme background image height is only valid if |imageUrl| is valid. |
+ info->Set(v8::String::New("imageHeight"), |
+ v8::Int32::New(image_url.empty() ? 0 : theme_info.image_height)); |
+ |
+ return info; |
+} |
+ |
+ |
+v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetThemeAreaHeight( |
+ const v8::Arguments& args) { |
+ content::RenderView* render_view = GetRenderView(); |
+ if (!render_view) return v8::Undefined(); |
+ |
+ return v8::Int32::New(SearchBox::Get(render_view)->GetThemeAreaHeight()); |
+} |
+ |
// static |
v8::Handle<v8::Value> SearchBoxExtensionWrapper::NavigateContentWindow( |
const v8::Arguments& args) { |
@@ -601,6 +738,17 @@ void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { |
} |
// static |
+void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { |
+ Dispatch(frame, kDispatchThemeChangeEventScript); |
+} |
+ |
+// static |
+void SearchBoxExtension::DispatchThemeAreaHeightChange( |
+ WebKit::WebFrame* frame) { |
+ Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); |
+} |
+ |
+// static |
v8::Extension* SearchBoxExtension::Get() { |
return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). |
GetRawDataResource(IDR_SEARCHBOX_API)); |