Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / pdf / pdf.js
index 7c3d906..0616c8d 100644 (file)
@@ -4,10 +4,6 @@
 
 'use strict';
 
-<include src="../../../../ui/webui/resources/js/util.js">
-<include src="pdf_scripting_api.js">
-<include src="viewport.js">
-
 /**
  * @return {number} Width of a scrollbar in pixels
  */
@@ -33,8 +29,11 @@ PDFViewer.MIN_TOOLBAR_OFFSET = 15;
 /**
  * Creates a new PDFViewer. There should only be one of these objects per
  * document.
+ * @param {Object} streamDetails The stream object which points to the data
+ *     contained in the PDF.
  */
-function PDFViewer() {
+function PDFViewer(streamDetails) {
+  this.streamDetails = streamDetails;
   this.loaded = false;
 
   // The sizer element is placed behind the plugin element to cause scrollbars
@@ -76,28 +75,6 @@ function PDFViewer() {
                           false);
   this.sendScriptingMessage_({type: 'readyToReceive'});
 
-  // If the viewer is started from a MIME type request, there will be a
-  // background page and stream details object with the details of the request.
-  // Otherwise, we take the query string of the URL to indicate the URL of the
-  // PDF to load. This is used for print preview in particular.
-  if (chrome.extension.getBackgroundPage &&
-      chrome.extension.getBackgroundPage()) {
-    this.streamDetails =
-        chrome.extension.getBackgroundPage().popStreamDetails();
-  }
-
-  if (!this.streamDetails) {
-    // The URL of this page will be of the form
-    // "chrome-extension://<extension id>?<pdf url>". We pull out the <pdf url>
-    // part here.
-    var url = window.location.search.substring(1);
-    this.streamDetails = {
-      streamUrl: url,
-      originalUrl: url,
-      responseHeaders: ''
-    };
-  }
-
   this.plugin_.setAttribute('src', this.streamDetails.originalUrl);
   this.plugin_.setAttribute('stream-url', this.streamDetails.streamUrl);
   var headers = '';
@@ -146,10 +123,17 @@ function PDFViewer() {
       var MIN_ZOOM_DELTA = 0.01;
       var zoomDelta = Math.abs(this.viewport_.zoom -
                                zoomChangeInfo.newZoomFactor);
-      if (zoomDelta > MIN_ZOOM_DELTA)
+      // We should not change zoom level when we are responsible for initiating
+      // the zoom. onZoomChange() is called before setZoomComplete() callback
+      // when we initiate the zoom.
+      if ((zoomDelta > MIN_ZOOM_DELTA) && !this.setZoomInProgress_)
         this.viewport_.setZoom(zoomChangeInfo.newZoomFactor);
     }.bind(this));
   }
+
+  // Parse open pdf parameters.
+  var paramsParser = new OpenPDFParamsParser(this.streamDetails.originalUrl);
+  this.urlParams_ = paramsParser.urlParams;
 }
 
 PDFViewer.prototype = {
@@ -174,7 +158,7 @@ PDFViewer.prototype = {
         position.y -= this.viewport.size.height;
         this.viewport.position = position;
       }
-    };
+    }.bind(this);
     var pageDownHandler = function() {
       // Go to the next page if we are fit-to-page.
       if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) {
@@ -185,7 +169,7 @@ PDFViewer.prototype = {
         position.y += this.viewport.size.height;
         this.viewport.position = position;
       }
-    };
+    }.bind(this);
 
     switch (e.keyCode) {
       case 32:  // Space key.
@@ -250,6 +234,20 @@ PDFViewer.prototype = {
           e.preventDefault();
         }
         return;
+      case 219:  // left bracket.
+        if (e.ctrlKey) {
+          this.plugin_.postMessage({
+            type: 'rotateCounterclockwise',
+          });
+        }
+        return;
+      case 221:  // right bracket.
+        if (e.ctrlKey) {
+          this.plugin_.postMessage({
+            type: 'rotateClockwise',
+          });
+        }
+        return;
     }
   },
 
@@ -265,6 +263,26 @@ PDFViewer.prototype = {
 
   /**
    * @private
+   * Handle open pdf parameters. This function updates the viewport as per
+   * the parameters mentioned in the url while opening pdf. The order is
+   * important as later actions can override the effects of previous actions.
+   */
+  handleURLParams_: function() {
+    if (this.urlParams_.page)
+      this.viewport_.goToPage(this.urlParams_.page);
+    if (this.urlParams_.position) {
+      // Make sure we don't cancel effect of page parameter.
+      this.viewport_.position = {
+        x: this.viewport_.position.x + this.urlParams_.position.x,
+        y: this.viewport_.position.y + this.urlParams_.position.y
+      };
+    }
+    if (this.urlParams_.zoom)
+      this.viewport_.setZoom(this.urlParams_.zoom);
+  },
+
+  /**
+   * @private
    * Update the loading progress of the document in response to a progress
    * message being received from the plugin.
    * @param {number} progress the progress as a percentage.
@@ -282,14 +300,15 @@ PDFViewer.prototype = {
       }
     } else if (progress == 100) {
       // Document load complete.
+      if (this.lastViewportPosition_)
+        this.viewport_.position = this.lastViewportPosition_;
+      this.handleURLParams_();
       this.loaded = true;
       var loadEvent = new Event('pdfload');
       window.dispatchEvent(loadEvent);
       this.sendScriptingMessage_({
         type: 'documentLoaded'
       });
-      if (this.lastViewportPosition_)
-        this.viewport_.position = this.lastViewportPosition_;
     }
   },
 
@@ -544,5 +563,3 @@ PDFViewer.prototype = {
     return this.viewport_;
   }
 };
-
-var viewer = new PDFViewer();