OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/chromeos/gview_request_interceptor.h" | 5 #include "chrome/browser/chromeos/gview_request_interceptor.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/memory/singleton.h" |
8 #include "base/path_service.h" | 9 #include "base/path_service.h" |
9 #include "chrome/common/chrome_paths.h" | 10 #include "chrome/common/chrome_paths.h" |
10 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
11 #include "net/base/escape.h" | 12 #include "net/base/escape.h" |
12 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
13 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
| 15 #include "net/url_request/url_request_job.h" |
14 #include "net/url_request/url_request_redirect_job.h" | 16 #include "net/url_request/url_request_redirect_job.h" |
15 #include "webkit/glue/plugins/plugin_list.h" | 17 #include "webkit/glue/plugins/plugin_list.h" |
16 | 18 |
17 namespace chromeos { | 19 namespace chromeos { |
18 | 20 |
19 // The PDF mime type is treated special if the browser has a built-in | 21 // The PDF mime type is treated special if the browser has a built-in |
20 // PDF viewer plug-in installed - we want to intercept only if we're | 22 // PDF viewer plug-in installed - we want to intercept only if we're |
21 // told to. | 23 // told to. |
22 static const char kPdfMimeType[] = "application/pdf"; | 24 static const char* const kPdfMimeType = "application/pdf"; |
23 | 25 |
24 // This is the list of mime types currently supported by the Google | 26 // This is the list of mime types currently supported by the Google |
25 // Document Viewer. | 27 // Document Viewer. |
26 static const char* const kSupportedMimeTypeList[] = { | 28 static const char* const supported_mime_type_list[] = { |
27 kPdfMimeType, | 29 kPdfMimeType, |
28 "application/vnd.ms-powerpoint" | 30 "application/vnd.ms-powerpoint" |
29 }; | 31 }; |
30 | 32 |
31 static const char kGViewUrlPrefix[] = "http://docs.google.com/gview?url="; | 33 static const char* const kGViewUrlPrefix = "http://docs.google.com/gview?url="; |
32 | 34 |
33 GViewRequestInterceptor::GViewRequestInterceptor() { | 35 GViewRequestInterceptor::GViewRequestInterceptor() { |
34 for (size_t i = 0; i < arraysize(kSupportedMimeTypeList); ++i) { | 36 net::URLRequest::RegisterRequestInterceptor(this); |
35 supported_mime_types_.insert(kSupportedMimeTypeList[i]); | 37 for (size_t i = 0; i < arraysize(supported_mime_type_list); ++i) { |
| 38 supported_mime_types_.insert(supported_mime_type_list[i]); |
36 } | 39 } |
37 } | 40 } |
38 | 41 |
39 GViewRequestInterceptor::~GViewRequestInterceptor() { | 42 GViewRequestInterceptor::~GViewRequestInterceptor() { |
| 43 net::URLRequest::UnregisterRequestInterceptor(this); |
40 } | 44 } |
41 | 45 |
42 net::URLRequestJob* GViewRequestInterceptor::MaybeIntercept( | 46 net::URLRequestJob* GViewRequestInterceptor::MaybeIntercept( |
43 net::URLRequest* request) const { | 47 net::URLRequest* request) { |
44 // Don't attempt to intercept here as we want to wait until the mime | 48 // Don't attempt to intercept here as we want to wait until the mime |
45 // type is fully determined. | 49 // type is fully determined. |
46 return NULL; | 50 return NULL; |
47 } | 51 } |
48 | 52 |
49 net::URLRequestJob* GViewRequestInterceptor::MaybeInterceptRedirect( | |
50 const GURL& location, | |
51 net::URLRequest* request) const { | |
52 return NULL; | |
53 } | |
54 | |
55 net::URLRequestJob* GViewRequestInterceptor::MaybeInterceptResponse( | 53 net::URLRequestJob* GViewRequestInterceptor::MaybeInterceptResponse( |
56 net::URLRequest* request) const { | 54 net::URLRequest* request) { |
57 // Do not intercept this request if it is a download. | 55 // Do not intercept this request if it is a download. |
58 if (request->load_flags() & net::LOAD_IS_DOWNLOAD) { | 56 if (request->load_flags() & net::LOAD_IS_DOWNLOAD) { |
59 return NULL; | 57 return NULL; |
60 } | 58 } |
61 | 59 |
62 std::string mime_type; | 60 std::string mime_type; |
63 request->GetMimeType(&mime_type); | 61 request->GetMimeType(&mime_type); |
64 // If the local PDF viewing plug-in is installed and enabled, don't | 62 // If the local PDF viewing plug-in is installed and enabled, don't |
65 // redirect PDF files to Google Document Viewer. | 63 // redirect PDF files to Google Document Viewer. |
66 if (mime_type == kPdfMimeType) { | 64 if (mime_type == kPdfMimeType) { |
67 FilePath pdf_path; | 65 FilePath pdf_path; |
68 webkit::npapi::WebPluginInfo info; | 66 webkit::npapi::WebPluginInfo info; |
69 PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path); | 67 PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path); |
70 if (webkit::npapi::PluginList::Singleton()->GetPluginInfoByPath( | 68 if (webkit::npapi::PluginList::Singleton()->GetPluginInfoByPath( |
71 pdf_path, &info) && | 69 pdf_path, &info) && |
72 webkit::npapi::IsPluginEnabled(info)) | 70 webkit::npapi::IsPluginEnabled(info)) |
73 return NULL; | 71 return NULL; |
74 } | 72 } |
75 // If supported, build the URL to the Google Document Viewer | 73 // If supported, build the URL to the Google Document Viewer |
76 // including the origial document's URL, then create a new job that | 74 // including the origial document's URL, then create a new job that |
77 // will redirect the browser to this new URL. | 75 // will redirect the browser to this new URL. |
78 if (supported_mime_types_.count(mime_type) > 0) { | 76 if (supported_mime_types_.count(mime_type) > 0) { |
79 std::string url(kGViewUrlPrefix); | 77 std::string url(kGViewUrlPrefix); |
80 url += EscapePath(request->url().spec()); | 78 url += EscapePath(request->url().spec()); |
81 return new net::URLRequestRedirectJob(request, GURL(url)); | 79 return new net::URLRequestRedirectJob(request, GURL(url)); |
82 } | 80 } |
83 return NULL; | 81 return NULL; |
84 } | 82 } |
85 | 83 |
| 84 GViewRequestInterceptor* GViewRequestInterceptor::GetInstance() { |
| 85 return Singleton<GViewRequestInterceptor>::get(); |
| 86 } |
| 87 |
86 } // namespace chromeos | 88 } // namespace chromeos |
OLD | NEW |