OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/extension_toolstrip_api.h" |
| 6 |
| 7 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/extensions/extension_host.h" |
| 9 #include "chrome/browser/extensions/extension_shelf_model.h" |
| 10 |
| 11 namespace extension_toolstrip_api_constants { |
| 12 // Errors. |
| 13 const char kNotAToolstripError[] = "This page is not a toolstrip."; |
| 14 const char kAlreadyExpandedError[] = "This toolstrip is already expanded."; |
| 15 const char kAlreadyCollapsedError[] = "This toolstrip is already collapsed."; |
| 16 const char kInvalidURLError[] = "Invalid URL"; |
| 17 const char kBadHeightError[] = "Bad height."; |
| 18 |
| 19 // Function names. |
| 20 const char kExpandFunction[] = "toolstrip.expand"; |
| 21 const char kCollapseFunction[] = "toolstrip.collapse"; |
| 22 }; // namespace extension_toolstrip_api_constants |
| 23 |
| 24 namespace keys = extension_toolstrip_api_constants; |
| 25 |
| 26 namespace { |
| 27 |
| 28 ExtensionShelfModel* getModel(ExtensionFunctionDispatcher* dispatcher) { |
| 29 Browser* browser = dispatcher->GetBrowser(); |
| 30 return browser ? browser->extension_shelf_model() : NULL; |
| 31 } |
| 32 |
| 33 }; // namespace |
| 34 |
| 35 bool ToolstripFunction::RunImpl() { |
| 36 ExtensionHost* host = dispatcher()->GetHost(); |
| 37 if (!host) { |
| 38 error_ = keys::kNotAToolstripError; |
| 39 return false; |
| 40 } |
| 41 Browser* browser = dispatcher()->GetBrowser(); |
| 42 if (!browser) { |
| 43 error_ = keys::kNotAToolstripError; |
| 44 return false; |
| 45 } |
| 46 model_ = browser->extension_shelf_model(); |
| 47 if (!model_) { |
| 48 error_ = keys::kNotAToolstripError; |
| 49 return false; |
| 50 } |
| 51 toolstrip_ = model_->ToolstripForHost(host); |
| 52 if (toolstrip_ == model_->end()) { |
| 53 error_ = keys::kNotAToolstripError; |
| 54 return false; |
| 55 } |
| 56 return true; |
| 57 } |
| 58 |
| 59 bool ToolstripExpandFunction::RunImpl() { |
| 60 if (!ToolstripFunction::RunImpl()) |
| 61 return false; |
| 62 if ((*toolstrip_).height != 0) { |
| 63 error_ = keys::kAlreadyExpandedError; |
| 64 return false; |
| 65 } |
| 66 |
| 67 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); |
| 68 const ListValue* args = static_cast<const ListValue*>(args_); |
| 69 |
| 70 int height; |
| 71 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &height)); |
| 72 // TODO(erikkay) enforce a better min height |
| 73 EXTENSION_FUNCTION_VALIDATE(height >= 0); |
| 74 if (height > 1000) { |
| 75 error_ = keys::kBadHeightError; |
| 76 return false; |
| 77 } |
| 78 std::string url_str; |
| 79 EXTENSION_FUNCTION_VALIDATE(args->GetString(1, &url_str)); |
| 80 GURL url(url_str); |
| 81 if (!url.is_valid()) { |
| 82 error_ = keys::kInvalidURLError; |
| 83 return false; |
| 84 } |
| 85 |
| 86 model_->ExpandToolstrip(toolstrip_, url, height); |
| 87 return true; |
| 88 } |
| 89 |
| 90 bool ToolstripCollapseFunction::RunImpl() { |
| 91 if (!ToolstripFunction::RunImpl()) |
| 92 return false; |
| 93 if ((*toolstrip_).height == 0) { |
| 94 error_ = keys::kAlreadyCollapsedError; |
| 95 return false; |
| 96 } |
| 97 std::string url_str; |
| 98 EXTENSION_FUNCTION_VALIDATE(args_->GetAsString(&url_str)); |
| 99 GURL url(url_str); |
| 100 if (!url.is_valid()) { |
| 101 error_ = keys::kInvalidURLError; |
| 102 return false; |
| 103 } |
| 104 model_->CollapseToolstrip(toolstrip_, url); |
| 105 return true; |
| 106 } |
OLD | NEW |