summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorerikkay@chromium.org <erikkay@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-11 15:16:45 +0000
committererikkay@chromium.org <erikkay@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-11 15:16:45 +0000
commitadc2ccf7987a58fb73f2749d98cd49aae56c3700 (patch)
treea93b3de0ef8455799068ee77ce907a2635ccd033
parent6c3b5326b5e35d548f96496810164e596d7b210e (diff)
downloadchromium_src-adc2ccf7987a58fb73f2749d98cd49aae56c3700.zip
chromium_src-adc2ccf7987a58fb73f2749d98cd49aae56c3700.tar.gz
chromium_src-adc2ccf7987a58fb73f2749d98cd49aae56c3700.tar.bz2
add mole collapse/expand events. convert mappy to use this.
BUG=15494 TEST=browser_tests.exe --gtest_filter=ExtensionApiTest.Toolstrip Review URL: http://codereview.chromium.org/203023 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25976 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/extensions/extension_shelf_model.cc10
-rw-r--r--chrome/browser/extensions/extension_toolstrip_api.cc49
-rw-r--r--chrome/browser/extensions/extension_toolstrip_api.h28
-rw-r--r--chrome/browser/extensions/extension_toolstrip_apitest.cc9
-rw-r--r--chrome/chrome.gyp2
-rw-r--r--chrome/renderer/extensions/extension_process_bindings.cc9
-rw-r--r--chrome/renderer/resources/extension_process_bindings.js10
-rwxr-xr-xchrome/test/data/extensions/api_test/toolstrip/manifest.json6
-rwxr-xr-xchrome/test/data/extensions/api_test/toolstrip/test.html14
-rwxr-xr-xchrome/test/data/extensions/samples/mappy/mappy_toolstrip.html34
10 files changed, 168 insertions, 3 deletions
diff --git a/chrome/browser/extensions/extension_shelf_model.cc b/chrome/browser/extensions/extension_shelf_model.cc
index b2dbb9b..5a6db01 100644
--- a/chrome/browser/extensions/extension_shelf_model.cc
+++ b/chrome/browser/extensions/extension_shelf_model.cc
@@ -9,7 +9,9 @@
#include "chrome/browser/profile.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/extensions/extension_process_manager.h"
+#include "chrome/browser/extensions/extension_toolstrip_api.h"
#include "chrome/browser/extensions/extensions_service.h"
+#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_service.h"
@@ -132,6 +134,10 @@ void ExtensionShelfModel::ExpandToolstrip(iterator toolstrip,
toolstrip->url = url;
FOR_EACH_OBSERVER(ExtensionShelfModelObserver, observers_,
ToolstripChanged(toolstrip));
+ int routing_id = toolstrip->host->render_view_host()->routing_id();
+ ToolstripEventRouter::OnToolstripExpanded(browser_->profile(),
+ routing_id,
+ url, height);
}
void ExtensionShelfModel::CollapseToolstrip(iterator toolstrip,
@@ -142,6 +148,10 @@ void ExtensionShelfModel::CollapseToolstrip(iterator toolstrip,
toolstrip->url = url;
FOR_EACH_OBSERVER(ExtensionShelfModelObserver, observers_,
ToolstripChanged(toolstrip));
+ int routing_id = toolstrip->host->render_view_host()->routing_id();
+ ToolstripEventRouter::OnToolstripCollapsed(browser_->profile(),
+ routing_id,
+ url);
}
void ExtensionShelfModel::Observe(NotificationType type,
diff --git a/chrome/browser/extensions/extension_toolstrip_api.cc b/chrome/browser/extensions/extension_toolstrip_api.cc
index 8f70243..a502dca 100644
--- a/chrome/browser/extensions/extension_toolstrip_api.cc
+++ b/chrome/browser/extensions/extension_toolstrip_api.cc
@@ -4,16 +4,24 @@
#include "chrome/browser/extensions/extension_toolstrip_api.h"
+#include "base/json_writer.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/extensions/extension_host.h"
+#include "chrome/browser/extensions/extension_message_service.h"
#include "chrome/browser/extensions/extension_shelf_model.h"
#include "chrome/browser/extensions/extension_tabs_module_constants.h"
+#include "chrome/browser/profile.h"
namespace extension_toolstrip_api_functions {
const char kExpandFunction[] = "toolstrip.expand";
const char kCollapseFunction[] = "toolstrip.collapse";
}; // namespace extension_toolstrip_api_functions
+namespace extension_toolstrip_api_events {
+const char kOnToolstripExpanded[] = "toolstrip.onExpanded.%d";
+const char kOnToolstripCollapsed[] = "toolstrip.onCollapsed.%d";
+}; // namespace extension_toolstrip_api_events
+
namespace {
// Errors.
const char kNotAToolstripError[] = "This page is not a toolstrip.";
@@ -28,6 +36,7 @@ const int kMaxHeight = 1000;
}; // namespace
namespace keys = extension_tabs_module_constants;
+namespace events = extension_toolstrip_api_events;
bool ToolstripFunction::RunImpl() {
ExtensionHost* host = dispatcher()->GetExtensionHost();
@@ -119,3 +128,43 @@ bool ToolstripCollapseFunction::RunImpl() {
model_->CollapseToolstrip(toolstrip_, url);
return true;
}
+
+// static
+void ToolstripEventRouter::DispatchEvent(Profile *profile,
+ int routing_id,
+ const char *event_name,
+ const Value& json) {
+ if (profile->GetExtensionMessageService()) {
+ std::string json_args;
+ JSONWriter::Write(&json, false, &json_args);
+ std::string full_event_name = StringPrintf(event_name, routing_id);
+ profile->GetExtensionMessageService()->
+ DispatchEventToRenderers(full_event_name, json_args);
+ }
+}
+
+// static
+void ToolstripEventRouter::OnToolstripExpanded(Profile* profile,
+ int routing_id,
+ const GURL &url,
+ int height) {
+ ListValue args;
+ DictionaryValue* obj = new DictionaryValue();
+ if (!url.is_empty())
+ obj->SetString(keys::kUrlKey, url.spec());
+ obj->SetInteger(keys::kHeightKey, height);
+ args.Append(obj);
+ DispatchEvent(profile, routing_id, events::kOnToolstripExpanded, args);
+}
+
+// static
+void ToolstripEventRouter::OnToolstripCollapsed(Profile* profile,
+ int routing_id,
+ const GURL &url) {
+ ListValue args;
+ DictionaryValue* obj = new DictionaryValue();
+ if (!url.is_empty())
+ obj->SetString(keys::kUrlKey, url.spec());
+ args.Append(obj);
+ DispatchEvent(profile, routing_id, events::kOnToolstripCollapsed, args);
+}
diff --git a/chrome/browser/extensions/extension_toolstrip_api.h b/chrome/browser/extensions/extension_toolstrip_api.h
index 127c952..a21e627 100644
--- a/chrome/browser/extensions/extension_toolstrip_api.h
+++ b/chrome/browser/extensions/extension_toolstrip_api.h
@@ -8,12 +8,19 @@
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/browser/extensions/extension_shelf_model.h"
+class Profile;
+
// Function names.
namespace extension_toolstrip_api_functions {
extern const char kExpandFunction[];
extern const char kCollapseFunction[];
}; // namespace extension_toolstrip_api_functions
+namespace extension_toolstrip_api_events {
+ extern const char kOnToolstripExpanded[];
+ extern const char kOnToolstripCollapsed[];
+}; // namespace extension_toolstrip_api_events
+
class ToolstripFunction : public SyncExtensionFunction {
protected:
virtual bool RunImpl();
@@ -30,4 +37,25 @@ class ToolstripCollapseFunction : public ToolstripFunction {
virtual bool RunImpl();
};
+class ToolstripEventRouter {
+ public:
+ // Toolstrip events.
+ static void OnToolstripExpanded(Profile* profile,
+ int routing_id,
+ const GURL& url,
+ int height);
+ static void OnToolstripCollapsed(Profile* profile,
+ int routing_id,
+ const GURL& url);
+
+ private:
+ // Helper to actually dispatch an event to extension listeners.
+ static void DispatchEvent(Profile* profile,
+ int routing_id,
+ const char* event_name,
+ const Value& json);
+
+ DISALLOW_COPY_AND_ASSIGN(ToolstripEventRouter);
+};
+
#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLSTRIP_API_H_
diff --git a/chrome/browser/extensions/extension_toolstrip_apitest.cc b/chrome/browser/extensions/extension_toolstrip_apitest.cc
new file mode 100644
index 0000000..5e302db
--- /dev/null
+++ b/chrome/browser/extensions/extension_toolstrip_apitest.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/extensions/extension_apitest.h"
+
+IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Toolstrip) {
+ ASSERT_TRUE(RunExtensionTest("toolstrip")) << message_;
+}
diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp
index 782a764..cc56d34 100644
--- a/chrome/chrome.gyp
+++ b/chrome/chrome.gyp
@@ -67,6 +67,7 @@
'browser/extensions/extension_browsertest.h',
'browser/extensions/extension_browsertests_misc.cc',
'browser/extensions/extension_override_apitest.cc',
+ 'browser/extensions/extension_toolstrip_apitest.cc',
],
'browser_tests_sources_win_specific': [
'browser/extensions/extension_devtools_browsertest.cc',
@@ -96,6 +97,7 @@
'browser/extensions/extension_browsertest.h',
'browser/extensions/extension_browsertests_misc.cc',
'browser/extensions/extension_override_apitest.cc',
+ 'browser/extensions/extension_toolstrip_apitest.cc',
'browser/ssl/ssl_browser_tests.cc',
],
# TODO(jcampan): move these vars to views.gyp.
diff --git a/chrome/renderer/extensions/extension_process_bindings.cc b/chrome/renderer/extensions/extension_process_bindings.cc
index 8aaa2f2..7a9d0ac 100644
--- a/chrome/renderer/extensions/extension_process_bindings.cc
+++ b/chrome/renderer/extensions/extension_process_bindings.cc
@@ -106,6 +106,8 @@ class ExtensionImpl : public ExtensionBase {
return v8::FunctionTemplate::New(GetCurrentPageActions);
} else if (name->Equals(v8::String::New("StartRequest"))) {
return v8::FunctionTemplate::New(StartRequest);
+ } else if (name->Equals(v8::String::New("GetRenderViewId"))) {
+ return v8::FunctionTemplate::New(GetRenderViewId);
}
return ExtensionBase::GetNativeFunction(name);
@@ -268,6 +270,13 @@ class ExtensionImpl : public ExtensionBase {
return v8::Undefined();
}
+
+ static v8::Handle<v8::Value> GetRenderViewId(const v8::Arguments& args) {
+ RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext();
+ if (!renderview)
+ return v8::Undefined();
+ return v8::Integer::New(renderview->routing_id());
+ }
};
} // namespace
diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js
index 4312d04..657baec 100644
--- a/chrome/renderer/resources/extension_process_bindings.js
+++ b/chrome/renderer/resources/extension_process_bindings.js
@@ -19,6 +19,7 @@ var chrome = chrome || {};
native function GetChromeHidden();
native function GetNextRequestId();
native function OpenChannelToTab();
+ native function GetRenderViewId();
if (!chrome)
chrome = {};
@@ -182,6 +183,14 @@ var chrome = chrome || {};
}
}
+ function setupToolstripEvents(renderViewId) {
+ chrome.toolstrip = chrome.toolstrip || {};
+ chrome.toolstrip.onExpanded =
+ new chrome.Event("toolstrip.onExpanded." + renderViewId);
+ chrome.toolstrip.onCollapsed =
+ new chrome.Event("toolstrip.onCollapsed." + renderViewId);
+ }
+
chromeHidden.onLoad.addListener(function (extensionId) {
chrome.extension = new chrome.Extension(extensionId);
@@ -298,5 +307,6 @@ var chrome = chrome || {};
}
setupPageActionEvents(extensionId);
+ setupToolstripEvents(GetRenderViewId());
});
})();
diff --git a/chrome/test/data/extensions/api_test/toolstrip/manifest.json b/chrome/test/data/extensions/api_test/toolstrip/manifest.json
new file mode 100755
index 0000000..36ed6e8
--- /dev/null
+++ b/chrome/test/data/extensions/api_test/toolstrip/manifest.json
@@ -0,0 +1,6 @@
+{
+ "name": "chrome.toolstrip",
+ "version": "0.1",
+ "description": "end-to-end browser test for chrome.toolstrip API",
+ "toolstrips": ["test.html"]
+}
diff --git a/chrome/test/data/extensions/api_test/toolstrip/test.html b/chrome/test/data/extensions/api_test/toolstrip/test.html
new file mode 100755
index 0000000..237612d
--- /dev/null
+++ b/chrome/test/data/extensions/api_test/toolstrip/test.html
@@ -0,0 +1,14 @@
+<script>
+chrome.test.runTests([
+ function expand() {
+ chrome.test.listenOnce(chrome.toolstrip.onExpanded, function(){});
+ chrome.toolstrip.expand({height:200},
+ chrome.test.callbackPass(function(){}));
+ },
+
+ function collapse() {
+ chrome.test.listenOnce(chrome.toolstrip.onCollapsed, function(){});
+ chrome.toolstrip.collapse({}, chrome.test.callbackPass(function(){}));
+ }
+]);
+</script>
diff --git a/chrome/test/data/extensions/samples/mappy/mappy_toolstrip.html b/chrome/test/data/extensions/samples/mappy/mappy_toolstrip.html
index 096b1e3..9573344 100755
--- a/chrome/test/data/extensions/samples/mappy/mappy_toolstrip.html
+++ b/chrome/test/data/extensions/samples/mappy/mappy_toolstrip.html
@@ -1,8 +1,36 @@
+<style>
+#map {
+ display: none;
+ width: 512px;
+ height: 512px;
+}
+</style>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAATfHumDbW3OmRByfquHd3SRTRERdeAiwZ9EeJWta3L_JZVS0bOBRQeZgr4K0xyVKzUdnnuFl8X9PX0w&sensor=false"
type="text/javascript"></script>
<script>
var maps_key = "ABQIAAAATfHumDbW3OmRByfquHd3SRTRERdeAiwZ9EeJWta3L_JZVS0bOBRQeZgr4K0xyVKzUdnnuFl8X9PX0w";
+chrome.toolstrip.onCollapsed.addListener(function() {
+ var map = document.getElementById("map");
+ map.src = "";
+ map.style.display = "none";
+ var button = document.getElementById("button");
+ button.style.display = "block";
+});
+
+function expand(url) {
+ var button = document.getElementById("button");
+ button.style.display = "none";
+ var map = document.getElementById("map");
+ map.src = url;
+ map.style.display = "block";
+ chrome.toolstrip.expand({height:512}, function() {});
+}
+
+function collapse() {
+ chrome.toolstrip.collapse({}, function() {});
+}
+
function gclient_geocode(address) {
var geocoder = new GClientGeocoder();
geocoder.getLatLng(address, function(point) {
@@ -13,8 +41,7 @@ function gclient_geocode(address) {
var url = "http://maps.google.com/staticmap?center=" + latlng +
"&markers=" + latlng + "&zoom=14" +
"&size=512x512&sensor=false&key=" + maps_key;
- document.body.style.width = "512px";
- setTimeout(chrome.toolstrip.expand, 100, {height:512, url:url});
+ expand(url);
}
});
}
@@ -34,6 +61,7 @@ function map() {
});
};
</script>
-<div class="toolstrip-button" onclick="map()">
+<div id="button" class="toolstrip-button" onclick="map()">
<span>Mappy</span>
</div>
+<img id="map" onclick="collapse()">