Fix screenshot error when no screenshot is requested.

BUG=chromium:237142,chromium:239890
TEST=Verfied spurious error message is fixed.

Change-Id: I77129990f85e324e67066a55e2b0d4ecbd7d58d2
Reviewed-on: https://gerrit.chromium.org/gerrit/50980
Reviewed-by: Andrew de los Reyes <adlr@chromium.org>
Commit-Queue: WeiNan-Peter, Wen <wnwen@chromium.org>
Tested-by: WeiNan-Peter, Wen <wnwen@chromium.org>
diff --git a/mtedit/log.py b/mtedit/log.py
index bccedbd..0d8a9b2 100644
--- a/mtedit/log.py
+++ b/mtedit/log.py
@@ -33,7 +33,7 @@
   """
   if os.path.exists(source):
     if source.endswith(".zip") or source.endswith(".bz2"):
-      return FeedbackLog(source, None)
+      return FeedbackLog(source, options)
     return FileLog(source, options)
   else:
     match = re.search("Report/([0-9]+)", source)
@@ -46,8 +46,8 @@
 
 class AbstractLog(object):
   """
-    A log file consists of the activity log, the evdev log and
-    a system log which can be saved to disk all together.
+    A log file consists of the activity log, the evdev log, a system log, and
+    possibly a screenshot, which can be saved to disk all together.
   """
   def SaveAs(self, filename):
     open(filename, "w").write(self.activity)
@@ -55,6 +55,12 @@
       open(filename + ".evdev", "w").write(self.evdev)
     if self.system:
       open(filename + ".system", "w").write(self.system)
+    if self.image:
+      open(filename + ".jpg", "w").write(self.image)
+
+  def CleanUp(self):
+    if os.path.exists("screenshot.jpg"):
+      os.remove("screenshot.jpg")
 
 
 class FileLog(AbstractLog):
@@ -65,17 +71,21 @@
     self.activity = open(filename).read()
     self.evdev = ""
     self.system = ""
+    self.image = None
     if options.evdev:
       self.evdev = open(options.evdev).read()
     elif os.path.exists(filename + ".evdev"):
       self.evdev = open(filename + ".evdev").read()
     if os.path.exists(filename + ".system"):
       self.system = open(filename + ".system").read()
+    if os.path.exists(filename + ".jpg"):
+      self.image = open(filename + ".jpg").read()
+      file("screenshot.jpg", "w").write(self.image)
 
 
 class FeedbackLog(AbstractLog):
   """
-    Downloads logs from a feedback id or file name
+    Downloads logs and (possibly) screenshot from a feedback id or file name
   """
   def __init__(self, id_or_filename, options):
     if id_or_filename.endswith(".zip") or id_or_filename.endswith(".bz2"):
@@ -88,6 +98,12 @@
       cookie_processor = urllib2.HTTPCookieProcessor(self.cookies)
       opener = urllib2.build_opener(cookie_processor)
       self._DownloadSystemLog(id_or_filename, opener)
+      self.image = None
+      if options.screenshot:
+        self._DownloadScreenshot(id_or_filename, opener)
+      # Only write to screenshot.jpg if we will be viewing the screenshot
+      if not options.download and self.image:
+        file("screenshot.jpg", "w").write(self.image)
 
     self._ExtractSystemLog()
     self._ExtractLogFiles()
@@ -125,7 +141,7 @@
   def _DownloadSystemLog(self, id, opener):
     # First download the report.zip file
     logfile = "report-%s-system_logs.zip" % id
-    url = ("https://feedback.corp.googleusercontent.com/binarydata/"+
+    url = ("https://feedback.corp.googleusercontent.com/binarydata/" +
            "%s?id=%s&logIndex=0") % (logfile, id)
     self.report = opener.open(url).read()
     file("test.zip", "w").write(self.report)
@@ -135,6 +151,14 @@
       self._Login(opener)
       self.report = opener.open(url).read()
 
+  def _DownloadScreenshot(self, id, opener):
+    url = "https://feedback.corp.googleusercontent.com/screenshot?id=%s" % id
+    self.image = None
+    try:
+      self.image = opener.open(url).read()
+    except urllib2.URLError:
+      print "No screenshots available - %s" % url
+
   def _ExtractSystemLog(self):
     if self.report[0:2] == "BZ":
       self.system = bz2.decompress(self.report)
diff --git a/mtedit/main.py b/mtedit/main.py
index 3634f0b..ae51e97 100755
--- a/mtedit/main.py
+++ b/mtedit/main.py
@@ -30,7 +30,7 @@
                     help="set target filename for storing results",
                     metavar="output.log")
   parser.add_option("-d", "--download",
-                    action="store_true", dest="download", default=False,
+                    dest="download", action="store_true", default=False,
                     help="download file only, don't edit.")
   parser.add_option("-e", "--evdev",
                     dest="evdev", default=None,
@@ -46,6 +46,9 @@
   parser.add_option("-s", "--serve",
                     dest="serve", action="store_true", default=False,
                     help="Serve a standalone MTEdit.")
+  parser.add_option("-c", "--screenshot",
+                    dest="screenshot", action="store_true", default=False,
+                    help="Attempt to download screenshot from feedback url.")
   (options, args) = parser.parse_args()
 
   editor = LogEditor(persistent=options.persistent)
@@ -72,5 +75,8 @@
       log = editor.Edit(log)
       log.SaveAs(options.out)
 
+  log.CleanUp()
+
+
 if __name__ == "__main__":
   main(sys.argv)
diff --git a/mtedit/mtedit.html b/mtedit/mtedit.html
index 0a40265..fb78684 100644
--- a/mtedit/mtedit.html
+++ b/mtedit/mtedit.html
@@ -1,3 +1,4 @@
+<!DOCTYPE HTML>
 <html>
   <head>
     <meta http-equiv="Content-type" content="text/html; charset=utf-8">
@@ -445,6 +446,9 @@
   $('.pad').toggle(!isTouchscreen);
   $('.screen').toggle(isTouchscreen);
   $('#inputview').width(inputWidth);
+  $('#fcanvas').toggleClass('screenshot', isTouchscreen);
+  $('.view-container').width(canvasWidth);
+  $('.view-container').height(canvasHeight);
   canvas = document.getElementById('fview');
   canvas.setAttribute('width', canvasWidth);
   canvas.setAttribute('height', canvasHeight);
@@ -678,13 +682,18 @@
         <h2 class="screen">Touchscreen Viewer</h2>
         <div class="viewspan" id="inputview">
           <h3 class="pad">Input</h3>
-          <canvas class="view" id="fview" width="480" height="320"></canvas><br/>
+          <div class="view-container screenshot" id="fcanvas">
+            <canvas class="view" id="fview" width="480" height="320"></canvas>
+          </div>
+          <br/>
           <button class="button" id="in-resetzoom">Reset Zoom</button>
           <div id="intext">Time: 123123123.123<br/>Finger Cnt: 2<br/>Touch Cnt: 2</div>
         </div>
         <div class="viewspan pad">
           <h3>Output</h3>
-          <canvas class="view" id="gview" width="480" height="320"></canvas><br/>
+          <div class="view-container">
+            <canvas class="view" id="gview" width="480" height="320"></canvas>
+          <br/>
           <button class="button" id="out-resetzoom">Reset Zoom</button>
           <input type="checkbox" id="out-lock-head" />
           <label for="out-lock-head">Lock Head Location</label>
diff --git a/mtedit/style.css b/mtedit/style.css
index ee800d7..f2373fb 100644
--- a/mtedit/style.css
+++ b/mtedit/style.css
@@ -53,11 +53,20 @@
 	margin: 0.3em 0 0 1em;
 }
 
+.screenshot {
+	background-image: url(screenshot.jpg);
+	background-repeat: no-repeat;
+	background-size: cover;
+}
+
+.view-container {
+	margin: 0.3em auto 0 auto;
+}
+
 .view {
 	/*box-shadow: inset #888 0 2px 10px;*/
 	border: 1px solid #ccc;
 	display: block;
-	margin: 0.3em auto 0 auto;
 }
 
 .button {