Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(538)

Side by Side Diff: chrome/browser/ui/webui/chrome_web_ui_data_source.cc

Issue 11881055: Simplify WebUI data sources. Currently WebUI data sources implement a URLDataSourceDelegate interfa… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix issue in about_ui exposed by cros tests Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/browser/ui/webui/web_ui_util.h"
12 #include "chrome/common/jstemplate_builder.h" 13 #include "chrome/common/jstemplate_builder.h"
13 #include "ui/base/l10n/l10n_util.h" 14 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h" 15 #include "ui/base/resource/resource_bundle.h"
15 16
17 // Internal class to hide the fact that ChromeWebUIDataSource implements
18 // content::URLDataSource.
19 class ChromeWebUIDataSource::InternalDataSource
20 : public content::URLDataSource {
21 public:
22 InternalDataSource(ChromeWebUIDataSource* parent) : parent_(parent) {
23 }
24
25 ~InternalDataSource() {
26 }
27
28 // content::URLDataSource implementation.
29 virtual std::string GetSource() OVERRIDE {
30 return parent_->GetSource();
31 }
32 virtual std::string GetMimeType(const std::string& path) const OVERRIDE {
33 return parent_->GetMimeType(path);
34 }
35 virtual void StartDataRequest(
36 const std::string& path,
37 bool is_incognito,
38 const content::URLDataSource::GotDataCallback& callback) OVERRIDE {
39 return parent_->StartDataRequest(path, is_incognito, callback);
40 }
41
42 private:
43 ChromeWebUIDataSource* parent_;
44 };
45
16 ChromeWebUIDataSource::ChromeWebUIDataSource(const std::string& source_name) 46 ChromeWebUIDataSource::ChromeWebUIDataSource(const std::string& source_name)
17 : URLDataSource(source_name, this), 47 : URLDataSourceImpl(
48 source_name,
49 new InternalDataSource(ALLOW_THIS_IN_INITIALIZER_LIST(this))),
18 source_name_(source_name), 50 source_name_(source_name),
19 default_resource_(-1), 51 default_resource_(-1),
20 json_js_format_v2_(false) { 52 json_js_format_v2_(false) {
21 url_data_source_ = this;
22 } 53 }
23 54
24 ChromeWebUIDataSource::~ChromeWebUIDataSource() { 55 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();
29 } 56 }
30 57
31 void ChromeWebUIDataSource::AddString(const std::string& name, 58 void ChromeWebUIDataSource::AddString(const std::string& name,
32 const string16& value) { 59 const string16& value) {
33 localized_strings_.SetString(name, value); 60 localized_strings_.SetString(name, value);
34 } 61 }
35 62
36 void ChromeWebUIDataSource::AddString(const std::string& name, 63 void ChromeWebUIDataSource::AddString(const std::string& name,
37 const std::string& value) { 64 const std::string& value) {
38 localized_strings_.SetString(name, value); 65 localized_strings_.SetString(name, value);
(...skipping 24 matching lines...) Expand all
63 90
64 if (EndsWith(path, ".json", false)) 91 if (EndsWith(path, ".json", false))
65 return "application/json"; 92 return "application/json";
66 93
67 if (EndsWith(path, ".pdf", false)) 94 if (EndsWith(path, ".pdf", false))
68 return "application/pdf"; 95 return "application/pdf";
69 96
70 return "text/html"; 97 return "text/html";
71 } 98 }
72 99
73 void ChromeWebUIDataSource::StartDataRequest(const std::string& path, 100 void ChromeWebUIDataSource::StartDataRequest(
74 bool is_incognito, 101 const std::string& path,
75 int request_id) { 102 bool is_incognito,
103 const content::URLDataSource::GotDataCallback& callback) {
76 if (!filter_callback_.is_null() && 104 if (!filter_callback_.is_null() &&
77 filter_callback_.Run( 105 filter_callback_.Run(path, callback)) {
78 path,
79 base::Bind(&URLDataSource::SendResponse, this, request_id))) {
80 return; 106 return;
81 } 107 }
82 108
83 if (!json_path_.empty() && path == json_path_) { 109 if (!json_path_.empty() && path == json_path_) {
84 SendLocalizedStringsAsJSON(request_id); 110 SendLocalizedStringsAsJSON(callback);
85 return; 111 return;
86 } 112 }
87 113
88 int resource_id = default_resource_; 114 int resource_id = default_resource_;
89 std::map<std::string, int>::iterator result; 115 std::map<std::string, int>::iterator result;
90 result = path_to_idr_map_.find(path); 116 result = path_to_idr_map_.find(path);
91 if (result != path_to_idr_map_.end()) 117 if (result != path_to_idr_map_.end())
92 resource_id = result->second; 118 resource_id = result->second;
93 DCHECK_NE(resource_id, -1); 119 DCHECK_NE(resource_id, -1);
94 SendFromResourceBundle(request_id, resource_id); 120 SendFromResourceBundle(callback, resource_id);
95 } 121 }
96 122
97 void ChromeWebUIDataSource::SendLocalizedStringsAsJSON(int request_id) { 123 void ChromeWebUIDataSource::SendLocalizedStringsAsJSON(
124 const content::URLDataSource::GotDataCallback& callback) {
98 std::string template_data; 125 std::string template_data;
99 URLDataSource::SetFontAndTextDirection(&localized_strings_); 126 web_ui_util::SetFontAndTextDirection(&localized_strings_);
100 127
101 scoped_ptr<jstemplate_builder::UseVersion2> version2; 128 scoped_ptr<jstemplate_builder::UseVersion2> version2;
102 if (json_js_format_v2_) 129 if (json_js_format_v2_)
103 version2.reset(new jstemplate_builder::UseVersion2); 130 version2.reset(new jstemplate_builder::UseVersion2);
104 131
105 jstemplate_builder::AppendJsonJS(&localized_strings_, &template_data); 132 jstemplate_builder::AppendJsonJS(&localized_strings_, &template_data);
106 SendResponse(request_id, base::RefCountedString::TakeString(&template_data)); 133 callback.Run(base::RefCountedString::TakeString(&template_data));
107 } 134 }
108 135
109 void ChromeWebUIDataSource::SendFromResourceBundle(int request_id, int idr) { 136 void ChromeWebUIDataSource::SendFromResourceBundle(
137 const content::URLDataSource::GotDataCallback& callback, int idr) {
110 scoped_refptr<base::RefCountedStaticMemory> response( 138 scoped_refptr<base::RefCountedStaticMemory> response(
111 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( 139 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
112 idr)); 140 idr));
113 SendResponse(request_id, response); 141 callback.Run(response);
114 } 142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698