| OLD | NEW |
| 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/json_asynchronous_unpacker.h" | 5 #include "chrome/browser/web_resource/json_asynchronous_unpacker.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/web_resource/web_resource_service.h" | 8 #include "chrome/browser/web_resource/web_resource_service.h" |
| 9 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
| 10 #include "chrome/common/chrome_utility_messages.h" | 10 #include "chrome/common/chrome_utility_messages.h" |
| 11 #include "chrome/common/web_resource/web_resource_unpacker.h" | |
| 12 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/resource_dispatcher_host.h" | |
| 14 #include "content/public/browser/utility_process_host.h" | 12 #include "content/public/browser/utility_process_host.h" |
| 15 #include "content/public/browser/utility_process_host_client.h" | 13 #include "content/public/browser/utility_process_host_client.h" |
| 16 | 14 |
| 17 using content::BrowserThread; | 15 using content::BrowserThread; |
| 18 using content::UtilityProcessHost; | 16 using content::UtilityProcessHost; |
| 19 using content::UtilityProcessHostClient; | 17 using content::UtilityProcessHostClient; |
| 20 | 18 |
| 21 // This class coordinates a web resource unpack and parse task which is run in | 19 // This class coordinates a web resource unpack and parse task which is run in |
| 22 // a separate process. Results are sent back to this class and routed to | 20 // a separate process. Results are sent back to this class and routed to |
| 23 // the WebResourceService. | 21 // the WebResourceService. |
| 24 class JSONAsynchronousUnpackerImpl | 22 class JSONAsynchronousUnpackerImpl |
| 25 : public UtilityProcessHostClient, | 23 : public UtilityProcessHostClient, |
| 26 public JSONAsynchronousUnpacker { | 24 public JSONAsynchronousUnpacker { |
| 27 public: | 25 public: |
| 28 explicit JSONAsynchronousUnpackerImpl( | 26 explicit JSONAsynchronousUnpackerImpl( |
| 29 JSONAsynchronousUnpackerDelegate* delegate) | 27 JSONAsynchronousUnpackerDelegate* delegate) |
| 30 : JSONAsynchronousUnpacker(delegate), | 28 : JSONAsynchronousUnpacker(delegate), |
| 31 got_response_(false) { | 29 got_response_(false) { |
| 32 } | 30 } |
| 33 | 31 |
| 34 virtual void Start(const std::string& json_data) OVERRIDE { | 32 virtual void Start(const std::string& json_data) OVERRIDE { |
| 35 AddRef(); // balanced in Cleanup. | 33 AddRef(); // balanced in Cleanup. |
| 36 | 34 |
| 37 // TODO(willchan): Look for a better signal of whether we're in a unit test | 35 BrowserThread::ID thread_id; |
| 38 // or not. Using |ResourceDispatcherHost::Get()| for this is pretty lame. | 36 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id)); |
| 39 // If we don't have a ResourceDispatcherHost, assume we're in a test and | 37 BrowserThread::PostTask( |
| 40 // run the unpacker directly in-process. | 38 BrowserThread::IO, FROM_HERE, |
| 41 bool use_utility_process = | 39 base::Bind( |
| 42 content::ResourceDispatcherHost::Get() && | 40 &JSONAsynchronousUnpackerImpl::StartProcessOnIOThread, |
| 43 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess); | 41 this, thread_id, json_data)); |
| 44 if (use_utility_process) { | |
| 45 BrowserThread::ID thread_id; | |
| 46 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id)); | |
| 47 BrowserThread::PostTask( | |
| 48 BrowserThread::IO, FROM_HERE, | |
| 49 base::Bind( | |
| 50 &JSONAsynchronousUnpackerImpl::StartProcessOnIOThread, | |
| 51 this, thread_id, json_data)); | |
| 52 } else { | |
| 53 WebResourceUnpacker unpacker(json_data); | |
| 54 if (unpacker.Run()) { | |
| 55 OnUnpackWebResourceSucceeded(*unpacker.parsed_json()); | |
| 56 } else { | |
| 57 OnUnpackWebResourceFailed(unpacker.error_message()); | |
| 58 } | |
| 59 } | |
| 60 } | 42 } |
| 61 | 43 |
| 62 private: | 44 private: |
| 63 virtual ~JSONAsynchronousUnpackerImpl() {} | 45 virtual ~JSONAsynchronousUnpackerImpl() {} |
| 64 | 46 |
| 65 // UtilityProcessHostClient. | 47 // UtilityProcessHostClient. |
| 66 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 48 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| 67 bool handled = true; | 49 bool handled = true; |
| 68 IPC_BEGIN_MESSAGE_MAP(JSONAsynchronousUnpackerImpl, message) | 50 IPC_BEGIN_MESSAGE_MAP(JSONAsynchronousUnpackerImpl, message) |
| 69 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnpackWebResource_Succeeded, | 51 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnpackWebResource_Succeeded, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 // True if we got a response from the utility process and have cleaned up | 98 // True if we got a response from the utility process and have cleaned up |
| 117 // already. | 99 // already. |
| 118 bool got_response_; | 100 bool got_response_; |
| 119 }; | 101 }; |
| 120 | 102 |
| 121 JSONAsynchronousUnpacker* JSONAsynchronousUnpacker::Create( | 103 JSONAsynchronousUnpacker* JSONAsynchronousUnpacker::Create( |
| 122 JSONAsynchronousUnpackerDelegate* delegate) { | 104 JSONAsynchronousUnpackerDelegate* delegate) { |
| 123 return new JSONAsynchronousUnpackerImpl(delegate); | 105 return new JSONAsynchronousUnpackerImpl(delegate); |
| 124 } | 106 } |
| 125 | 107 |
| OLD | NEW |