OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/gview_request_interceptor.h" | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/path_service.h" | |
9 #include "chrome/browser/chrome_plugin_service_filter.h" | |
10 #include "chrome/common/chrome_paths.h" | |
11 #include "chrome/common/url_constants.h" | |
12 #include "content/public/browser/plugin_service.h" | |
13 #include "content/public/browser/resource_request_info.h" | |
14 #include "googleurl/src/gurl.h" | |
15 #include "net/base/escape.h" | |
16 #include "net/base/load_flags.h" | |
17 #include "net/url_request/url_request.h" | |
18 #include "net/url_request/url_request_redirect_job.h" | |
19 #include "webkit/plugins/webplugininfo.h" | |
20 | |
21 using content::PluginService; | |
22 using content::ResourceRequestInfo; | |
23 | |
24 namespace chromeos { | |
25 | |
26 namespace { | |
27 | |
28 // The PDF mime type is treated special if the browser has a built-in | |
29 // PDF viewer plug-in installed - we want to intercept only if we're | |
30 // told to. | |
31 const char kPdfMimeType[] = "application/pdf"; | |
32 | |
33 // This is the list of mime types currently supported by the Google | |
34 // Document Viewer. | |
35 const char* const kSupportedMimeTypeList[] = { | |
36 kPdfMimeType, | |
37 "application/vnd.ms-powerpoint" | |
38 }; | |
39 | |
40 const char kGViewUrlPrefix[] = "http://docs.google.com/gview?url="; | |
41 | |
42 } // namespace | |
43 | |
44 GViewRequestInterceptor::GViewRequestInterceptor() { | |
45 for (size_t i = 0; i < arraysize(kSupportedMimeTypeList); ++i) { | |
46 supported_mime_types_.insert(kSupportedMimeTypeList[i]); | |
47 } | |
48 } | |
49 | |
50 GViewRequestInterceptor::~GViewRequestInterceptor() { | |
51 } | |
52 | |
53 net::URLRequestJob* GViewRequestInterceptor::MaybeIntercept( | |
54 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | |
55 // Don't attempt to intercept here as we want to wait until the mime | |
56 // type is fully determined. | |
57 return NULL; | |
58 } | |
59 | |
60 net::URLRequestJob* GViewRequestInterceptor::MaybeInterceptRedirect( | |
61 const GURL& location, | |
62 net::URLRequest* request, | |
63 net::NetworkDelegate* network_delegate) const { | |
64 return NULL; | |
65 } | |
66 | |
67 bool GViewRequestInterceptor::ShouldUsePdfPlugin( | |
68 net::URLRequest* request) const { | |
69 FilePath pdf_path; | |
70 PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path); | |
71 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | |
72 if (!info) | |
73 return false; | |
74 | |
75 webkit::WebPluginInfo plugin; | |
76 if (!PluginService::GetInstance()->GetPluginInfoByPath(pdf_path, &plugin)) { | |
77 return false; | |
78 } | |
79 | |
80 return ChromePluginServiceFilter::GetInstance()->ShouldUsePlugin( | |
81 info->GetChildID(), info->GetRouteID(), info->GetContext(), | |
82 request->url(), GURL(), &plugin); | |
83 } | |
84 | |
85 bool GViewRequestInterceptor::ShouldInterceptScheme( | |
86 const std::string& scheme) const { | |
87 return scheme == chrome::kHttpScheme; | |
88 } | |
89 | |
90 net::URLRequestJob* GViewRequestInterceptor::MaybeInterceptResponse( | |
91 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | |
92 // Do not intercept this request if it is a download. | |
93 if (request->load_flags() & net::LOAD_IS_DOWNLOAD) { | |
94 return NULL; | |
95 } | |
96 | |
97 std::string mime_type; | |
98 request->GetMimeType(&mime_type); | |
99 | |
100 if (request->method() != "GET" || | |
101 !ShouldInterceptScheme(request->original_url().scheme())) { | |
102 return NULL; | |
103 } | |
104 | |
105 // If the local PDF viewing plug-in is installed and enabled, don't | |
106 // redirect PDF files to Google Document Viewer. | |
107 if (mime_type == kPdfMimeType && ShouldUsePdfPlugin(request)) | |
108 return NULL; | |
109 | |
110 // If supported, build the URL to the Google Document Viewer | |
111 // including the origial document's URL, then create a new job that | |
112 // will redirect the browser to this new URL. | |
113 if (supported_mime_types_.count(mime_type) > 0) { | |
114 std::string url(kGViewUrlPrefix); | |
115 url += net::EscapePath(request->url().spec()); | |
116 return new net::URLRequestRedirectJob( | |
117 request, network_delegate, GURL(url), | |
118 net::URLRequestRedirectJob::REDIRECT_302_FOUND); | |
119 } | |
120 return NULL; | |
121 } | |
122 | |
123 } // namespace chromeos | |
OLD | NEW |