| 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_functions { | |
| 12 const char kExpandFunction[] = "toolstrip.expand"; | |
| 13 const char kCollapseFunction[] = "toolstrip.collapse"; | |
| 14 }; // namespace extension_toolstrip_api_functions | |
| 15 | |
| 16 namespace { | |
| 17 // Errors. | |
| 18 const char kNotAToolstripError[] = "This page is not a toolstrip."; | |
| 19 const char kAlreadyExpandedError[] = "This toolstrip is already expanded."; | |
| 20 const char kAlreadyCollapsedError[] = "This toolstrip is already collapsed."; | |
| 21 const char kInvalidURLError[] = "Invalid URL"; | |
| 22 const char kBadHeightError[] = "Bad height."; | |
| 23 | |
| 24 // TODO(erikkay) what are good values here? | |
| 25 const int kMinHeight = 50; | |
| 26 const int kMaxHeight = 1000; | |
| 27 }; // namespace | |
| 28 | |
| 29 bool ToolstripFunction::RunImpl() { | |
| 30 ExtensionHost* host = dispatcher()->GetExtensionHost(); | |
| 31 if (!host) { | |
| 32 error_ = kNotAToolstripError; | |
| 33 return false; | |
| 34 } | |
| 35 Browser* browser = dispatcher()->GetBrowser(); | |
| 36 if (!browser) { | |
| 37 error_ = kNotAToolstripError; | |
| 38 return false; | |
| 39 } | |
| 40 model_ = browser->extension_shelf_model(); | |
| 41 if (!model_) { | |
| 42 error_ = kNotAToolstripError; | |
| 43 return false; | |
| 44 } | |
| 45 toolstrip_ = model_->ToolstripForHost(host); | |
| 46 if (toolstrip_ == model_->end()) { | |
| 47 error_ = kNotAToolstripError; | |
| 48 return false; | |
| 49 } | |
| 50 return true; | |
| 51 } | |
| 52 | |
| 53 bool ToolstripExpandFunction::RunImpl() { | |
| 54 if (!ToolstripFunction::RunImpl()) | |
| 55 return false; | |
| 56 if (toolstrip_->height != 0) { | |
| 57 error_ = kAlreadyExpandedError; | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); | |
| 62 const ListValue* args = static_cast<const ListValue*>(args_); | |
| 63 EXTENSION_FUNCTION_VALIDATE(args->GetSize() <= 2); | |
| 64 | |
| 65 int height; | |
| 66 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &height)); | |
| 67 EXTENSION_FUNCTION_VALIDATE(height >= 0); | |
| 68 if (height < kMinHeight || height > kMaxHeight) { | |
| 69 error_ = kBadHeightError; | |
| 70 return false; | |
| 71 } | |
| 72 | |
| 73 GURL url; | |
| 74 if (args->GetSize() == 2) { | |
| 75 Value* url_val; | |
| 76 EXTENSION_FUNCTION_VALIDATE(args->Get(1, &url_val)); | |
| 77 if (url_val->GetType() != Value::TYPE_NULL) { | |
| 78 std::string url_str; | |
| 79 EXTENSION_FUNCTION_VALIDATE(url_val->GetAsString(&url_str)); | |
| 80 url = GURL(url_str); | |
| 81 if (!url.is_valid() && !url.is_empty()) { | |
| 82 error_ = kInvalidURLError; | |
| 83 return false; | |
| 84 } | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 model_->ExpandToolstrip(toolstrip_, url, height); | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 bool ToolstripCollapseFunction::RunImpl() { | |
| 93 if (!ToolstripFunction::RunImpl()) | |
| 94 return false; | |
| 95 | |
| 96 if (toolstrip_->height == 0) { | |
| 97 error_ = kAlreadyCollapsedError; | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 GURL url; | |
| 102 if (args_->GetType() != Value::TYPE_NULL) { | |
| 103 std::string url_str; | |
| 104 EXTENSION_FUNCTION_VALIDATE(args_->GetAsString(&url_str)); | |
| 105 url = GURL(url_str); | |
| 106 if (!url.is_valid() && !url.is_empty()) { | |
| 107 error_ = kInvalidURLError; | |
| 108 return false; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 model_->CollapseToolstrip(toolstrip_, url); | |
| 113 return true; | |
| 114 } | |
| OLD | NEW |