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 "base/json_writer.h" |
8 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
9 #include "chrome/browser/extensions/extension_host.h" | 9 #include "chrome/browser/extensions/extension_host.h" |
10 #include "chrome/browser/extensions/extension_message_service.h" | 10 #include "chrome/browser/extensions/extension_message_service.h" |
(...skipping 15 matching lines...) Expand all Loading... | |
26 // Errors. | 26 // Errors. |
27 const char kNotAToolstripError[] = "This page is not a toolstrip."; | 27 const char kNotAToolstripError[] = "This page is not a toolstrip."; |
28 const char kAlreadyExpandedError[] = "This toolstrip is already expanded."; | 28 const char kAlreadyExpandedError[] = "This toolstrip is already expanded."; |
29 const char kAlreadyCollapsedError[] = "This toolstrip is already collapsed."; | 29 const char kAlreadyCollapsedError[] = "This toolstrip is already collapsed."; |
30 const char kInvalidURLError[] = "Invalid URL"; | 30 const char kInvalidURLError[] = "Invalid URL"; |
31 const char kBadHeightError[] = "Bad height."; | 31 const char kBadHeightError[] = "Bad height."; |
32 | 32 |
33 // TODO(erikkay) what are good values here? | 33 // TODO(erikkay) what are good values here? |
34 const int kMinHeight = 50; | 34 const int kMinHeight = 50; |
35 const int kMaxHeight = 1000; | 35 const int kMaxHeight = 1000; |
36 | |
37 // If |url_string| is a valid URL, simply return that, otherwise see if it's | |
38 // a valid extension URL, relative to the |current_url|. | |
39 static GURL ResolveURLForExtension(const std::string& url_string, | |
Matt Perry
2009/09/15 20:31:57
I think Resolve works on absolute URLs too, so thi
| |
40 const GURL& current_url, | |
41 Extension *extension) { | |
42 GURL url = GURL(url_string); | |
43 if (!url.is_valid()) { | |
44 url = current_url.Resolve(url_string); | |
45 if (url.is_empty()) | |
46 return url; | |
47 } | |
48 return url; | |
49 } | |
50 | |
36 }; // namespace | 51 }; // namespace |
37 | 52 |
38 namespace keys = extension_tabs_module_constants; | 53 namespace keys = extension_tabs_module_constants; |
39 namespace events = extension_toolstrip_api_events; | 54 namespace events = extension_toolstrip_api_events; |
40 | 55 |
41 bool ToolstripFunction::RunImpl() { | 56 bool ToolstripFunction::RunImpl() { |
42 ExtensionHost* host = dispatcher()->GetExtensionHost(); | 57 ExtensionHost* host = dispatcher()->GetExtensionHost(); |
43 if (!host) { | 58 if (!host) { |
44 error_ = kNotAToolstripError; | 59 error_ = kNotAToolstripError; |
45 return false; | 60 return false; |
(...skipping 29 matching lines...) Expand all Loading... | |
75 | 90 |
76 int height; | 91 int height; |
77 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kHeightKey, | 92 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kHeightKey, |
78 &height)); | 93 &height)); |
79 EXTENSION_FUNCTION_VALIDATE(height >= 0); | 94 EXTENSION_FUNCTION_VALIDATE(height >= 0); |
80 if (height < kMinHeight || height > kMaxHeight) { | 95 if (height < kMinHeight || height > kMaxHeight) { |
81 error_ = kBadHeightError; | 96 error_ = kBadHeightError; |
82 return false; | 97 return false; |
83 } | 98 } |
84 | 99 |
85 | |
86 GURL url; | 100 GURL url; |
87 if (args->HasKey(keys::kUrlKey)) { | 101 if (args->HasKey(keys::kUrlKey)) { |
88 std::string url_string; | 102 std::string url_string; |
89 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, | 103 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, |
90 &url_string)); | 104 &url_string)); |
91 url = GURL(url_string); | 105 url = ResolveURLForExtension(url_string, |
92 if (!url.is_valid() && !url.is_empty()) { | 106 dispatcher()->url(), |
107 dispatcher()->GetExtension()); | |
108 if (!url.is_valid()) { | |
93 error_ = kInvalidURLError; | 109 error_ = kInvalidURLError; |
94 return false; | 110 return false; |
95 } | 111 } |
96 } | 112 } |
97 | 113 |
98 model_->ExpandToolstrip(toolstrip_, url, height); | 114 model_->ExpandToolstrip(toolstrip_, url, height); |
99 return true; | 115 return true; |
100 } | 116 } |
101 | 117 |
102 bool ToolstripCollapseFunction::RunImpl() { | 118 bool ToolstripCollapseFunction::RunImpl() { |
103 if (!ToolstripFunction::RunImpl()) | 119 if (!ToolstripFunction::RunImpl()) |
104 return false; | 120 return false; |
105 | 121 |
106 if (toolstrip_->height == 0) { | 122 if (toolstrip_->height == 0) { |
107 error_ = kAlreadyCollapsedError; | 123 error_ = kAlreadyCollapsedError; |
108 return false; | 124 return false; |
109 } | 125 } |
110 | 126 |
111 GURL url; | 127 GURL url; |
112 if (args_->GetType() != Value::TYPE_NULL) { | 128 if (args_->GetType() != Value::TYPE_NULL) { |
113 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 129 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
114 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); | 130 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); |
115 | 131 |
116 if (args->HasKey(keys::kUrlKey)) { | 132 if (args->HasKey(keys::kUrlKey)) { |
117 std::string url_string; | 133 std::string url_string; |
118 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, | 134 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, |
119 &url_string)); | 135 &url_string)); |
120 url = GURL(url_string); | 136 url = ResolveURLForExtension(url_string, |
121 if (!url.is_valid() && !url.is_empty()) { | 137 dispatcher()->url(), |
138 dispatcher()->GetExtension()); | |
139 if (!url.is_valid()) { | |
122 error_ = kInvalidURLError; | 140 error_ = kInvalidURLError; |
123 return false; | 141 return false; |
124 } | 142 } |
125 } | 143 } |
126 } | 144 } |
127 | 145 |
128 model_->CollapseToolstrip(toolstrip_, url); | 146 model_->CollapseToolstrip(toolstrip_, url); |
129 return true; | 147 return true; |
130 } | 148 } |
131 | 149 |
(...skipping 29 matching lines...) Expand all Loading... | |
161 void ToolstripEventRouter::OnToolstripCollapsed(Profile* profile, | 179 void ToolstripEventRouter::OnToolstripCollapsed(Profile* profile, |
162 int routing_id, | 180 int routing_id, |
163 const GURL &url) { | 181 const GURL &url) { |
164 ListValue args; | 182 ListValue args; |
165 DictionaryValue* obj = new DictionaryValue(); | 183 DictionaryValue* obj = new DictionaryValue(); |
166 if (!url.is_empty()) | 184 if (!url.is_empty()) |
167 obj->SetString(keys::kUrlKey, url.spec()); | 185 obj->SetString(keys::kUrlKey, url.spec()); |
168 args.Append(obj); | 186 args.Append(obj); |
169 DispatchEvent(profile, routing_id, events::kOnToolstripCollapsed, args); | 187 DispatchEvent(profile, routing_id, events::kOnToolstripCollapsed, args); |
170 } | 188 } |
OLD | NEW |