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

Unified Diff: net/proxy/proxy_script_fetcher.cc

Issue 2817043: Reduce the copying of string data between C++ and javascript in proxy_resolver_v8.cc. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix comment typo 'converts' Created 10 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/proxy/proxy_script_fetcher.h ('k') | net/proxy/proxy_script_fetcher_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/proxy/proxy_script_fetcher.cc
===================================================================
--- net/proxy/proxy_script_fetcher.cc (revision 51195)
+++ net/proxy/proxy_script_fetcher.cc (working copy)
@@ -1,6 +1,6 @@
-// Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this
-// source code is governed by a BSD-style license that can be found in the
-// LICENSE file.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
#include "net/proxy/proxy_script_fetcher.h"
@@ -45,9 +45,12 @@
return false;
}
-// Convert |bytes| (which is encoded by |charset|) in place to UTF8.
+// Converts |bytes| (which is encoded by |charset|) to UTF16, saving the resul
+// to |*utf16|.
// If |charset| is empty, then we don't know what it was and guess.
-void ConvertResponseToUTF8(const std::string& charset, std::string* bytes) {
+void ConvertResponseToUTF16(const std::string& charset,
+ const std::string& bytes,
+ string16* utf16) {
const char* codepage;
if (charset.empty()) {
@@ -61,12 +64,9 @@
// We will be generous in the conversion -- if any characters lie
// outside of |charset| (i.e. invalid), then substitute them with
// U+FFFD rather than failing.
- std::wstring tmp_wide;
- base::CodepageToWide(*bytes, codepage,
- base::OnStringConversionError::SUBSTITUTE,
- &tmp_wide);
- // TODO(eroman): would be nice to have a CodepageToUTF8() function.
- *bytes = WideToUTF8(tmp_wide);
+ base::CodepageToUTF16(bytes, codepage,
+ base::OnStringConversionError::SUBSTITUTE,
+ utf16);
}
} // namespace
@@ -83,7 +83,7 @@
// ProxyScriptFetcher methods:
- virtual int Fetch(const GURL& url, std::string* bytes,
+ virtual int Fetch(const GURL& url, string16* text,
CompletionCallback* callback);
virtual void Cancel();
virtual URLRequestContext* GetRequestContext();
@@ -103,7 +103,7 @@
void ReadBody(URLRequest* request);
// Called once the request has completed to notify the caller of
- // |response_code_| and |response_bytes_|.
+ // |response_code_| and |response_text_|.
void FetchCompleted();
// Clear out the state for the current request.
@@ -140,9 +140,12 @@
// Holds the error condition that was hit on the current request, or OK.
int result_code_;
- // Holds the bytes read so far. Will not exceed |max_response_bytes|. This
- // buffer is owned by the owner of |callback|.
- std::string* result_bytes_;
+ // Holds the bytes read so far. Will not exceed |max_response_bytes|.
+ std::string bytes_read_so_far_;
+
+ // This buffer is owned by the owner of |callback|, and will be filled with
+ // UTF16 response on completion.
+ string16* result_text_;
};
ProxyScriptFetcherImpl::ProxyScriptFetcherImpl(
@@ -155,7 +158,7 @@
cur_request_id_(0),
callback_(NULL),
result_code_(OK),
- result_bytes_(NULL) {
+ result_text_(NULL) {
DCHECK(url_request_context);
}
@@ -165,13 +168,13 @@
}
int ProxyScriptFetcherImpl::Fetch(const GURL& url,
- std::string* bytes,
+ string16* text,
CompletionCallback* callback) {
// It is invalid to call Fetch() while a request is already in progress.
DCHECK(!cur_request_.get());
DCHECK(callback);
- DCHECK(bytes);
+ DCHECK(text);
cur_request_.reset(new URLRequest(url, this));
cur_request_->set_context(url_request_context_);
@@ -186,9 +189,10 @@
// Save the caller's info for notification on completion.
callback_ = callback;
- result_bytes_ = bytes;
- result_bytes_->clear();
+ result_text_ = text;
+ bytes_read_so_far_.clear();
+
// Post a task to timeout this request if it takes too long.
cur_request_id_ = ++next_id_;
MessageLoop::current()->PostDelayedTask(FROM_HERE,
@@ -269,13 +273,13 @@
DCHECK(request == cur_request_.get());
if (num_bytes > 0) {
// Enforce maximum size bound.
- if (num_bytes + result_bytes_->size() >
+ if (num_bytes + bytes_read_so_far_.size() >
static_cast<size_t>(max_response_bytes)) {
result_code_ = ERR_FILE_TOO_BIG;
request->Cancel();
return;
}
- result_bytes_->append(buf_->data(), num_bytes);
+ bytes_read_so_far_.append(buf_->data(), num_bytes);
ReadBody(request);
} else { // Error while reading, or EOF
OnResponseCompleted(request);
@@ -305,13 +309,13 @@
void ProxyScriptFetcherImpl::FetchCompleted() {
if (result_code_ == OK) {
- // The caller expects the response to be encoded as UTF8.
+ // The caller expects the response to be encoded as UTF16.
std::string charset;
cur_request_->GetCharset(&charset);
- ConvertResponseToUTF8(charset, result_bytes_);
+ ConvertResponseToUTF16(charset, bytes_read_so_far_, result_text_);
} else {
// On error, the caller expects empty string for bytes.
- result_bytes_->clear();
+ result_text_->clear();
}
int result_code = result_code_;
@@ -327,7 +331,7 @@
cur_request_id_ = 0;
callback_ = NULL;
result_code_ = OK;
- result_bytes_ = NULL;
+ result_text_ = NULL;
}
void ProxyScriptFetcherImpl::OnTimeout(int id) {
« no previous file with comments | « net/proxy/proxy_script_fetcher.h ('k') | net/proxy/proxy_script_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698