| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "chrome/common/jstemplate_builder.h" | 12 #include "chrome/common/jstemplate_builder.h" |
| 13 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
| 14 #include "ui/base/resource/resource_bundle.h" | 14 #include "ui/base/resource/resource_bundle.h" |
| 15 | 15 |
| 16 ChromeWebUIDataSource::ChromeWebUIDataSource(const std::string& source_name) | 16 ChromeWebUIDataSource::ChromeWebUIDataSource(const std::string& source_name) |
| 17 : DataSource(source_name, MessageLoop::current()), | 17 : URLDataSource(source_name, this), |
| 18 source_name_(source_name), |
| 18 default_resource_(-1), | 19 default_resource_(-1), |
| 19 json_js_format_v2_(false) { | 20 json_js_format_v2_(false) { |
| 20 } | 21 url_data_source_ = this; |
| 21 | |
| 22 ChromeWebUIDataSource::ChromeWebUIDataSource(const std::string& source_name, | |
| 23 MessageLoop* loop) | |
| 24 : DataSource(source_name, loop), | |
| 25 default_resource_(-1), | |
| 26 json_js_format_v2_(false) { | |
| 27 } | 22 } |
| 28 | 23 |
| 29 ChromeWebUIDataSource::~ChromeWebUIDataSource() { | 24 ChromeWebUIDataSource::~ChromeWebUIDataSource() { |
| 30 } | 25 } |
| 31 | 26 |
| 32 void ChromeWebUIDataSource::AddString(const std::string& name, | 27 void ChromeWebUIDataSource::AddString(const std::string& name, |
| 33 const string16& value) { | 28 const string16& value) { |
| 34 localized_strings_.SetString(name, value); | 29 localized_strings_.SetString(name, value); |
| 35 } | 30 } |
| 36 | 31 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 47 void ChromeWebUIDataSource::AddLocalizedStrings( | 42 void ChromeWebUIDataSource::AddLocalizedStrings( |
| 48 const DictionaryValue& localized_strings) { | 43 const DictionaryValue& localized_strings) { |
| 49 localized_strings_.MergeDictionary(&localized_strings); | 44 localized_strings_.MergeDictionary(&localized_strings); |
| 50 } | 45 } |
| 51 | 46 |
| 52 void ChromeWebUIDataSource::SetRequestFilter( | 47 void ChromeWebUIDataSource::SetRequestFilter( |
| 53 const HandleRequestCallback& callback) { | 48 const HandleRequestCallback& callback) { |
| 54 filter_callback_ = callback; | 49 filter_callback_ = callback; |
| 55 } | 50 } |
| 56 | 51 |
| 52 std::string ChromeWebUIDataSource::GetSource() { |
| 53 return source_name_; |
| 54 } |
| 55 |
| 57 std::string ChromeWebUIDataSource::GetMimeType(const std::string& path) const { | 56 std::string ChromeWebUIDataSource::GetMimeType(const std::string& path) const { |
| 58 if (EndsWith(path, ".js", false)) | 57 if (EndsWith(path, ".js", false)) |
| 59 return "application/javascript"; | 58 return "application/javascript"; |
| 60 | 59 |
| 61 if (EndsWith(path, ".json", false)) | 60 if (EndsWith(path, ".json", false)) |
| 62 return "application/json"; | 61 return "application/json"; |
| 63 | 62 |
| 64 if (EndsWith(path, ".pdf", false)) | 63 if (EndsWith(path, ".pdf", false)) |
| 65 return "application/pdf"; | 64 return "application/pdf"; |
| 66 | 65 |
| 67 return "text/html"; | 66 return "text/html"; |
| 68 } | 67 } |
| 69 | 68 |
| 70 void ChromeWebUIDataSource::StartDataRequest(const std::string& path, | 69 void ChromeWebUIDataSource::StartDataRequest(const std::string& path, |
| 71 bool is_incognito, | 70 bool is_incognito, |
| 72 int request_id) { | 71 int request_id) { |
| 73 if (!filter_callback_.is_null() && | 72 if (!filter_callback_.is_null() && |
| 74 filter_callback_.Run( | 73 filter_callback_.Run( |
| 75 path, | 74 path, |
| 76 base::Bind(&ChromeURLDataManager::DataSource::SendResponse, | 75 base::Bind(&URLDataSource::SendResponse, this, request_id))) { |
| 77 this, request_id))) { | |
| 78 return; | 76 return; |
| 79 } | 77 } |
| 80 | 78 |
| 81 if (!json_path_.empty() && path == json_path_) { | 79 if (!json_path_.empty() && path == json_path_) { |
| 82 SendLocalizedStringsAsJSON(request_id); | 80 SendLocalizedStringsAsJSON(request_id); |
| 83 return; | 81 return; |
| 84 } | 82 } |
| 85 | 83 |
| 86 int resource_id = default_resource_; | 84 int resource_id = default_resource_; |
| 87 std::map<std::string, int>::iterator result; | 85 std::map<std::string, int>::iterator result; |
| 88 result = path_to_idr_map_.find(path); | 86 result = path_to_idr_map_.find(path); |
| 89 if (result != path_to_idr_map_.end()) | 87 if (result != path_to_idr_map_.end()) |
| 90 resource_id = result->second; | 88 resource_id = result->second; |
| 91 DCHECK_NE(resource_id, -1); | 89 DCHECK_NE(resource_id, -1); |
| 92 SendFromResourceBundle(request_id, resource_id); | 90 SendFromResourceBundle(request_id, resource_id); |
| 93 } | 91 } |
| 94 | 92 |
| 95 void ChromeWebUIDataSource::SendLocalizedStringsAsJSON(int request_id) { | 93 void ChromeWebUIDataSource::SendLocalizedStringsAsJSON(int request_id) { |
| 96 std::string template_data; | 94 std::string template_data; |
| 97 SetFontAndTextDirection(&localized_strings_); | 95 URLDataSource::SetFontAndTextDirection(&localized_strings_); |
| 98 | 96 |
| 99 scoped_ptr<jstemplate_builder::UseVersion2> version2; | 97 scoped_ptr<jstemplate_builder::UseVersion2> version2; |
| 100 if (json_js_format_v2_) | 98 if (json_js_format_v2_) |
| 101 version2.reset(new jstemplate_builder::UseVersion2); | 99 version2.reset(new jstemplate_builder::UseVersion2); |
| 102 | 100 |
| 103 jstemplate_builder::AppendJsonJS(&localized_strings_, &template_data); | 101 jstemplate_builder::AppendJsonJS(&localized_strings_, &template_data); |
| 104 SendResponse(request_id, base::RefCountedString::TakeString(&template_data)); | 102 SendResponse(request_id, base::RefCountedString::TakeString(&template_data)); |
| 105 } | 103 } |
| 106 | 104 |
| 107 void ChromeWebUIDataSource::SendFromResourceBundle(int request_id, int idr) { | 105 void ChromeWebUIDataSource::SendFromResourceBundle(int request_id, int idr) { |
| 108 scoped_refptr<base::RefCountedStaticMemory> response( | 106 scoped_refptr<base::RefCountedStaticMemory> response( |
| 109 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 107 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 110 idr)); | 108 idr)); |
| 111 SendResponse(request_id, response); | 109 SendResponse(request_id, response); |
| 112 } | 110 } |
| OLD | NEW |