Home | History | Annotate | Download | only in content
      1 /*
      2  * Copyright (C) 2012 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *    * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *    * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *    * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef CONTENT_RENDERER_ANDROID_CONTENT_DETECTOR_H_
     32 #define CONTENT_RENDERER_ANDROID_CONTENT_DETECTOR_H_
     33 #pragma once
     34 
     35 #include "build/build_config.h"  // Needed for OS_ANDROID
     36 
     37 #if defined(OS_ANDROID)
     38 
     39 #include "base/string_util.h"
     40 #include "googleurl/src/gurl.h"
     41 #include "public/WebRange.h"
     42 
     43 namespace WebKit {
     44 class WebHitTestInfo;
     45 }
     46 
     47 namespace WebCore {
     48 class Settings;
     49 }
     50 
     51 // Base class for text-based content detectors.
     52 class ContentDetector {
     53  public:
     54 
     55   // Holds the content detection results.
     56   struct Result {
     57     bool valid; // Flag indicating if the result is valid.
     58     WebKit::WebRange range; // Range describing the content boundaries.
     59     std::string text; // Processed text of the content.
     60     GURL intent_url; // URL of the intent that should process this content.
     61 
     62     Result() : valid(false) {}
     63 
     64     Result(const WebKit::WebRange& range,
     65            const std::string& text,
     66            const GURL& intent_url)
     67         : valid(true),
     68           range(range),
     69           text(text),
     70           intent_url(intent_url) {}
     71   };
     72 
     73   virtual ~ContentDetector() {}
     74 
     75   // Returns a WebKit range delimiting the contents found around the tapped
     76   // position. If no content is found a null range will be returned.
     77   Result FindTappedContent(const WebKit::WebHitTestInfo& hit_test);
     78 
     79  protected:
     80   // Parses the input string defined by the begin/end iterators returning true
     81   // if the desired content is found. The start and end positions relative to
     82   // the input iterators are returned in start_pos and end_pos.
     83   // The end position is assumed to be non-inclusive.
     84   virtual bool FindContent(const string16::const_iterator& begin,
     85                            const string16::const_iterator& end,
     86                            size_t* start_pos,
     87                            size_t* end_pos) = 0;
     88 
     89   virtual bool IsEnabled(const WebKit::WebHitTestInfo& hit_test) = 0;
     90   WebCore::Settings* GetSettings(const WebKit::WebHitTestInfo& hit_test);
     91 
     92   // Extracts and processes the text of the detected content.
     93   virtual std::string GetContentText(const WebKit::WebRange& range) = 0;
     94 
     95   // Returns the intent URL that should process the content, if any.
     96   virtual GURL GetIntentURL(const std::string& content_text) = 0;
     97 
     98   // Returns the maximum length of text to be extracted around the tapped
     99   // position in order to search for content.
    100   virtual size_t GetMaximumContentLength() = 0;
    101 
    102   ContentDetector() {}
    103   WebKit::WebRange FindContentRange(const WebKit::WebHitTestInfo& hit_test);
    104 
    105   DISALLOW_COPY_AND_ASSIGN(ContentDetector);
    106 };
    107 
    108 #endif  // defined(OS_ANDROID)
    109 
    110 #endif  // CONTENT_RENDERER_ANDROID_CONTENT_DETECTOR_H_
    111