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() { |
| 25 // TODO(jam): since temporarily ChromeWebUIDataSource is a URLDataSource and a |
| 26 // content::URLDataSourceDelegate, NULL the delegate pointer out to avoid a |
| 27 // double delete. |
| 28 release_delegate(); |
30 } | 29 } |
31 | 30 |
32 void ChromeWebUIDataSource::AddString(const std::string& name, | 31 void ChromeWebUIDataSource::AddString(const std::string& name, |
33 const string16& value) { | 32 const string16& value) { |
34 localized_strings_.SetString(name, value); | 33 localized_strings_.SetString(name, value); |
35 } | 34 } |
36 | 35 |
37 void ChromeWebUIDataSource::AddString(const std::string& name, | 36 void ChromeWebUIDataSource::AddString(const std::string& name, |
38 const std::string& value) { | 37 const std::string& value) { |
39 localized_strings_.SetString(name, value); | 38 localized_strings_.SetString(name, value); |
40 } | 39 } |
41 | 40 |
42 void ChromeWebUIDataSource::AddLocalizedString(const std::string& name, | 41 void ChromeWebUIDataSource::AddLocalizedString(const std::string& name, |
43 int ids) { | 42 int ids) { |
44 localized_strings_.SetString(name, l10n_util::GetStringUTF16(ids)); | 43 localized_strings_.SetString(name, l10n_util::GetStringUTF16(ids)); |
45 } | 44 } |
46 | 45 |
47 void ChromeWebUIDataSource::AddLocalizedStrings( | 46 void ChromeWebUIDataSource::AddLocalizedStrings( |
48 const DictionaryValue& localized_strings) { | 47 const DictionaryValue& localized_strings) { |
49 localized_strings_.MergeDictionary(&localized_strings); | 48 localized_strings_.MergeDictionary(&localized_strings); |
50 } | 49 } |
51 | 50 |
52 void ChromeWebUIDataSource::SetRequestFilter( | 51 void ChromeWebUIDataSource::SetRequestFilter( |
53 const HandleRequestCallback& callback) { | 52 const HandleRequestCallback& callback) { |
54 filter_callback_ = callback; | 53 filter_callback_ = callback; |
55 } | 54 } |
56 | 55 |
| 56 std::string ChromeWebUIDataSource::GetSource() { |
| 57 return source_name_; |
| 58 } |
| 59 |
57 std::string ChromeWebUIDataSource::GetMimeType(const std::string& path) const { | 60 std::string ChromeWebUIDataSource::GetMimeType(const std::string& path) const { |
58 if (EndsWith(path, ".js", false)) | 61 if (EndsWith(path, ".js", false)) |
59 return "application/javascript"; | 62 return "application/javascript"; |
60 | 63 |
61 if (EndsWith(path, ".json", false)) | 64 if (EndsWith(path, ".json", false)) |
62 return "application/json"; | 65 return "application/json"; |
63 | 66 |
64 if (EndsWith(path, ".pdf", false)) | 67 if (EndsWith(path, ".pdf", false)) |
65 return "application/pdf"; | 68 return "application/pdf"; |
66 | 69 |
67 return "text/html"; | 70 return "text/html"; |
68 } | 71 } |
69 | 72 |
70 void ChromeWebUIDataSource::StartDataRequest(const std::string& path, | 73 void ChromeWebUIDataSource::StartDataRequest(const std::string& path, |
71 bool is_incognito, | 74 bool is_incognito, |
72 int request_id) { | 75 int request_id) { |
73 if (!filter_callback_.is_null() && | 76 if (!filter_callback_.is_null() && |
74 filter_callback_.Run( | 77 filter_callback_.Run( |
75 path, | 78 path, |
76 base::Bind(&ChromeURLDataManager::DataSource::SendResponse, | 79 base::Bind(&URLDataSource::SendResponse, this, request_id))) { |
77 this, request_id))) { | |
78 return; | 80 return; |
79 } | 81 } |
80 | 82 |
81 if (!json_path_.empty() && path == json_path_) { | 83 if (!json_path_.empty() && path == json_path_) { |
82 SendLocalizedStringsAsJSON(request_id); | 84 SendLocalizedStringsAsJSON(request_id); |
83 return; | 85 return; |
84 } | 86 } |
85 | 87 |
86 int resource_id = default_resource_; | 88 int resource_id = default_resource_; |
87 std::map<std::string, int>::iterator result; | 89 std::map<std::string, int>::iterator result; |
88 result = path_to_idr_map_.find(path); | 90 result = path_to_idr_map_.find(path); |
89 if (result != path_to_idr_map_.end()) | 91 if (result != path_to_idr_map_.end()) |
90 resource_id = result->second; | 92 resource_id = result->second; |
91 DCHECK_NE(resource_id, -1); | 93 DCHECK_NE(resource_id, -1); |
92 SendFromResourceBundle(request_id, resource_id); | 94 SendFromResourceBundle(request_id, resource_id); |
93 } | 95 } |
94 | 96 |
95 void ChromeWebUIDataSource::SendLocalizedStringsAsJSON(int request_id) { | 97 void ChromeWebUIDataSource::SendLocalizedStringsAsJSON(int request_id) { |
96 std::string template_data; | 98 std::string template_data; |
97 SetFontAndTextDirection(&localized_strings_); | 99 URLDataSource::SetFontAndTextDirection(&localized_strings_); |
98 | 100 |
99 scoped_ptr<jstemplate_builder::UseVersion2> version2; | 101 scoped_ptr<jstemplate_builder::UseVersion2> version2; |
100 if (json_js_format_v2_) | 102 if (json_js_format_v2_) |
101 version2.reset(new jstemplate_builder::UseVersion2); | 103 version2.reset(new jstemplate_builder::UseVersion2); |
102 | 104 |
103 jstemplate_builder::AppendJsonJS(&localized_strings_, &template_data); | 105 jstemplate_builder::AppendJsonJS(&localized_strings_, &template_data); |
104 SendResponse(request_id, base::RefCountedString::TakeString(&template_data)); | 106 SendResponse(request_id, base::RefCountedString::TakeString(&template_data)); |
105 } | 107 } |
106 | 108 |
107 void ChromeWebUIDataSource::SendFromResourceBundle(int request_id, int idr) { | 109 void ChromeWebUIDataSource::SendFromResourceBundle(int request_id, int idr) { |
108 scoped_refptr<base::RefCountedStaticMemory> response( | 110 scoped_refptr<base::RefCountedStaticMemory> response( |
109 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 111 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
110 idr)); | 112 idr)); |
111 SendResponse(request_id, response); | 113 SendResponse(request_id, response); |
112 } | 114 } |
OLD | NEW |