| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_toolstrip_api.h" | 5 #include "chrome/browser/extensions/extension_toolstrip_api.h" |
| 6 | 6 |
| 7 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/extensions/extension_host.h" | 8 #include "chrome/browser/extensions/extension_host.h" |
| 9 #include "chrome/browser/extensions/extension_shelf_model.h" | 9 #include "chrome/browser/extensions/extension_shelf_model.h" |
| 10 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
| 10 | 11 |
| 11 namespace extension_toolstrip_api_functions { | 12 namespace extension_toolstrip_api_functions { |
| 12 const char kExpandFunction[] = "toolstrip.expand"; | 13 const char kExpandFunction[] = "toolstrip.expand"; |
| 13 const char kCollapseFunction[] = "toolstrip.collapse"; | 14 const char kCollapseFunction[] = "toolstrip.collapse"; |
| 14 }; // namespace extension_toolstrip_api_functions | 15 }; // namespace extension_toolstrip_api_functions |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 // Errors. | 18 // Errors. |
| 18 const char kNotAToolstripError[] = "This page is not a toolstrip."; | 19 const char kNotAToolstripError[] = "This page is not a toolstrip."; |
| 19 const char kAlreadyExpandedError[] = "This toolstrip is already expanded."; | 20 const char kAlreadyExpandedError[] = "This toolstrip is already expanded."; |
| 20 const char kAlreadyCollapsedError[] = "This toolstrip is already collapsed."; | 21 const char kAlreadyCollapsedError[] = "This toolstrip is already collapsed."; |
| 21 const char kInvalidURLError[] = "Invalid URL"; | 22 const char kInvalidURLError[] = "Invalid URL"; |
| 22 const char kBadHeightError[] = "Bad height."; | 23 const char kBadHeightError[] = "Bad height."; |
| 23 | 24 |
| 24 // TODO(erikkay) what are good values here? | 25 // TODO(erikkay) what are good values here? |
| 25 const int kMinHeight = 50; | 26 const int kMinHeight = 50; |
| 26 const int kMaxHeight = 1000; | 27 const int kMaxHeight = 1000; |
| 27 }; // namespace | 28 }; // namespace |
| 28 | 29 |
| 30 namespace keys = extension_tabs_module_constants; |
| 31 |
| 29 bool ToolstripFunction::RunImpl() { | 32 bool ToolstripFunction::RunImpl() { |
| 30 ExtensionHost* host = dispatcher()->GetExtensionHost(); | 33 ExtensionHost* host = dispatcher()->GetExtensionHost(); |
| 31 if (!host) { | 34 if (!host) { |
| 32 error_ = kNotAToolstripError; | 35 error_ = kNotAToolstripError; |
| 33 return false; | 36 return false; |
| 34 } | 37 } |
| 35 Browser* browser = dispatcher()->GetBrowser(); | 38 Browser* browser = dispatcher()->GetBrowser(); |
| 36 if (!browser) { | 39 if (!browser) { |
| 37 error_ = kNotAToolstripError; | 40 error_ = kNotAToolstripError; |
| 38 return false; | 41 return false; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 51 } | 54 } |
| 52 | 55 |
| 53 bool ToolstripExpandFunction::RunImpl() { | 56 bool ToolstripExpandFunction::RunImpl() { |
| 54 if (!ToolstripFunction::RunImpl()) | 57 if (!ToolstripFunction::RunImpl()) |
| 55 return false; | 58 return false; |
| 56 if (toolstrip_->height != 0) { | 59 if (toolstrip_->height != 0) { |
| 57 error_ = kAlreadyExpandedError; | 60 error_ = kAlreadyExpandedError; |
| 58 return false; | 61 return false; |
| 59 } | 62 } |
| 60 | 63 |
| 61 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); | 64 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 62 const ListValue* args = static_cast<const ListValue*>(args_); | 65 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); |
| 63 EXTENSION_FUNCTION_VALIDATE(args->GetSize() <= 2); | |
| 64 | 66 |
| 65 int height; | 67 int height; |
| 66 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &height)); | 68 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kHeightKey, |
| 69 &height)); |
| 67 EXTENSION_FUNCTION_VALIDATE(height >= 0); | 70 EXTENSION_FUNCTION_VALIDATE(height >= 0); |
| 68 if (height < kMinHeight || height > kMaxHeight) { | 71 if (height < kMinHeight || height > kMaxHeight) { |
| 69 error_ = kBadHeightError; | 72 error_ = kBadHeightError; |
| 70 return false; | 73 return false; |
| 71 } | 74 } |
| 72 | 75 |
| 76 |
| 73 GURL url; | 77 GURL url; |
| 74 if (args->GetSize() == 2) { | 78 if (args->HasKey(keys::kUrlKey)) { |
| 75 Value* url_val; | 79 std::string url_string; |
| 76 EXTENSION_FUNCTION_VALIDATE(args->Get(1, &url_val)); | 80 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, |
| 77 if (url_val->GetType() != Value::TYPE_NULL) { | 81 &url_string)); |
| 78 std::string url_str; | 82 url = GURL(url_string); |
| 79 EXTENSION_FUNCTION_VALIDATE(url_val->GetAsString(&url_str)); | 83 if (!url.is_valid() && !url.is_empty()) { |
| 80 url = GURL(url_str); | 84 error_ = kInvalidURLError; |
| 81 if (!url.is_valid() && !url.is_empty()) { | 85 return false; |
| 82 error_ = kInvalidURLError; | |
| 83 return false; | |
| 84 } | |
| 85 } | 86 } |
| 86 } | 87 } |
| 87 | 88 |
| 88 model_->ExpandToolstrip(toolstrip_, url, height); | 89 model_->ExpandToolstrip(toolstrip_, url, height); |
| 89 return true; | 90 return true; |
| 90 } | 91 } |
| 91 | 92 |
| 92 bool ToolstripCollapseFunction::RunImpl() { | 93 bool ToolstripCollapseFunction::RunImpl() { |
| 93 if (!ToolstripFunction::RunImpl()) | 94 if (!ToolstripFunction::RunImpl()) |
| 94 return false; | 95 return false; |
| 95 | 96 |
| 96 if (toolstrip_->height == 0) { | 97 if (toolstrip_->height == 0) { |
| 97 error_ = kAlreadyCollapsedError; | 98 error_ = kAlreadyCollapsedError; |
| 98 return false; | 99 return false; |
| 99 } | 100 } |
| 100 | 101 |
| 101 GURL url; | 102 GURL url; |
| 102 if (args_->GetType() != Value::TYPE_NULL) { | 103 if (args_->GetType() != Value::TYPE_NULL) { |
| 103 std::string url_str; | 104 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 104 EXTENSION_FUNCTION_VALIDATE(args_->GetAsString(&url_str)); | 105 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); |
| 105 url = GURL(url_str); | 106 |
| 106 if (!url.is_valid() && !url.is_empty()) { | 107 if (args->HasKey(keys::kUrlKey)) { |
| 107 error_ = kInvalidURLError; | 108 std::string url_string; |
| 108 return false; | 109 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, |
| 110 &url_string)); |
| 111 url = GURL(url_string); |
| 112 if (!url.is_valid() && !url.is_empty()) { |
| 113 error_ = kInvalidURLError; |
| 114 return false; |
| 115 } |
| 109 } | 116 } |
| 110 } | 117 } |
| 111 | 118 |
| 112 model_->CollapseToolstrip(toolstrip_, url); | 119 model_->CollapseToolstrip(toolstrip_, url); |
| 113 return true; | 120 return true; |
| 114 } | 121 } |
| OLD | NEW |