Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/webui/chrome_web_ui_data_source.h" | 5 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "chrome/common/jstemplate_builder.h" | 11 #include "chrome/common/jstemplate_builder.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 12 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 14 | 14 |
| 15 ChromeWebUIDataSource::ChromeWebUIDataSource(const std::string& source_name) | 15 ChromeWebUIDataSource::ChromeWebUIDataSource(const std::string& source_name) |
| 16 : DataSource(source_name, MessageLoop::current()), | 16 : DataSource(source_name, MessageLoop::current()), |
| 17 default_resource_(-1) {} | 17 default_resource_(-1) { |
| 18 } | |
| 19 | |
| 20 ChromeWebUIDataSource::ChromeWebUIDataSource(const std::string& source_name, | |
| 21 MessageLoop* loop) | |
| 22 : DataSource(source_name, loop), | |
| 23 default_resource_(-1) { | |
| 24 } | |
| 18 | 25 |
| 19 ChromeWebUIDataSource::~ChromeWebUIDataSource() { | 26 ChromeWebUIDataSource::~ChromeWebUIDataSource() { |
| 20 } | 27 } |
| 21 | 28 |
| 22 void ChromeWebUIDataSource::AddString(const std::string& name, | 29 void ChromeWebUIDataSource::AddString(const std::string& name, |
| 23 const string16& value) { | 30 const string16& value) { |
| 24 localized_strings_.SetString(name, value); | 31 localized_strings_.SetString(name, value); |
| 25 } | 32 } |
| 26 | 33 |
| 27 void ChromeWebUIDataSource::AddLocalizedString(const std::string& name, | 34 void ChromeWebUIDataSource::AddLocalizedString(const std::string& name, |
| 28 int ids) { | 35 int ids) { |
| 29 localized_strings_.SetString(name, l10n_util::GetStringUTF16(ids)); | 36 localized_strings_.SetString(name, l10n_util::GetStringUTF16(ids)); |
| 30 } | 37 } |
| 31 | 38 |
| 32 std::string ChromeWebUIDataSource::GetMimeType(const std::string& path) const { | 39 std::string ChromeWebUIDataSource::GetMimeType(const std::string& path) const { |
| 33 if (EndsWith(path, ".js", false)) | 40 if (EndsWith(path, ".js", false)) |
| 34 return "application/javascript"; | 41 return "application/javascript"; |
| 35 | 42 |
| 43 if (EndsWith(path, ".json", false)) | |
| 44 return "application/json"; | |
| 45 | |
| 36 if (EndsWith(path, ".pdf", false)) | 46 if (EndsWith(path, ".pdf", false)) |
| 37 return "application/pdf"; | 47 return "application/pdf"; |
| 38 | 48 |
| 39 return "text/html"; | 49 return "text/html"; |
| 40 } | 50 } |
| 41 | 51 |
| 42 void ChromeWebUIDataSource::StartDataRequest(const std::string& path, | 52 void ChromeWebUIDataSource::StartDataRequest(const std::string& path, |
| 43 bool is_incognito, | 53 bool is_incognito, |
| 44 int request_id) { | 54 int request_id) { |
| 45 if (path == json_path_) { | 55 if (!json_path_.empty() && path == json_path_) { |
|
Evan Stade
2011/08/24 20:47:23
I think it would be perhaps clearer to instead do
Tom Sepez
2011/08/24 20:52:36
Not quite. json_path_ can be empty if the particu
| |
| 46 SendLocalizedStringsAsJSON(request_id); | 56 SendLocalizedStringsAsJSON(request_id); |
| 47 } else { | 57 } else { |
| 48 int resource_id = default_resource_; | 58 int resource_id = default_resource_; |
| 49 std::map<std::string, int>::iterator result; | 59 std::map<std::string, int>::iterator result; |
| 50 result = path_to_idr_map_.find(path); | 60 result = path_to_idr_map_.find(path); |
| 51 if (result != path_to_idr_map_.end()) | 61 if (result != path_to_idr_map_.end()) |
| 52 resource_id = result->second; | 62 resource_id = result->second; |
| 53 DCHECK_NE(resource_id, -1); | 63 DCHECK_NE(resource_id, -1); |
| 54 SendFromResourceBundle(request_id, resource_id); | 64 SendFromResourceBundle(request_id, resource_id); |
| 55 } | 65 } |
| 56 } | 66 } |
| 57 | 67 |
| 58 void ChromeWebUIDataSource::SendLocalizedStringsAsJSON(int request_id) { | 68 void ChromeWebUIDataSource::SendLocalizedStringsAsJSON(int request_id) { |
| 59 std::string template_data; | 69 std::string template_data; |
| 60 SetFontAndTextDirection(&localized_strings_); | 70 SetFontAndTextDirection(&localized_strings_); |
| 61 jstemplate_builder::AppendJsonJS(&localized_strings_, &template_data); | 71 jstemplate_builder::AppendJsonJS(&localized_strings_, &template_data); |
| 62 SendResponse(request_id, base::RefCountedString::TakeString(&template_data)); | 72 SendResponse(request_id, base::RefCountedString::TakeString(&template_data)); |
| 63 } | 73 } |
| 64 | 74 |
| 65 void ChromeWebUIDataSource::SendFromResourceBundle(int request_id, int idr) { | 75 void ChromeWebUIDataSource::SendFromResourceBundle(int request_id, int idr) { |
| 66 scoped_refptr<RefCountedStaticMemory> response( | 76 scoped_refptr<RefCountedStaticMemory> response( |
| 67 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(idr)); | 77 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(idr)); |
| 68 SendResponse(request_id, response); | 78 SendResponse(request_id, response); |
| 69 } | 79 } |
| OLD | NEW |