Home | History | Annotate | Download | only in updater
      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/extensions/updater/safe_manifest_parser.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/command_line.h"
      9 #include "base/location.h"
     10 #include "base/logging.h"
     11 #include "chrome/common/chrome_utility_messages.h"
     12 #include "content/public/browser/browser_thread.h"
     13 #include "content/public/browser/utility_process_host.h"
     14 #include "content/public/common/content_switches.h"
     15 #include "ipc/ipc_message_macros.h"
     16 
     17 using content::BrowserThread;
     18 
     19 namespace extensions {
     20 
     21 SafeManifestParser::SafeManifestParser(const std::string& xml,
     22                                        ManifestFetchData* fetch_data,
     23                                        const UpdateCallback& update_callback)
     24     : xml_(xml),
     25       fetch_data_(fetch_data),
     26       update_callback_(update_callback) {
     27   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     28 }
     29 
     30 void SafeManifestParser::Start() {
     31   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     32   if (!BrowserThread::PostTask(
     33           BrowserThread::IO, FROM_HERE,
     34           base::Bind(&SafeManifestParser::ParseInSandbox, this))) {
     35     NOTREACHED();
     36   }
     37 }
     38 
     39 SafeManifestParser::~SafeManifestParser() {
     40   // If we're using UtilityProcessHost, we may not be destroyed on
     41   // the UI or IO thread.
     42 }
     43 
     44 void SafeManifestParser::ParseInSandbox() {
     45   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     46 
     47   content::UtilityProcessHost* host = content::UtilityProcessHost::Create(
     48       this,
     49       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get());
     50   host->EnableZygote();
     51   host->Send(new ChromeUtilityMsg_ParseUpdateManifest(xml_));
     52 }
     53 
     54 bool SafeManifestParser::OnMessageReceived(const IPC::Message& message) {
     55   bool handled = true;
     56   IPC_BEGIN_MESSAGE_MAP(SafeManifestParser, message)
     57     IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseUpdateManifest_Succeeded,
     58                         OnParseUpdateManifestSucceeded)
     59     IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseUpdateManifest_Failed,
     60                         OnParseUpdateManifestFailed)
     61     IPC_MESSAGE_UNHANDLED(handled = false)
     62   IPC_END_MESSAGE_MAP()
     63   return handled;
     64 }
     65 
     66 void SafeManifestParser::OnParseUpdateManifestSucceeded(
     67     const UpdateManifest::Results& results) {
     68   VLOG(2) << "parsing manifest succeeded (" << fetch_data_->full_url() << ")";
     69   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     70   update_callback_.Run(*fetch_data_, &results);
     71 }
     72 
     73 void SafeManifestParser::OnParseUpdateManifestFailed(
     74     const std::string& error_message) {
     75   VLOG(2) << "parsing manifest failed (" << fetch_data_->full_url() << ")";
     76   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     77   LOG(WARNING) << "Error parsing update manifest:\n" << error_message;
     78   update_callback_.Run(*fetch_data_, NULL);
     79 }
     80 
     81 }  // namespace extensions
     82