Index: chrome/browser/extensions/extension_toolstrip_api.cc |
diff --git a/chrome/browser/extensions/extension_toolstrip_api.cc b/chrome/browser/extensions/extension_toolstrip_api.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2e7f2a2ef8b98a62485eaf5359ef5d80c1c1a067 |
--- /dev/null |
+++ b/chrome/browser/extensions/extension_toolstrip_api.cc |
@@ -0,0 +1,106 @@ |
+// 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_toolstrip_api.h" |
+ |
+#include "chrome/browser/browser.h" |
+#include "chrome/browser/extensions/extension_host.h" |
+#include "chrome/browser/extensions/extension_shelf_model.h" |
+ |
+namespace extension_toolstrip_api_constants { |
+ // Errors. |
+ const char kNotAToolstripError[] = "This page is not a toolstrip."; |
+ const char kAlreadyExpandedError[] = "This toolstrip is already expanded."; |
+ const char kAlreadyCollapsedError[] = "This toolstrip is already collapsed."; |
+ const char kInvalidURLError[] = "Invalid URL"; |
+ const char kBadHeightError[] = "Bad height."; |
+ |
+ // Function names. |
+ const char kExpandFunction[] = "toolstrip.expand"; |
+ const char kCollapseFunction[] = "toolstrip.collapse"; |
+}; // namespace extension_toolstrip_api_constants |
+ |
+namespace keys = extension_toolstrip_api_constants; |
+ |
+namespace { |
+ |
+ExtensionShelfModel* getModel(ExtensionFunctionDispatcher* dispatcher) { |
+ Browser* browser = dispatcher->GetBrowser(); |
+ return browser ? browser->extension_shelf_model() : NULL; |
+} |
+ |
+}; // namespace |
+ |
+bool ToolstripFunction::RunImpl() { |
+ ExtensionHost* host = dispatcher()->GetHost(); |
+ if (!host) { |
+ error_ = keys::kNotAToolstripError; |
+ return false; |
+ } |
+ Browser* browser = dispatcher()->GetBrowser(); |
+ if (!browser) { |
+ error_ = keys::kNotAToolstripError; |
+ return false; |
+ } |
+ model_ = browser->extension_shelf_model(); |
+ if (!model_) { |
+ error_ = keys::kNotAToolstripError; |
+ return false; |
+ } |
+ toolstrip_ = model_->ToolstripForHost(host); |
+ if (toolstrip_ == model_->end()) { |
+ error_ = keys::kNotAToolstripError; |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+bool ToolstripExpandFunction::RunImpl() { |
+ if (!ToolstripFunction::RunImpl()) |
+ return false; |
+ if ((*toolstrip_).height != 0) { |
+ error_ = keys::kAlreadyExpandedError; |
+ return false; |
+ } |
+ |
+ EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); |
+ const ListValue* args = static_cast<const ListValue*>(args_); |
+ |
+ int height; |
+ EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &height)); |
+ // TODO(erikkay) enforce a better min height |
+ EXTENSION_FUNCTION_VALIDATE(height >= 0); |
+ if (height > 1000) { |
+ error_ = keys::kBadHeightError; |
+ return false; |
+ } |
+ std::string url_str; |
+ EXTENSION_FUNCTION_VALIDATE(args->GetString(1, &url_str)); |
+ GURL url(url_str); |
+ if (!url.is_valid()) { |
+ error_ = keys::kInvalidURLError; |
+ return false; |
+ } |
+ |
+ model_->ExpandToolstrip(toolstrip_, url, height); |
+ return true; |
+} |
+ |
+bool ToolstripCollapseFunction::RunImpl() { |
+ if (!ToolstripFunction::RunImpl()) |
+ return false; |
+ if ((*toolstrip_).height == 0) { |
+ error_ = keys::kAlreadyCollapsedError; |
+ return false; |
+ } |
+ std::string url_str; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetAsString(&url_str)); |
+ GURL url(url_str); |
+ if (!url.is_valid()) { |
+ error_ = keys::kInvalidURLError; |
+ return false; |
+ } |
+ model_->CollapseToolstrip(toolstrip_, url); |
+ return true; |
+} |