Chromium Code Reviews| 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 "base/json_writer.h" | |
| 7 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/extensions/extension_host.h" | 9 #include "chrome/browser/extensions/extension_host.h" |
| 10 #include "chrome/browser/extensions/extension_message_service.h" | |
| 9 #include "chrome/browser/extensions/extension_shelf_model.h" | 11 #include "chrome/browser/extensions/extension_shelf_model.h" |
| 10 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 12 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
| 13 #include "chrome/browser/profile.h" | |
| 11 | 14 |
| 12 namespace extension_toolstrip_api_functions { | 15 namespace extension_toolstrip_api_functions { |
| 13 const char kExpandFunction[] = "toolstrip.expand"; | 16 const char kExpandFunction[] = "toolstrip.expand"; |
| 14 const char kCollapseFunction[] = "toolstrip.collapse"; | 17 const char kCollapseFunction[] = "toolstrip.collapse"; |
| 15 }; // namespace extension_toolstrip_api_functions | 18 }; // namespace extension_toolstrip_api_functions |
| 16 | 19 |
| 20 namespace extension_toolstrip_api_events { | |
| 21 const char kOnToolstripExpanded[] = "toolstrip.onExpanded.%d"; | |
| 22 const char kOnToolstripCollapsed[] = "toolstrip.onCollapsed.%d"; | |
| 23 }; // namespace extension_toolstrip_api_events | |
| 24 | |
| 17 namespace { | 25 namespace { |
| 18 // Errors. | 26 // Errors. |
| 19 const char kNotAToolstripError[] = "This page is not a toolstrip."; | 27 const char kNotAToolstripError[] = "This page is not a toolstrip."; |
| 20 const char kAlreadyExpandedError[] = "This toolstrip is already expanded."; | 28 const char kAlreadyExpandedError[] = "This toolstrip is already expanded."; |
| 21 const char kAlreadyCollapsedError[] = "This toolstrip is already collapsed."; | 29 const char kAlreadyCollapsedError[] = "This toolstrip is already collapsed."; |
| 22 const char kInvalidURLError[] = "Invalid URL"; | 30 const char kInvalidURLError[] = "Invalid URL"; |
| 23 const char kBadHeightError[] = "Bad height."; | 31 const char kBadHeightError[] = "Bad height."; |
| 24 | 32 |
| 25 // TODO(erikkay) what are good values here? | 33 // TODO(erikkay) what are good values here? |
| 26 const int kMinHeight = 50; | 34 const int kMinHeight = 50; |
| 27 const int kMaxHeight = 1000; | 35 const int kMaxHeight = 1000; |
| 28 }; // namespace | 36 }; // namespace |
| 29 | 37 |
| 30 namespace keys = extension_tabs_module_constants; | 38 namespace keys = extension_tabs_module_constants; |
| 39 namespace events = extension_toolstrip_api_events; | |
| 31 | 40 |
| 32 bool ToolstripFunction::RunImpl() { | 41 bool ToolstripFunction::RunImpl() { |
| 33 ExtensionHost* host = dispatcher()->GetExtensionHost(); | 42 ExtensionHost* host = dispatcher()->GetExtensionHost(); |
| 34 if (!host) { | 43 if (!host) { |
| 35 error_ = kNotAToolstripError; | 44 error_ = kNotAToolstripError; |
| 36 return false; | 45 return false; |
| 37 } | 46 } |
| 38 Browser* browser = dispatcher()->GetBrowser(); | 47 Browser* browser = dispatcher()->GetBrowser(); |
| 39 if (!browser) { | 48 if (!browser) { |
| 40 error_ = kNotAToolstripError; | 49 error_ = kNotAToolstripError; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 if (!url.is_valid() && !url.is_empty()) { | 121 if (!url.is_valid() && !url.is_empty()) { |
| 113 error_ = kInvalidURLError; | 122 error_ = kInvalidURLError; |
| 114 return false; | 123 return false; |
| 115 } | 124 } |
| 116 } | 125 } |
| 117 } | 126 } |
| 118 | 127 |
| 119 model_->CollapseToolstrip(toolstrip_, url); | 128 model_->CollapseToolstrip(toolstrip_, url); |
| 120 return true; | 129 return true; |
| 121 } | 130 } |
| 131 | |
| 132 // static | |
| 133 void ToolstripEventRouter::DispatchEvent(Profile *profile, | |
| 134 int routing_id, | |
| 135 const char *event_name, | |
| 136 const Value& json) { | |
| 137 if (profile->GetExtensionMessageService()) { | |
| 138 std::string json_args; | |
| 139 JSONWriter::Write(&json, false, &json_args); | |
| 140 std::string full_event_name = StringPrintf(event_name, routing_id); | |
| 141 profile->GetExtensionMessageService()-> | |
| 142 DispatchEventToRenderers(full_event_name, json_args); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 // static | |
| 147 void ToolstripEventRouter::OnToolstripExpanded(Profile* profile, | |
| 148 int routing_id, | |
| 149 const GURL &url, | |
| 150 int height) { | |
| 151 ListValue args; | |
| 152 DictionaryValue* obj = new DictionaryValue(); | |
| 153 if (!url.is_empty()) | |
| 154 obj->SetString(keys::kUrlKey, url.spec()); | |
| 155 obj->SetInteger(keys::kHeightKey, height); | |
| 156 args.Append(obj); | |
| 157 DispatchEvent(profile, routing_id, events::kOnToolstripExpanded, args); | |
| 158 } | |
| 159 | |
| 160 // static | |
| 161 void ToolstripEventRouter::OnToolstripCollapsed(Profile* profile, | |
| 162 int routing_id, | |
| 163 const GURL &url) { | |
| 164 ListValue args; | |
| 165 DictionaryValue* obj = new DictionaryValue(); | |
| 166 if (!url.is_empty()) | |
|
Matt Perry
2009/09/10 23:02:50
should we set the .url property to be "" rather th
| |
| 167 obj->SetString(keys::kUrlKey, url.spec()); | |
| 168 args.Append(obj); | |
| 169 DispatchEvent(profile, routing_id, events::kOnToolstripCollapsed, args); | |
| 170 } | |
| OLD | NEW |