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