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

Side by Side Diff: chrome/browser/extensions/api/streams_private/streams_resource_throttle.cc

Issue 12381035: Move Mime type handling to streamsPrivate API, so that it works on Desktop Chrome. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Initialize test variable Created 7 years, 9 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/extensions/file_browser_resource_throttle.h" 5 #include "chrome/browser/extensions/api/streams_private/streams_resource_throttl e.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/chromeos/extensions/file_browser_handler.h"
12 #include "chrome/browser/extensions/event_router.h" 11 #include "chrome/browser/extensions/event_router.h"
13 #include "chrome/browser/extensions/extension_info_map.h" 12 #include "chrome/browser/extensions/extension_info_map.h"
14 #include "chrome/browser/extensions/extension_system.h" 13 #include "chrome/browser/extensions/extension_system.h"
15 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/extensions/extension.h" 15 #include "chrome/common/extensions/extension.h"
17 #include "chrome/common/extensions/extension_constants.h" 16 #include "chrome/common/extensions/extension_constants.h"
18 #include "chrome/common/extensions/extension_set.h" 17 #include "chrome/common/extensions/extension_set.h"
18 #include "chrome/common/extensions/mime_types_handler.h"
19 #include "content/public/browser/browser_context.h" 19 #include "content/public/browser/browser_context.h"
20 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/render_view_host.h" 21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/resource_controller.h" 22 #include "content/public/browser/resource_controller.h"
23 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
24 #include "net/url_request/url_request.h" 24 #include "net/url_request/url_request.h"
25 25
26 using extensions::Event; 26 using extensions::Event;
27 using extensions::Extension; 27 using extensions::Extension;
28 using extensions::ExtensionSystem; 28 using extensions::ExtensionSystem;
29 29
30 namespace { 30 namespace {
31 31
32 const char* const kOnExecuteContentHandlerEvent = 32 const char* const kOnExecuteMimeTypeHandlerEvent =
33 "fileBrowserHandler.onExecuteContentHandler"; 33 "streamsPrivate.onExecuteMimeTypeHandler";
34 34
35 // Goes through the extension's file browser handlers and checks it there is one 35 // Goes through the extension's mime type handlers and checks it there is one
36 // that can handle the |mime_type|. 36 // that can handle the |mime_type|.
37 // |extension| must not be NULL. 37 // |extension| must not be NULL.
38 bool CanHandleMimeType(const Extension* extension, 38 bool CanHandleMimeType(const Extension* extension,
39 const std::string& mime_type) { 39 const std::string& mime_type) {
40 FileBrowserHandler::List* handlers = 40 MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
41 FileBrowserHandler::GetHandlers(extension); 41 if (!handler)
42 if (!handlers)
43 return false; 42 return false;
44 43
45 for (FileBrowserHandler::List::const_iterator handler = handlers->begin(); 44 return handler->CanHandleMIMEType(mime_type);
46 handler != handlers->end();
47 ++handler) {
48 if ((*handler)->CanHandleMIMEType(mime_type)) {
49 return true;
50 }
51 }
52
53 return false;
54 } 45 }
55 46
56 // Retrieves Profile for a render view host specified by |render_process_id| and 47 // Retrieves Profile for a render view host specified by |render_process_id| and
57 // |render_view_id|. 48 // |render_view_id|.
58 Profile* GetProfile(int render_process_id, int render_view_id) { 49 Profile* GetProfile(int render_process_id, int render_view_id) {
59 content::RenderViewHost* render_view_host = 50 content::RenderViewHost* render_view_host =
60 content::RenderViewHost::FromID(render_process_id, render_view_id); 51 content::RenderViewHost::FromID(render_process_id, render_view_id);
61 if (!render_view_host) 52 if (!render_view_host)
62 return NULL; 53 return NULL;
63 54
(...skipping 18 matching lines...) Expand all
82 73
83 Profile* profile = GetProfile(render_process_id, render_view_id); 74 Profile* profile = GetProfile(render_process_id, render_view_id);
84 if (!profile) 75 if (!profile)
85 return; 76 return;
86 77
87 // Create the event's arguments value. 78 // Create the event's arguments value.
88 scoped_ptr<ListValue> event_args(new ListValue()); 79 scoped_ptr<ListValue> event_args(new ListValue());
89 event_args->Append(new base::StringValue(mime_type)); 80 event_args->Append(new base::StringValue(mime_type));
90 event_args->Append(new base::StringValue(request_url.spec())); 81 event_args->Append(new base::StringValue(request_url.spec()));
91 82
92 scoped_ptr<Event> event(new Event(kOnExecuteContentHandlerEvent, 83 scoped_ptr<Event> event(new Event(kOnExecuteMimeTypeHandlerEvent,
93 event_args.Pass())); 84 event_args.Pass()));
94 85
95 ExtensionSystem::Get(profile)->event_router()->DispatchEventToExtension( 86 ExtensionSystem::Get(profile)->event_router()->DispatchEventToExtension(
96 extension_id, event.Pass()); 87 extension_id, event.Pass());
97 } 88 }
98 89
99 // Default implementation of FileBrowserHandlerEventRouter. 90 // Default implementation of StreamsPrivateEventRouter.
100 class FileBrowserHandlerEventRouterImpl 91 class StreamsPrivateEventRouterImpl
101 : public FileBrowserResourceThrottle::FileBrowserHandlerEventRouter { 92 : public StreamsResourceThrottle::StreamsPrivateEventRouter {
102 public: 93 public:
103 FileBrowserHandlerEventRouterImpl() {} 94 StreamsPrivateEventRouterImpl() {}
104 virtual ~FileBrowserHandlerEventRouterImpl() {} 95 virtual ~StreamsPrivateEventRouterImpl() {}
105 96
106 virtual void DispatchMimeTypeHandlerEvent( 97 virtual void DispatchMimeTypeHandlerEvent(
107 int render_process_id, 98 int render_process_id,
108 int render_view_id, 99 int render_view_id,
109 const std::string& mime_type, 100 const std::string& mime_type,
110 const GURL& request_url, 101 const GURL& request_url,
111 const std::string& extension_id) OVERRIDE { 102 const std::string& extension_id) OVERRIDE {
112 // The event must be dispatched on the UI thread. 103 // The event must be dispatched on the UI thread.
113 content::BrowserThread::PostTask( 104 content::BrowserThread::PostTask(
114 content::BrowserThread::UI, FROM_HERE, 105 content::BrowserThread::UI, FROM_HERE,
115 base::Bind(&DispatchEventOnUIThread, mime_type, request_url, 106 base::Bind(&DispatchEventOnUIThread, mime_type, request_url,
116 render_process_id, render_view_id, extension_id)); 107 render_process_id, render_view_id, extension_id));
117 } 108 }
118 }; 109 };
119 110
120 } // namespace 111 } // namespace
121 112
122 // static 113 // static
123 FileBrowserResourceThrottle* FileBrowserResourceThrottle::Create( 114 StreamsResourceThrottle* StreamsResourceThrottle::Create(
124 int render_process_id, 115 int render_process_id,
125 int render_view_id, 116 int render_view_id,
126 net::URLRequest* request, 117 net::URLRequest* request,
127 bool profile_is_incognito, 118 bool profile_is_incognito,
128 const ExtensionInfoMap* extension_info_map) { 119 const ExtensionInfoMap* extension_info_map) {
129 std::string mime_type; 120 std::string mime_type;
130 request->GetMimeType(&mime_type); 121 request->GetMimeType(&mime_type);
131 scoped_ptr<FileBrowserHandlerEventRouter> event_router( 122 scoped_ptr<StreamsPrivateEventRouter> event_router(
132 new FileBrowserHandlerEventRouterImpl()); 123 new StreamsPrivateEventRouterImpl());
133 return new FileBrowserResourceThrottle(render_process_id, render_view_id, 124 return new StreamsResourceThrottle(render_process_id, render_view_id,
134 mime_type, request->url(), profile_is_incognito, extension_info_map, 125 mime_type, request->url(), profile_is_incognito, extension_info_map,
135 event_router.Pass()); 126 event_router.Pass());
136 } 127 }
137 128
138 // static 129 // static
139 FileBrowserResourceThrottle* FileBrowserResourceThrottle::CreateForTest( 130 StreamsResourceThrottle* StreamsResourceThrottle::CreateForTest(
140 int render_process_id, 131 int render_process_id,
141 int render_view_id, 132 int render_view_id,
142 const std::string& mime_type, 133 const std::string& mime_type,
143 const GURL& request_url, 134 const GURL& request_url,
144 bool profile_is_incognito, 135 bool profile_is_incognito,
145 const ExtensionInfoMap* extension_info_map, 136 const ExtensionInfoMap* extension_info_map,
146 scoped_ptr<FileBrowserHandlerEventRouter> event_router_in) { 137 scoped_ptr<StreamsPrivateEventRouter> event_router_in) {
147 scoped_ptr<FileBrowserHandlerEventRouter> event_router = 138 scoped_ptr<StreamsPrivateEventRouter> event_router =
148 event_router_in.Pass(); 139 event_router_in.Pass();
149 if (!event_router) 140 if (!event_router)
150 event_router.reset(new FileBrowserHandlerEventRouterImpl()); 141 event_router.reset(new StreamsPrivateEventRouterImpl());
151 return new FileBrowserResourceThrottle(render_process_id, render_view_id, 142 return new StreamsResourceThrottle(render_process_id, render_view_id,
152 mime_type, request_url, profile_is_incognito, extension_info_map, 143 mime_type, request_url, profile_is_incognito, extension_info_map,
153 event_router.Pass()); 144 event_router.Pass());
154 } 145 }
155 146
156 FileBrowserResourceThrottle::FileBrowserResourceThrottle( 147 StreamsResourceThrottle::StreamsResourceThrottle(
157 int render_process_id, 148 int render_process_id,
158 int render_view_id, 149 int render_view_id,
159 const std::string& mime_type, 150 const std::string& mime_type,
160 const GURL& request_url, 151 const GURL& request_url,
161 bool profile_is_incognito, 152 bool profile_is_incognito,
162 const ExtensionInfoMap* extension_info_map, 153 const ExtensionInfoMap* extension_info_map,
163 scoped_ptr<FileBrowserHandlerEventRouter> event_router) 154 scoped_ptr<StreamsPrivateEventRouter> event_router)
164 : render_process_id_(render_process_id), 155 : render_process_id_(render_process_id),
165 render_view_id_(render_view_id), 156 render_view_id_(render_view_id),
166 mime_type_(mime_type), 157 mime_type_(mime_type),
167 request_url_(request_url), 158 request_url_(request_url),
168 profile_is_incognito_(profile_is_incognito), 159 profile_is_incognito_(profile_is_incognito),
169 extension_info_map_(extension_info_map), 160 extension_info_map_(extension_info_map),
170 event_router_(event_router.Pass()) { 161 event_router_(event_router.Pass()) {
171 } 162 }
172 163
173 FileBrowserResourceThrottle::~FileBrowserResourceThrottle() {} 164 StreamsResourceThrottle::~StreamsResourceThrottle() {}
174 165
175 void FileBrowserResourceThrottle::WillProcessResponse(bool* defer) { 166 void StreamsResourceThrottle::WillProcessResponse(bool* defer) {
176 std::vector<std::string> whitelist = 167 std::vector<std::string> whitelist = MimeTypesHandler::GetMIMETypeWhitelist();
177 FileBrowserHandler::GetMIMETypeWhitelist();
178 // Go through the white-listed extensions and try to use them to intercept 168 // Go through the white-listed extensions and try to use them to intercept
179 // the URL request. 169 // the URL request.
180 for (size_t i = 0; i < whitelist.size(); ++i) { 170 for (size_t i = 0; i < whitelist.size(); ++i) {
181 if (MaybeInterceptWithExtension(whitelist[i])) 171 if (MaybeInterceptWithExtension(whitelist[i]))
182 return; 172 return;
183 } 173 }
184 } 174 }
185 175
186 bool FileBrowserResourceThrottle::MaybeInterceptWithExtension( 176 bool StreamsResourceThrottle::MaybeInterceptWithExtension(
187 const std::string& extension_id) { 177 const std::string& extension_id) {
188 const Extension* extension = 178 const Extension* extension =
189 extension_info_map_->extensions().GetByID(extension_id); 179 extension_info_map_->extensions().GetByID(extension_id);
190 // The white-listed extension may not be installed, so we have to NULL check 180 // The white-listed extension may not be installed, so we have to NULL check
191 // |extension|. 181 // |extension|.
192 if (!extension) 182 if (!extension)
193 return false; 183 return false;
194 184
195 // If in incognito mode, skip the extensions that are not incognito enabled. 185 // If in incognito mode, skip the extensions that are not incognito enabled.
196 if (profile_is_incognito_ && 186 if (profile_is_incognito_ &&
197 !extension_info_map_->IsIncognitoEnabled(extension_id)) { 187 !extension_info_map_->IsIncognitoEnabled(extension_id)) {
198 return false; 188 return false;
199 } 189 }
200 190
201 if (CanHandleMimeType(extension, mime_type_)) { 191 if (CanHandleMimeType(extension, mime_type_)) {
202 event_router_->DispatchMimeTypeHandlerEvent(render_process_id_, 192 event_router_->DispatchMimeTypeHandlerEvent(render_process_id_,
203 render_view_id_, mime_type_, request_url_, extension->id()); 193 render_view_id_, mime_type_, request_url_, extension->id());
204 controller()->CancelAndIgnore(); 194 controller()->CancelAndIgnore();
205 return true; 195 return true;
206 } 196 }
207 return false; 197 return false;
208 } 198 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698