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

Side by Side Diff: chrome/browser/web_resource/web_resource_service.cc

Issue 9317074: Create an API around UtilityProcessHost and use that from chrome. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 8 years, 10 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 | Annotate | Revision Log
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/web_resource/web_resource_service.h" 5 #include "chrome/browser/web_resource/web_resource_service.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/time.h" 12 #include "base/time.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/browser/browser_process.h" 15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/prefs/pref_service.h" 16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/chrome_utility_messages.h" 18 #include "chrome/common/chrome_utility_messages.h"
19 #include "chrome/common/web_resource/web_resource_unpacker.h" 19 #include "chrome/common/web_resource/web_resource_unpacker.h"
20 #include "content/browser/renderer_host/resource_dispatcher_host.h" 20 #include "content/browser/renderer_host/resource_dispatcher_host.h"
21 #include "content/browser/utility_process_host.h"
22 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/utility_process_host.h"
23 #include "content/public/browser/utility_process_host_client.h"
23 #include "content/public/common/url_fetcher.h" 24 #include "content/public/common/url_fetcher.h"
24 #include "googleurl/src/gurl.h" 25 #include "googleurl/src/gurl.h"
25 #include "net/base/load_flags.h" 26 #include "net/base/load_flags.h"
26 #include "net/url_request/url_request_status.h" 27 #include "net/url_request/url_request_status.h"
27 28
28 using content::BrowserThread; 29 using content::BrowserThread;
30 using content::UtilityProcessHost;
31 using content::UtilityProcessHostClient;
29 32
30 // This class coordinates a web resource unpack and parse task which is run in 33 // This class coordinates a web resource unpack and parse task which is run in
31 // a separate process. Results are sent back to this class and routed to 34 // a separate process. Results are sent back to this class and routed to
32 // the WebResourceService. 35 // the WebResourceService.
33 class WebResourceService::UnpackerClient : public UtilityProcessHost::Client { 36 class WebResourceService::UnpackerClient : public UtilityProcessHostClient {
34 public: 37 public:
35 explicit UnpackerClient(WebResourceService* web_resource_service) 38 explicit UnpackerClient(WebResourceService* web_resource_service)
36 : web_resource_service_(web_resource_service), 39 : web_resource_service_(web_resource_service),
37 resource_dispatcher_host_(ResourceDispatcherHost::Get()), 40 resource_dispatcher_host_(ResourceDispatcherHost::Get()),
38 got_response_(false) { 41 got_response_(false) {
39 } 42 }
40 43
41 void Start(const std::string& json_data) { 44 void Start(const std::string& json_data) {
42 AddRef(); // balanced in Cleanup. 45 AddRef(); // balanced in Cleanup.
43 46
(...skipping 17 matching lines...) Expand all
61 OnUnpackWebResourceSucceeded(*unpacker.parsed_json()); 64 OnUnpackWebResourceSucceeded(*unpacker.parsed_json());
62 } else { 65 } else {
63 OnUnpackWebResourceFailed(unpacker.error_message()); 66 OnUnpackWebResourceFailed(unpacker.error_message());
64 } 67 }
65 } 68 }
66 } 69 }
67 70
68 private: 71 private:
69 virtual ~UnpackerClient() {} 72 virtual ~UnpackerClient() {}
70 73
71 // UtilityProcessHost::Client 74 // UtilityProcessHostClient
72 virtual bool OnMessageReceived(const IPC::Message& message) { 75 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
73 bool handled = true; 76 bool handled = true;
74 IPC_BEGIN_MESSAGE_MAP(WebResourceService::UnpackerClient, message) 77 IPC_BEGIN_MESSAGE_MAP(WebResourceService::UnpackerClient, message)
75 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnpackWebResource_Succeeded, 78 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnpackWebResource_Succeeded,
76 OnUnpackWebResourceSucceeded) 79 OnUnpackWebResourceSucceeded)
77 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnpackWebResource_Failed, 80 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnpackWebResource_Failed,
78 OnUnpackWebResourceFailed) 81 OnUnpackWebResourceFailed)
79 IPC_MESSAGE_UNHANDLED(handled = false) 82 IPC_MESSAGE_UNHANDLED(handled = false)
80 IPC_END_MESSAGE_MAP() 83 IPC_END_MESSAGE_MAP()
81 return handled; 84 return handled;
82 } 85 }
83 86
84 virtual void OnProcessCrashed(int exit_code) { 87 virtual void OnProcessCrashed(int exit_code) OVERRIDE {
85 if (got_response_) 88 if (got_response_)
86 return; 89 return;
87 90
88 OnUnpackWebResourceFailed( 91 OnUnpackWebResourceFailed(
89 "Utility process crashed while trying to retrieve web resources."); 92 "Utility process crashed while trying to retrieve web resources.");
90 } 93 }
91 94
92 void OnUnpackWebResourceSucceeded( 95 void OnUnpackWebResourceSucceeded(
93 const DictionaryValue& parsed_json) { 96 const DictionaryValue& parsed_json) {
94 web_resource_service_->Unpack(parsed_json); 97 web_resource_service_->Unpack(parsed_json);
95 Cleanup(); 98 Cleanup();
96 } 99 }
97 100
98 void OnUnpackWebResourceFailed(const std::string& error_message) { 101 void OnUnpackWebResourceFailed(const std::string& error_message) {
99 LOG(ERROR) << error_message; 102 LOG(ERROR) << error_message;
100 Cleanup(); 103 Cleanup();
101 } 104 }
102 105
103 // Release reference and set got_response_. 106 // Release reference and set got_response_.
104 void Cleanup() { 107 void Cleanup() {
105 DCHECK(!got_response_); 108 DCHECK(!got_response_);
106 got_response_ = true; 109 got_response_ = true;
107 110
108 web_resource_service_->EndFetch(); 111 web_resource_service_->EndFetch();
109 Release(); 112 Release();
110 } 113 }
111 114
112 void StartProcessOnIOThread(BrowserThread::ID thread_id, 115 void StartProcessOnIOThread(BrowserThread::ID thread_id,
113 const std::string& json_data) { 116 const std::string& json_data) {
114 UtilityProcessHost* host = new UtilityProcessHost(this, thread_id); 117 UtilityProcessHost* host = UtilityProcessHost::Create(this, thread_id);
115 host->set_use_linux_zygote(true); 118 host->EnableZygote();
116 // TODO(mrc): get proper file path when we start using web resources 119 // TODO(mrc): get proper file path when we start using web resources
117 // that need to be unpacked. 120 // that need to be unpacked.
118 host->Send(new ChromeUtilityMsg_UnpackWebResource(json_data)); 121 host->Send(new ChromeUtilityMsg_UnpackWebResource(json_data));
119 } 122 }
120 123
121 scoped_refptr<WebResourceService> web_resource_service_; 124 scoped_refptr<WebResourceService> web_resource_service_;
122 125
123 // Owned by the global browser process. 126 // Owned by the global browser process.
124 ResourceDispatcherHost* resource_dispatcher_host_; 127 ResourceDispatcherHost* resource_dispatcher_host_;
125 128
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 234
232 std::string data; 235 std::string data;
233 source->GetResponseAsString(&data); 236 source->GetResponseAsString(&data);
234 237
235 // UnpackerClient releases itself. 238 // UnpackerClient releases itself.
236 UnpackerClient* client = new UnpackerClient(this); 239 UnpackerClient* client = new UnpackerClient(this);
237 client->Start(data); 240 client->Start(data);
238 241
239 Release(); 242 Release();
240 } 243 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698