diff options
Diffstat (limited to 'content')
-rw-r--r-- | content/browser/DEPS | 3 | ||||
-rw-r--r-- | content/browser/find_pasteboard.h | 58 | ||||
-rw-r--r-- | content/browser/find_pasteboard.mm | 82 | ||||
-rw-r--r-- | content/browser/net/url_request_failed_dns_job.cc | 46 | ||||
-rw-r--r-- | content/browser/net/url_request_failed_dns_job.h | 37 | ||||
-rw-r--r-- | content/browser/net/url_request_mock_http_job.cc | 118 | ||||
-rw-r--r-- | content/browser/net/url_request_mock_http_job.h | 53 | ||||
-rw-r--r-- | content/browser/net/url_request_mock_net_error_job.cc | 125 | ||||
-rw-r--r-- | content/browser/net/url_request_mock_net_error_job.h | 62 | ||||
-rw-r--r-- | content/browser/net/url_request_slow_download_job.cc | 184 | ||||
-rw-r--r-- | content/browser/net/url_request_slow_download_job.h | 64 | ||||
-rw-r--r-- | content/browser/net/url_request_slow_http_job.cc | 62 | ||||
-rw-r--r-- | content/browser/net/url_request_slow_http_job.h | 42 | ||||
-rw-r--r-- | content/browser/renderer_host/clipboard_message_filter_mac.mm | 2 | ||||
-rw-r--r-- | content/browser/renderer_host/resource_dispatcher_host_uitest.cc | 4 | ||||
-rw-r--r-- | content/content_browser.gypi | 12 |
16 files changed, 948 insertions, 6 deletions
diff --git a/content/browser/DEPS b/content/browser/DEPS index 2a04b2b..1480204 100644 --- a/content/browser/DEPS +++ b/content/browser/DEPS @@ -31,8 +31,5 @@ include_rules = [ "+chrome/browser/tab_contents/render_view_host_delegate_helper.h", # ONLY USED BY TESTS - "+chrome/browser/net/url_request_failed_dns_job.h", - "+chrome/browser/net/url_request_mock_http_job.h", "+chrome/browser/ui/browser.h", - "+chrome/browser/ui/cocoa/find_pasteboard.h", ] diff --git a/content/browser/find_pasteboard.h b/content/browser/find_pasteboard.h new file mode 100644 index 0000000..9369ba6 --- /dev/null +++ b/content/browser/find_pasteboard.h @@ -0,0 +1,58 @@ +// Copyright (c) 2011 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. + +#ifndef CONTENT_BROWSER_FIND_PASTEBOARD_H_ +#define CONTENT_BROWSER_FIND_PASTEBOARD_H_ +#pragma once + +#include "base/string16.h" + +#ifdef __OBJC__ + +#import <Cocoa/Cocoa.h> + +#include "base/memory/scoped_nsobject.h" + +extern NSString* kFindPasteboardChangedNotification; + +// Manages the find pasteboard. Use this to copy text to the find pasteboard, +// to get the text currently on the find pasteboard, and to receive +// notifications when the text on the find pasteboard has changed. You should +// always use this class instead of accessing +// [NSPasteboard pasteboardWithName:NSFindPboard] directly. +// +// This is not thread-safe and must be used on the main thread. +// +// This is supposed to be a singleton. +@interface FindPasteboard : NSObject { + @private + scoped_nsobject<NSString> findText_; +} + +// Returns the singleton instance of this class. ++ (FindPasteboard*)sharedInstance; + +// Returns the current find text. This is never nil; if there is no text on the +// find pasteboard, this returns an empty string. +- (NSString*)findText; + +// Sets the current find text to |newText| and sends a +// |kFindPasteboardChangedNotification| to the default notification center if +// it the new text different from the current text. |newText| must not be nil. +- (void)setFindText:(NSString*)newText; +@end + +@interface FindPasteboard (TestingAPI) +- (void)loadTextFromPasteboard:(NSNotification*)notification; + +// This methods is meant to be overridden in tests. +- (NSPasteboard*)findPboard; +@end + +#endif // __OBJC__ + +// Also provide a c++ interface +string16 GetFindPboardText(); + +#endif // CONTENT_BROWSER_FIND_PASTEBOARD_H_ diff --git a/content/browser/find_pasteboard.mm b/content/browser/find_pasteboard.mm new file mode 100644 index 0000000..2d61db1 --- /dev/null +++ b/content/browser/find_pasteboard.mm @@ -0,0 +1,82 @@ +// Copyright (c) 2009 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. + +#import "content/browser/find_pasteboard.h" + +#include "base/logging.h" +#include "base/sys_string_conversions.h" + +NSString* kFindPasteboardChangedNotification = + @"kFindPasteboardChangedNotification_Chrome"; + +@implementation FindPasteboard + ++ (FindPasteboard*)sharedInstance { + static FindPasteboard* instance = nil; + if (!instance) { + instance = [[FindPasteboard alloc] init]; + } + return instance; +} + +- (id)init { + if ((self = [super init])) { + findText_.reset([[NSString alloc] init]); + + // Check if the text in the findboard has changed on app activate. + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(loadTextFromPasteboard:) + name:NSApplicationDidBecomeActiveNotification + object:nil]; + [self loadTextFromPasteboard:nil]; + } + return self; +} + +- (void)dealloc { + // Since this is a singleton, this should only be executed in test code. + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [super dealloc]; +} + +- (NSPasteboard*)findPboard { + return [NSPasteboard pasteboardWithName:NSFindPboard]; +} + +- (void)loadTextFromPasteboard:(NSNotification*)notification { + NSPasteboard* findPboard = [self findPboard]; + if ([[findPboard types] containsObject:NSStringPboardType]) + [self setFindText:[findPboard stringForType:NSStringPboardType]]; +} + +- (NSString*)findText { + return findText_; +} + +- (void)setFindText:(NSString*)newText { + DCHECK(newText); + if (!newText) + return; + + DCHECK([NSThread isMainThread]); + + BOOL needToSendNotification = ![findText_.get() isEqualToString:newText]; + if (needToSendNotification) { + findText_.reset([newText copy]); + NSPasteboard* findPboard = [self findPboard]; + [findPboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] + owner:nil]; + [findPboard setString:findText_.get() forType:NSStringPboardType]; + [[NSNotificationCenter defaultCenter] + postNotificationName:kFindPasteboardChangedNotification + object:self]; + } +} + +@end + +string16 GetFindPboardText() { + return base::SysNSStringToUTF16([[FindPasteboard sharedInstance] findText]); +} diff --git a/content/browser/net/url_request_failed_dns_job.cc b/content/browser/net/url_request_failed_dns_job.cc new file mode 100644 index 0000000..bffb121 --- /dev/null +++ b/content/browser/net/url_request_failed_dns_job.cc @@ -0,0 +1,46 @@ +// Copyright (c) 2011 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 "content/browser/net/url_request_failed_dns_job.h" + +#include "base/compiler_specific.h" +#include "base/message_loop.h" +#include "googleurl/src/gurl.h" +#include "net/base/net_errors.h" +#include "net/url_request/url_request.h" +#include "net/url_request/url_request_filter.h" + +const char URLRequestFailedDnsJob::kTestUrl[] = + "http://url.handled.by.fake.dns/"; + +URLRequestFailedDnsJob::URLRequestFailedDnsJob(net::URLRequest* request) + : net::URLRequestJob(request), + ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} + +URLRequestFailedDnsJob::~URLRequestFailedDnsJob() {} + +void URLRequestFailedDnsJob::Start() { + MessageLoop::current()->PostTask( + FROM_HERE, + method_factory_.NewRunnableMethod( + &URLRequestFailedDnsJob::StartAsync)); +} + +// static +void URLRequestFailedDnsJob::AddUrlHandler() { + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); + filter->AddUrlHandler(GURL(kTestUrl), + &URLRequestFailedDnsJob::Factory); +} + +/*static */ +net::URLRequestJob* URLRequestFailedDnsJob::Factory(net::URLRequest* request, + const std::string& scheme) { + return new URLRequestFailedDnsJob(request); +} + +void URLRequestFailedDnsJob::StartAsync() { + NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, + net::ERR_NAME_NOT_RESOLVED)); +} diff --git a/content/browser/net/url_request_failed_dns_job.h b/content/browser/net/url_request_failed_dns_job.h new file mode 100644 index 0000000..55d1bac --- /dev/null +++ b/content/browser/net/url_request_failed_dns_job.h @@ -0,0 +1,37 @@ +// 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. +// This class simulates what wininet does when a dns lookup fails. + +#ifndef CONTENT_BROWSER_NET_URL_REQUEST_FAILED_DNS_JOB_H_ +#define CONTENT_BROWSER_NET_URL_REQUEST_FAILED_DNS_JOB_H_ +#pragma once + +#include "base/task.h" +#include "net/url_request/url_request_job.h" + +class URLRequestFailedDnsJob : public net::URLRequestJob { + public: + explicit URLRequestFailedDnsJob(net::URLRequest* request); + + virtual void Start(); + + static net::URLRequestJob* Factory(net::URLRequest* request, + const std::string& scheme); + + // A test URL that can be used in UI tests. + static const char kTestUrl[]; + + // Adds the testing URLs to the net::URLRequestFilter. + static void AddUrlHandler(); + + private: + virtual ~URLRequestFailedDnsJob(); + + // Simulate a DNS failure. + void StartAsync(); + + ScopedRunnableMethodFactory<URLRequestFailedDnsJob> method_factory_; +}; + +#endif // CONTENT_BROWSER_NET_URL_REQUEST_FAILED_DNS_JOB_H_ diff --git a/content/browser/net/url_request_mock_http_job.cc b/content/browser/net/url_request_mock_http_job.cc new file mode 100644 index 0000000..43dde94 --- /dev/null +++ b/content/browser/net/url_request_mock_http_job.cc @@ -0,0 +1,118 @@ +// 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 "content/browser/net/url_request_mock_http_job.h" + +#include "base/file_util.h" +#include "base/message_loop.h" +#include "base/string_util.h" +#include "base/threading/thread_restrictions.h" +#include "base/utf_string_conversions.h" +#include "chrome/common/url_constants.h" +#include "net/base/net_util.h" +#include "net/http/http_response_headers.h" +#include "net/url_request/url_request_filter.h" + +static const char kMockHostname[] = "mock.http"; +static const FilePath::CharType kMockHeaderFileSuffix[] = + FILE_PATH_LITERAL(".mock-http-headers"); + +FilePath URLRequestMockHTTPJob::base_path_; + +// static +net::URLRequestJob* URLRequestMockHTTPJob::Factory(net::URLRequest* request, + const std::string& scheme) { + return new URLRequestMockHTTPJob(request, + GetOnDiskPath(base_path_, request, scheme)); +} + +// static +void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { + base_path_ = base_path; + + // Add kMockHostname to net::URLRequestFilter. + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); + filter->AddHostnameHandler("http", kMockHostname, + URLRequestMockHTTPJob::Factory); +} + +/* static */ +GURL URLRequestMockHTTPJob::GetMockUrl(const FilePath& path) { + std::string url = "http://"; + url.append(kMockHostname); + url.append("/"); + std::string path_str = path.MaybeAsASCII(); + DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. + url.append(path_str); + return GURL(url); +} + +/* static */ +GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const FilePath& path) { + std::string url = chrome::kViewSourceScheme; + url.append(":"); + url.append(GetMockUrl(path).spec()); + return GURL(url); +} + +/* static */ +FilePath URLRequestMockHTTPJob::GetOnDiskPath(const FilePath& base_path, + net::URLRequest* request, + const std::string& scheme) { + // Conceptually we just want to "return base_path + request->url().path()". + // But path in the request URL is in URL space (i.e. %-encoded spaces). + // So first we convert base FilePath to a URL, then append the URL + // path to that, and convert the final URL back to a FilePath. + GURL file_url(net::FilePathToFileURL(base_path)); + std::string url = file_url.spec() + request->url().path(); + FilePath file_path; + net::FileURLToFilePath(GURL(url), &file_path); + return file_path; +} + +URLRequestMockHTTPJob::URLRequestMockHTTPJob(net::URLRequest* request, + const FilePath& file_path) + : net::URLRequestFileJob(request, file_path) { } + +// Public virtual version. +void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { + // Forward to private const version. + GetResponseInfoConst(info); +} + +bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, + int* http_status_code) { + // Override the net::URLRequestFileJob implementation to invoke the default + // one based on HttpResponseInfo. + return net::URLRequestJob::IsRedirectResponse(location, http_status_code); +} + +// Private const version. +void URLRequestMockHTTPJob::GetResponseInfoConst( + net::HttpResponseInfo* info) const { + // We have to load our headers from disk, but we only use this class + // from tests, so allow these IO operations to happen on any thread. + base::ThreadRestrictions::ScopedAllowIO allow_io; + + FilePath header_file = FilePath(file_path_.value() + kMockHeaderFileSuffix); + std::string raw_headers; + if (!file_util::ReadFileToString(header_file, &raw_headers)) + return; + + // ParseRawHeaders expects \0 to end each header line. + ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); + info->headers = new net::HttpResponseHeaders(raw_headers); +} + +bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { + net::HttpResponseInfo info; + GetResponseInfoConst(&info); + return info.headers && info.headers->GetMimeType(mime_type); +} + +bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { + net::HttpResponseInfo info; + GetResponseInfo(&info); + return info.headers && info.headers->GetCharset(charset); +} diff --git a/content/browser/net/url_request_mock_http_job.h b/content/browser/net/url_request_mock_http_job.h new file mode 100644 index 0000000..8c6b5fb --- /dev/null +++ b/content/browser/net/url_request_mock_http_job.h @@ -0,0 +1,53 @@ +// 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. +// +// A net::URLRequestJob class that pulls the content and http headers from disk. + +#ifndef CONTENT_BROWSER_NET_URL_REQUEST_MOCK_HTTP_JOB_H_ +#define CONTENT_BROWSER_NET_URL_REQUEST_MOCK_HTTP_JOB_H_ +#pragma once + +#include <string> + +#include "net/url_request/url_request_file_job.h" + +class FilePath; + +class URLRequestMockHTTPJob : public net::URLRequestFileJob { + public: + URLRequestMockHTTPJob(net::URLRequest* request, const FilePath& file_path); + + virtual bool GetMimeType(std::string* mime_type) const; + virtual bool GetCharset(std::string* charset); + virtual void GetResponseInfo(net::HttpResponseInfo* info); + virtual bool IsRedirectResponse(GURL* location, int* http_status_code); + + static net::URLRequest::ProtocolFactory Factory; + + // Adds the testing URLs to the net::URLRequestFilter. + static void AddUrlHandler(const FilePath& base_path); + + // Given the path to a file relative to base_path_, construct a mock URL. + static GURL GetMockUrl(const FilePath& path); + + // Given the path to a file relative to base_path_, + // construct a mock URL for view source. + static GURL GetMockViewSourceUrl(const FilePath& path); + + protected: + virtual ~URLRequestMockHTTPJob() { } + + static FilePath GetOnDiskPath(const FilePath& base_path, + net::URLRequest* request, + const std::string& scheme); + + private: + void GetResponseInfoConst(net::HttpResponseInfo* info) const; + + // This is the file path leading to the root of the directory to use as the + // root of the http server. + static FilePath base_path_; +}; + +#endif // CONTENT_BROWSER_NET_URL_REQUEST_MOCK_HTTP_JOB_H_ diff --git a/content/browser/net/url_request_mock_net_error_job.cc b/content/browser/net/url_request_mock_net_error_job.cc new file mode 100644 index 0000000..69e3294 --- /dev/null +++ b/content/browser/net/url_request_mock_net_error_job.cc @@ -0,0 +1,125 @@ +// Copyright (c) 2011 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 "content/browser/net/url_request_mock_net_error_job.h" + +#include <string> +#include <vector> + +#include "base/compiler_specific.h" +#include "base/file_util.h" +#include "base/message_loop.h" +#include "base/task.h" +#include "base/utf_string_conversions.h" +#include "net/base/net_errors.h" +#include "net/base/net_util.h" +#include "net/base/x509_certificate.h" +#include "net/url_request/url_request_filter.h" + +// static +URLRequestMockNetErrorJob::URLMockInfoMap + URLRequestMockNetErrorJob::url_mock_info_map_; + +struct URLRequestMockNetErrorJob::MockInfo { + MockInfo() : ssl_cert(NULL) { } + MockInfo(std::wstring base, + std::vector<int> errors, + net::X509Certificate* ssl_cert) + : base(base), + errors(errors), + ssl_cert(ssl_cert) { } + + std::wstring base; + std::vector<int> errors; + scoped_refptr<net::X509Certificate> ssl_cert; +}; + +// static +void URLRequestMockNetErrorJob::AddMockedURL(const GURL& url, + const std::wstring& base, + const std::vector<int>& errors, + net::X509Certificate* ssl_cert) { +#ifndef NDEBUG + URLMockInfoMap::const_iterator iter = url_mock_info_map_.find(url); + DCHECK(iter == url_mock_info_map_.end()); +#endif + + url_mock_info_map_[url] = MockInfo(base, errors, ssl_cert); + net::URLRequestFilter::GetInstance() + ->AddUrlHandler(url, &URLRequestMockNetErrorJob::Factory); +} + +// static +void URLRequestMockNetErrorJob::RemoveMockedURL(const GURL& url) { + URLMockInfoMap::iterator iter = url_mock_info_map_.find(url); + DCHECK(iter != url_mock_info_map_.end()); + url_mock_info_map_.erase(iter); + net::URLRequestFilter::GetInstance()->RemoveUrlHandler(url); +} + +// static +net::URLRequestJob* URLRequestMockNetErrorJob::Factory( + net::URLRequest* request, + const std::string& scheme) { + GURL url = request->url(); + + URLMockInfoMap::const_iterator iter = url_mock_info_map_.find(url); + DCHECK(iter != url_mock_info_map_.end()); + + MockInfo mock_info = iter->second; + + // URLRequestMockNetErrorJob derives from net::URLRequestFileJob. We pass a + // FilePath so that the net::URLRequestFileJob methods will do the loading + // from the files. + std::wstring file_url(L"file:///"); + file_url.append(mock_info.base); + file_url.append(UTF8ToWide(url.path())); + // Convert the file:/// URL to a path on disk. + FilePath file_path; + net::FileURLToFilePath(GURL(WideToUTF8(file_url)), &file_path); + return new URLRequestMockNetErrorJob(request, mock_info.errors, + mock_info.ssl_cert, + file_path); +} + +URLRequestMockNetErrorJob::URLRequestMockNetErrorJob(net::URLRequest* request, + const std::vector<int>& errors, net::X509Certificate* cert, + const FilePath& file_path) + : URLRequestMockHTTPJob(request, file_path), + errors_(errors), + ssl_cert_(cert), + ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { +} + +URLRequestMockNetErrorJob::~URLRequestMockNetErrorJob() { +} + +void URLRequestMockNetErrorJob::Start() { + MessageLoop::current()->PostTask( + FROM_HERE, + method_factory_.NewRunnableMethod( + &URLRequestMockNetErrorJob::StartAsync)); +} + +void URLRequestMockNetErrorJob::StartAsync() { + if (errors_.empty()) { + URLRequestMockHTTPJob::Start(); + } else { + int error = errors_[0]; + errors_.erase(errors_.begin()); + + if (net::IsCertificateError(error)) { + DCHECK(ssl_cert_); + request_->delegate()->OnSSLCertificateError(request_, error, + ssl_cert_.get()); + } else { + NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, + error)); + } + } +} + +void URLRequestMockNetErrorJob::ContinueDespiteLastError() { + Start(); +} diff --git a/content/browser/net/url_request_mock_net_error_job.h b/content/browser/net/url_request_mock_net_error_job.h new file mode 100644 index 0000000..e0ba914 --- /dev/null +++ b/content/browser/net/url_request_mock_net_error_job.h @@ -0,0 +1,62 @@ +// Copyright (c) 2011 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. + +// A net::URLRequestJob class that simulates network errors (including https +// related). +// It is based on URLRequestMockHttpJob. + +#ifndef CONTENT_BROWSER_NET_URL_REQUEST_MOCK_NET_ERROR_JOB_H_ +#define CONTENT_BROWSER_NET_URL_REQUEST_MOCK_NET_ERROR_JOB_H_ +#pragma once + +#include "base/task.h" +#include "content/browser/net/url_request_mock_http_job.h" + +class URLRequestMockNetErrorJob : public URLRequestMockHTTPJob { + public: + URLRequestMockNetErrorJob(net::URLRequest* request, + const std::vector<int>& errors, + net::X509Certificate* ssl_cert, + const FilePath& file_path); + + virtual void Start(); + virtual void ContinueDespiteLastError(); + + // Add the specified URL to the list of URLs that should be mocked. When this + // URL is hit, the specified |errors| will be played. If any of these errors + // is a cert error, |ssl_cert| will be used as the ssl cert when notifying of + // the error. |ssl_cert| can be NULL if |errors| does not contain any cert + // errors. |base| is the location on disk where the file mocking the URL + // contents and http-headers should be retrieved from. + static void AddMockedURL(const GURL& url, + const std::wstring& base, + const std::vector<int>& errors, + net::X509Certificate* ssl_cert); + + // Removes the specified |url| from the list of mocked urls. + static void RemoveMockedURL(const GURL& url); + + private: + virtual ~URLRequestMockNetErrorJob(); + + static net::URLRequest::ProtocolFactory Factory; + + void StartAsync(); + + // The errors to simulate. + std::vector<int> errors_; + + // The certificate to use for SSL errors. + scoped_refptr<net::X509Certificate> ssl_cert_; + + struct MockInfo; + typedef std::map<GURL, MockInfo> URLMockInfoMap; + static URLMockInfoMap url_mock_info_map_; + + ScopedRunnableMethodFactory<URLRequestMockNetErrorJob> method_factory_; + + DISALLOW_COPY_AND_ASSIGN(URLRequestMockNetErrorJob); +}; + +#endif // CONTENT_BROWSER_NET_URL_REQUEST_MOCK_NET_ERROR_JOB_H_ diff --git a/content/browser/net/url_request_slow_download_job.cc b/content/browser/net/url_request_slow_download_job.cc new file mode 100644 index 0000000..ef1370f --- /dev/null +++ b/content/browser/net/url_request_slow_download_job.cc @@ -0,0 +1,184 @@ +// Copyright (c) 2011 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 "content/browser/net/url_request_slow_download_job.h" + +#include "base/compiler_specific.h" +#include "base/message_loop.h" +#include "base/stringprintf.h" +#include "base/string_util.h" +#include "googleurl/src/gurl.h" +#include "net/base/io_buffer.h" +#include "net/http/http_response_headers.h" +#include "net/url_request/url_request.h" +#include "net/url_request/url_request_filter.h" + +const int kFirstDownloadSize = 1024 * 35; +const int kSecondDownloadSize = 1024 * 10; + +const char URLRequestSlowDownloadJob::kUnknownSizeUrl[] = + "http://url.handled.by.slow.download/download-unknown-size"; +const char URLRequestSlowDownloadJob::kKnownSizeUrl[] = + "http://url.handled.by.slow.download/download-known-size"; +const char URLRequestSlowDownloadJob::kFinishDownloadUrl[] = + "http://url.handled.by.slow.download/download-finish"; + +std::vector<URLRequestSlowDownloadJob*> + URLRequestSlowDownloadJob::kPendingRequests; + +void URLRequestSlowDownloadJob::Start() { + MessageLoop::current()->PostTask( + FROM_HERE, + method_factory_.NewRunnableMethod( + &URLRequestSlowDownloadJob::StartAsync)); +} + +// static +void URLRequestSlowDownloadJob::AddUrlHandler() { + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); + filter->AddUrlHandler(GURL(kUnknownSizeUrl), + &URLRequestSlowDownloadJob::Factory); + filter->AddUrlHandler(GURL(kKnownSizeUrl), + &URLRequestSlowDownloadJob::Factory); + filter->AddUrlHandler(GURL(kFinishDownloadUrl), + &URLRequestSlowDownloadJob::Factory); +} + +/*static */ +net::URLRequestJob* URLRequestSlowDownloadJob::Factory( + net::URLRequest* request, + const std::string& scheme) { + URLRequestSlowDownloadJob* job = new URLRequestSlowDownloadJob(request); + if (request->url().spec() != kFinishDownloadUrl) + URLRequestSlowDownloadJob::kPendingRequests.push_back(job); + return job; +} + +/* static */ +void URLRequestSlowDownloadJob::FinishPendingRequests() { + typedef std::vector<URLRequestSlowDownloadJob*> JobList; + for (JobList::iterator it = kPendingRequests.begin(); it != + kPendingRequests.end(); ++it) { + (*it)->set_should_finish_download(); + } + kPendingRequests.clear(); +} + +URLRequestSlowDownloadJob::URLRequestSlowDownloadJob(net::URLRequest* request) + : net::URLRequestJob(request), + first_download_size_remaining_(kFirstDownloadSize), + should_finish_download_(false), + should_send_second_chunk_(false), + ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} + +void URLRequestSlowDownloadJob::StartAsync() { + if (LowerCaseEqualsASCII(kFinishDownloadUrl, request_->url().spec().c_str())) + URLRequestSlowDownloadJob::FinishPendingRequests(); + + NotifyHeadersComplete(); +} + +bool URLRequestSlowDownloadJob::ReadRawData(net::IOBuffer* buf, int buf_size, + int *bytes_read) { + if (LowerCaseEqualsASCII(kFinishDownloadUrl, + request_->url().spec().c_str())) { + *bytes_read = 0; + return true; + } + + if (should_send_second_chunk_) { + DCHECK(buf_size > kSecondDownloadSize); + for (int i = 0; i < kSecondDownloadSize; ++i) { + buf->data()[i] = '*'; + } + *bytes_read = kSecondDownloadSize; + should_send_second_chunk_ = false; + return true; + } + + if (first_download_size_remaining_ > 0) { + int send_size = std::min(first_download_size_remaining_, buf_size); + for (int i = 0; i < send_size; ++i) { + buf->data()[i] = '*'; + } + *bytes_read = send_size; + first_download_size_remaining_ -= send_size; + + DCHECK(!is_done()); + return true; + } + + if (should_finish_download_) { + *bytes_read = 0; + return true; + } + + // If we make it here, the first chunk has been sent and we need to wait + // until a request is made for kFinishDownloadUrl. + SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); + MessageLoop::current()->PostDelayedTask( + FROM_HERE, + method_factory_.NewRunnableMethod( + &URLRequestSlowDownloadJob::CheckDoneStatus), + 100); + + // Return false to signal there is pending data. + return false; +} + +void URLRequestSlowDownloadJob::CheckDoneStatus() { + if (should_finish_download_) { + should_send_second_chunk_ = true; + SetStatus(net::URLRequestStatus()); + NotifyReadComplete(kSecondDownloadSize); + } else { + MessageLoop::current()->PostDelayedTask( + FROM_HERE, + method_factory_.NewRunnableMethod( + &URLRequestSlowDownloadJob::CheckDoneStatus), + 100); + } +} + +// Public virtual version. +void URLRequestSlowDownloadJob::GetResponseInfo(net::HttpResponseInfo* info) { + // Forward to private const version. + GetResponseInfoConst(info); +} + +URLRequestSlowDownloadJob::~URLRequestSlowDownloadJob() {} + +// Private const version. +void URLRequestSlowDownloadJob::GetResponseInfoConst( + net::HttpResponseInfo* info) const { + // Send back mock headers. + std::string raw_headers; + if (LowerCaseEqualsASCII(kFinishDownloadUrl, + request_->url().spec().c_str())) { + raw_headers.append( + "HTTP/1.1 200 OK\n" + "Content-type: text/plain\n"); + } else { + raw_headers.append( + "HTTP/1.1 200 OK\n" + "Content-type: application/octet-stream\n" + "Cache-Control: max-age=0\n"); + + if (LowerCaseEqualsASCII(kKnownSizeUrl, request_->url().spec().c_str())) { + raw_headers.append(base::StringPrintf( + "Content-Length: %d\n", + kFirstDownloadSize + kSecondDownloadSize)); + } + } + + // ParseRawHeaders expects \0 to end each header line. + ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); + info->headers = new net::HttpResponseHeaders(raw_headers); +} + +bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const { + net::HttpResponseInfo info; + GetResponseInfoConst(&info); + return info.headers && info.headers->GetMimeType(mime_type); +} diff --git a/content/browser/net/url_request_slow_download_job.h b/content/browser/net/url_request_slow_download_job.h new file mode 100644 index 0000000..cef2adc --- /dev/null +++ b/content/browser/net/url_request_slow_download_job.h @@ -0,0 +1,64 @@ +// 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. +// This class simulates a slow download. This used in a UI test to test the +// download manager. Requests to |kUnknownSizeUrl| and |kKnownSizeUrl| start +// downloads that pause after the first + +#ifndef CONTENT_BROWSER_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_ +#define CONTENT_BROWSER_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_ +#pragma once + +#include <string> +#include <vector> + +#include "base/task.h" +#include "net/url_request/url_request_job.h" + +class URLRequestSlowDownloadJob : public net::URLRequestJob { + public: + explicit URLRequestSlowDownloadJob(net::URLRequest* request); + + // Timer callback, used to check to see if we should finish our download and + // send the second chunk. + void CheckDoneStatus(); + + // net::URLRequestJob methods + virtual void Start(); + virtual bool GetMimeType(std::string* mime_type) const; + virtual void GetResponseInfo(net::HttpResponseInfo* info); + virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); + + static net::URLRequestJob* Factory(net::URLRequest* request, + const std::string& scheme); + + // Test URLs. + static const char kUnknownSizeUrl[]; + static const char kKnownSizeUrl[]; + static const char kFinishDownloadUrl[]; + + // Adds the testing URLs to the net::URLRequestFilter. + static void AddUrlHandler(); + + private: + virtual ~URLRequestSlowDownloadJob(); + + void GetResponseInfoConst(net::HttpResponseInfo* info) const; + + // Mark all pending requests to be finished. We keep track of pending + // requests in |kPendingRequests|. + static void FinishPendingRequests(); + static std::vector<URLRequestSlowDownloadJob*> kPendingRequests; + + void StartAsync(); + + void set_should_finish_download() { should_finish_download_ = true; } + + int first_download_size_remaining_; + bool should_finish_download_; + bool should_send_second_chunk_; + + ScopedRunnableMethodFactory<URLRequestSlowDownloadJob> method_factory_; +}; + +#endif // CONTENT_BROWSER_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_ diff --git a/content/browser/net/url_request_slow_http_job.cc b/content/browser/net/url_request_slow_http_job.cc new file mode 100644 index 0000000..398298f --- /dev/null +++ b/content/browser/net/url_request_slow_http_job.cc @@ -0,0 +1,62 @@ +// 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 "content/browser/net/url_request_slow_http_job.h" + +#include "base/time.h" +#include "base/utf_string_conversions.h" +#include "net/url_request/url_request_filter.h" + +static const char kMockHostname[] = "mock.slow.http"; + +FilePath URLRequestSlowHTTPJob::base_path_; + +// static +const int URLRequestSlowHTTPJob::kDelayMs = 1000; + +using base::TimeDelta; + +/* static */ +net::URLRequestJob* URLRequestSlowHTTPJob::Factory(net::URLRequest* request, + const std::string& scheme) { + return new URLRequestSlowHTTPJob(request, + GetOnDiskPath(base_path_, request, scheme)); +} + +/* static */ +void URLRequestSlowHTTPJob::AddUrlHandler(const FilePath& base_path) { + base_path_ = base_path; + + // Add kMockHostname to net::URLRequestFilter. + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); + filter->AddHostnameHandler("http", kMockHostname, + URLRequestSlowHTTPJob::Factory); +} + +/* static */ +GURL URLRequestSlowHTTPJob::GetMockUrl(const FilePath& path) { + std::string url = "http://"; + url.append(kMockHostname); + url.append("/"); + std::string path_str = path.MaybeAsASCII(); + DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. + url.append(path_str); + return GURL(url); +} + +URLRequestSlowHTTPJob::URLRequestSlowHTTPJob(net::URLRequest* request, + const FilePath& file_path) + : URLRequestMockHTTPJob(request, file_path) { } + +void URLRequestSlowHTTPJob::Start() { + delay_timer_.Start(TimeDelta::FromMilliseconds(kDelayMs), this, + &URLRequestSlowHTTPJob::RealStart); +} + +URLRequestSlowHTTPJob::~URLRequestSlowHTTPJob() { +} + +void URLRequestSlowHTTPJob::RealStart() { + URLRequestMockHTTPJob::Start(); +} diff --git a/content/browser/net/url_request_slow_http_job.h b/content/browser/net/url_request_slow_http_job.h new file mode 100644 index 0000000..cc0458b --- /dev/null +++ b/content/browser/net/url_request_slow_http_job.h @@ -0,0 +1,42 @@ +// 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. +// +// A URLRequestMockHTTPJob class that inserts a time delay in processing. + +#ifndef CONTENT_BROWSER_NET_URL_REQUEST_SLOW_HTTP_JOB_H_ +#define CONTENT_BROWSER_NET_URL_REQUEST_SLOW_HTTP_JOB_H_ +#pragma once + +#include "base/timer.h" +#include "content/browser/net/url_request_mock_http_job.h" + +class URLRequestSlowHTTPJob : public URLRequestMockHTTPJob { + public: + URLRequestSlowHTTPJob(net::URLRequest* request, const FilePath& file_path); + + static const int kDelayMs; + + static net::URLRequest::ProtocolFactory Factory; + + // Adds the testing URLs to the net::URLRequestFilter. + static void AddUrlHandler(const FilePath& base_path); + + // Given the path to a file relative to base_path_, construct a mock URL. + static GURL GetMockUrl(const FilePath& path); + + virtual void Start(); + + private: + virtual ~URLRequestSlowHTTPJob(); + + void RealStart(); + + base::OneShotTimer<URLRequestSlowHTTPJob> delay_timer_; + + // This is the file path leading to the root of the directory to use as the + // root of the http server. + static FilePath base_path_; +}; + +#endif // CONTENT_BROWSER_NET_URL_REQUEST_SLOW_HTTP_JOB_H_ diff --git a/content/browser/renderer_host/clipboard_message_filter_mac.mm b/content/browser/renderer_host/clipboard_message_filter_mac.mm index 72cfc53..829a227 100644 --- a/content/browser/renderer_host/clipboard_message_filter_mac.mm +++ b/content/browser/renderer_host/clipboard_message_filter_mac.mm @@ -7,8 +7,8 @@ #import <Cocoa/Cocoa.h> #include "base/sys_string_conversions.h" -#import "chrome/browser/ui/cocoa/find_pasteboard.h" #include "content/browser/browser_thread.h" +#import "content/browser/find_pasteboard.h" // The number of utf16 code units that will be written to the find pasteboard, // longer texts are silently ignored. This is to prevent that a compromised diff --git a/content/browser/renderer_host/resource_dispatcher_host_uitest.cc b/content/browser/renderer_host/resource_dispatcher_host_uitest.cc index 25dcc0f..2e2e15f 100644 --- a/content/browser/renderer_host/resource_dispatcher_host_uitest.cc +++ b/content/browser/renderer_host/resource_dispatcher_host_uitest.cc @@ -10,11 +10,11 @@ #include "base/path_service.h" #include "base/string_util.h" #include "base/test/test_timeouts.h" -#include "chrome/browser/net/url_request_failed_dns_job.h" -#include "chrome/browser/net/url_request_mock_http_job.h" #include "chrome/test/automation/browser_proxy.h" #include "chrome/test/automation/tab_proxy.h" #include "chrome/test/ui/ui_test.h" +#include "content/browser/net/url_request_failed_dns_job.h" +#include "content/browser/net/url_request_mock_http_job.h" #include "content/common/test_url_constants.h" #include "content/common/url_constants.h" #include "net/base/net_util.h" diff --git a/content/content_browser.gypi b/content/content_browser.gypi index 5246fdd..ce212c9 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -73,6 +73,8 @@ 'browser/file_system/browser_file_system_helper.h', 'browser/file_system/file_system_dispatcher_host.cc', 'browser/file_system/file_system_dispatcher_host.h', + 'browser/find_pasteboard.h', + 'browser/find_pasteboard.mm', 'browser/font_list_async.cc', 'browser/font_list_async.h', 'browser/geolocation/access_token_store.h', @@ -180,6 +182,16 @@ 'browser/media_stream/video_capture_manager.h', 'browser/mime_registry_message_filter.cc', 'browser/mime_registry_message_filter.h', + # TODO: These should be moved to test_support (see below), but + # are currently used by production code in automation_provider.cc. + 'browser/net/url_request_failed_dns_job.cc', + 'browser/net/url_request_failed_dns_job.h', + 'browser/net/url_request_mock_http_job.cc', + 'browser/net/url_request_mock_http_job.h', + 'browser/net/url_request_slow_download_job.cc', + 'browser/net/url_request_slow_download_job.h', + 'browser/net/url_request_slow_http_job.cc', + 'browser/net/url_request_slow_http_job.h', 'browser/ppapi_plugin_process_host.cc', 'browser/ppapi_plugin_process_host.h', 'browser/ppapi_broker_process_host.cc', |